Giter Site home page Giter Site logo

robertjlooby / elm-generic-dict Goto Github PK

View Code? Open in Web Editor NEW
8.0 4.0 2.0 145 KB

A dictionary and set implementation for Elm that accepts a user-provided comparer

Home Page: http://package.elm-lang.org/packages/robertjlooby/elm-generic-dict/

License: BSD 3-Clause "New" or "Revised" License

Elm 100.00%

elm-generic-dict's Introduction

GenericDict / GenericSet

Build Status

The core Dict/Set implementations only allows keys of comparable type. This implementation builds off the core implementation but allows the user to provide their own comparer function for ordering the keys. This allows both the storage of arbitrary key types as well as changing the ordering of the elements (which affects how they will be returned from toList for example).

The api is essentially the same as the core Dict/Set except that the "builder" functions (empty, singleton , fromList) take a comparer of type (key -> key -> Order) as their first argument.

import GenericDict exposing (GenericDict)
import String exposing (toLower)

type alias Person =
    { id : Int
    , name : String
    }

dict1 : GenericDict Person Int
dict1 =
    GenericDict.fromList
        (\person1 person2 -> compare person1.id person2.id)
        [ ( { id = 1, name = "Noah" }, 1 ), ( { id = 2, name = "Evan" }, 2 ) ]
-- Dict.fromList [({ id = 1, name = "Noah" }, 1), ({ id = 2, name = "Evan" }, 2)]

dict2 : GenericDict String Int
dict2 =
    GenericDict.fromList
        (\str1 str2 -> compare (toLower str1) (toLower str2))
        [ ( "FIRST", 1 ), ( "SECOND", 2 ), ( "First", 3 ) ]
-- Dict.fromList [("FIRST", 3), ("SECOND", 2)]

f : Maybe Int
f = GenericDict.get "FiRsT" dict2
-- Just 3

This builds off of the core Dict implementation by Evan Czaplicki as well as the similar elm-all-dict by Noah Hall.

elm-generic-dict's People

Contributors

robertjlooby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

maksar owanturist

elm-generic-dict's Issues

Sample codes in README cannot be compiled

I found that sample codes in README.md produces compile errors.
There are two reasons why it fails to be compiled.
First, it uses ' in variable name, which was unsupported after Elm 0.18.
Next, second argument of the fromList is not a list of Tuple.

P.S. I feel that this module has the best function design of all the similar libraries!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.