emilianobovetti / list-assoc / List.Assoc

Some basic functions are missing by design: operations like add an entry, map, concat are provided by Elm's standard library so there is no need to have a List.Assoc version of them.

Search, filter and deletion

getFirst : k -> List ( k, v ) -> Maybe v

Gets the value of the first entry that has a given key.

getAll : k -> List ( k, v ) -> List v

Gives the values of all entries that with a given key.

findFirst : (k -> Basics.Bool) -> List ( k, v ) -> Maybe ( k, v )

Finds the first tuple with a key that satisfy some predicate.

removeFirst : k -> List ( k, v ) -> List ( k, v )

Removes the first pair with a given key.

removeFirstWith : (k -> Basics.Bool) -> List ( k, v ) -> List ( k, v )

Works like removeFirst, but takes a (k -> Bool) predicate.

removeAll : k -> List ( k, v ) -> List ( k, v )

Removes all entries that have some key.

filter : (k -> Basics.Bool) -> List ( k, v ) -> List ( k, v )

The same as List.filter, but the test is made on the key. If you need a removeAllWith or a findAll then this is your guy.

Grouping

group : List ( comparable, v ) -> List ( comparable, List v )

Sorts the list by key then groups together adjacent entries with the same key.

type alias User =
    { name : String
    , age : Int
    }

users : List User
users =
    [ { name = "bob", age = 20 }
    , { name = "alice", age = 25 }
    , { name = "bob", age = 32 }
    ]

usersByName : List ( String, List User )
usersByName =
    |> List.map (\p -> ( p.name, p ))
    |> List.Assoc.group

usersByName ==
    [ ( "alice", [ { age = 25, name = "alice" } ] )
    , ( "bob", [ { age = 20, name = "bob" }, { age = 32, name = "bob" } ] )
    ]

groupBy : (k -> comparable) -> List ( k, v ) -> List ( k, List v )

Same as group, but takes a function to map a key to comparable.

Cf. List.sortBy

groupWith : (k -> k -> Basics.Order) -> List ( k, v ) -> List ( k, List v )

Same as group, but takes a custom comparison function.

Cf. List.sortWith