-
Notifications
You must be signed in to change notification settings - Fork 1
/
Grouped.elm
67 lines (46 loc) · 1.36 KB
/
Grouped.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module Main exposing (..)
import Html exposing (..)
import List.Extra
-- MODEL
type alias Model =
List Airport
type alias Airport =
{ country : String, name : String }
type alias CountryGroup =
{ country : String, airports : List Airport }
initialModel : Model
initialModel =
[ { country = "Luxemburg", name = "Luxembourg Airport" }
, { country = "Germany", name = "Aiport Hahn" }
, { country = "Belgium", name = "Aiport Charleroi" }
, { country = "Germany", name = "Frankfurt Airport" }
, { country = "Belgium", name = "Aiport Bru" }
]
groupByCountry : Model -> List CountryGroup
groupByCountry model =
let
airportIn country =
List.filter (\x -> x.country == country) model
in
model
|> List.map .country
|> List.Extra.unique
|> List.map (\c -> { country = c, airports = (airportIn c) })
-- VIEW
view : Model -> Html a
view model =
let
viewGroup airportGroup =
div []
[ h1 [] [ text airportGroup.country ]
, ul [] (List.map viewAirport airportGroup.airports)
]
viewAirport airport =
li [] [ text (airport.country ++ ": " ++ airport.name) ]
in
groupByCountry model
|> List.map viewGroup
|> div []
main : Html a
main =
view initialModel