What would an Elm function definition look like which takes a list of records, and perform a groupby operation (see example below)? Using a SQL analogy, I'm basically trying to achieve select last, sum(age) from table group by last
in Elm.
[{name= "John", last= "Smith", age= 10}
,{name="Jason", last= "Smith", age= 9}
, {name="Jane", last ="White", age =5}]
return [{last="Smith", age =19},
{last ="White", age =5}]
You could use Dict.Extra.groupBy
to perform the initial grouping, then sum the ages by mapping over the list and summing the ages:
import Dict.Extra exposing (groupBy)
familySumAges : List Person -> List Family
familySumAges =
groupBy .last
>> Dict.map (\_ -> List.map .age >> List.sum)
>> Dict.toList
>> List.map (uncurry Family)
The following answer courtesy of @holy-meekrob and @ilias from the Elm Slack channel. https://ellie-app.com/7tqY9w6gNa1/1
module Main exposing (..)
import Dict exposing (..)
import Html exposing (text)
type alias Person =
{ name : String
, last : String
, age : Int
}
type alias Family =
{ last : String
, age : Int
}
people : List Person
people =
[ { name = "John"
, last = "Smith"
, age = 10
}
, { name = "Jason"
, last = "Smith"
, age = 9
}
, { name = "Jane"
, last = "White"
, age = 5
}
]
sumAges : Person -> Dict String Family -> Dict String Family
sumAges person families =
Dict.update
person.last
(\family ->
case family of
Nothing ->
Just { last = person.last, age = person.age }
Just fam ->
Just { last = person.last, age = fam.age + person.age }
)
families
main =
text (toString (List.foldl sumAges Dict.empty people |> Dict.values))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With