--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
module Group where
import List.Extra exposing (span)
countingGroupBy : (a -> b) -> List a -> List (b, Int)
countingGroupBy key xs' =
let
eq a b = key a == key b
in
case xs' of
[] ->
[]
(x::xs) ->
let
(ys, zs) = span (eq x) xs
in
(key x, (List.length ys) + 1) :: countingGroupBy key zs
countingGroup = countingGroupBy identity
$ eql-repl
> import Group
> Group.countingGroupBy .age [ { age = 10, name = "Ralf" }, { age = 10, name = "Peter" }, { age = 12, name = "Peter" } ]
[(10,2),(12,1)] : List ( number, Int )
> Group.countingGroup ["a", "b", "a", "d", "d", "c"]
[("a",1),("b",1),("a",1),("d",2),("c",1)] : List ( String, Int )
http://package.elm-lang.org/packages/mgold/Elm-Multiset/1.0.5/Multiset
In particular fromList. It uses the Dict based approach described above.
$ eql-repl
$ elm-package install --yes circuithub/elm-list-extra
$ elm-repl
> [ { age = 10, name = "Ralf" }, { age = 10, name = "Peter" }, { age = 12, name = "Peter" } ] |> List.sortBy .age |> Group.countingGroupBy .age
[(10,2),(12,1)] : List ( number, Int )
> ["a", "b", "a", "d", "d", "c"] |> List.sort |> Group.countingGroup
[("a",2),("b",1),("c",1),("d",2)] : List ( String, Int )