Skip to content

Instantly share code, notes, and snippets.

@amitaibu amitaibu/Main.elm forked from anonymous/Main.elm
Created May 14, 2017

Embed
What would you like to do?
contacts page
{
"version": "1.0.0",
"summary": "Tell the world about your project!",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 5.1.1",
"elm-lang/html": "2.0.0 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
<html>
<head>
<style>
html {
background: #F7F7F7;
color: red;
}
</style>
</head>
<body>
<script>
var app = Elm.Main.fullscreen()
</script>
</body>
</html>
module Main exposing (..)
import Html exposing (Html, div, li, text, ul)
main : Html a
main =
div []
[ div [] [ text <| "Showing contacts for language " ++ toString model.language ]
, viewContacts model.language model.contacts
]
type Language
= Arabic
| Hebrew
type Name
= Name Language String
type alias Contact =
{ name : List Name
, phone : String
}
contact1 : Contact
contact1 =
{ name =
[ Name Arabic "Foo"
, Name Hebrew "Bar"
]
, phone = "1234"
}
contact2 : Contact
contact2 =
{ name =
[ Name Hebrew "Nik"
]
, phone = "5678"
}
type alias Model =
{ language : Language
, contacts : List Contact
}
model : Model
model =
{ language = Hebrew
, contacts = [ contact1, contact2 ]
}
viewContacts : Language -> List Contact -> Html msg
viewContacts language contacts =
div []
(List.map
(\contact ->
ul
[]
[ li [] [ text <| getContactNameByLanguage language contact ]
, li [] [ text contact.phone ]
]
)
(getContactsByLanguage language contacts)
)
{-| Determine if we have the name of the contact in the given language.
-}
getContactsByLanguage : Language -> List Contact -> List Contact
getContactsByLanguage language contacts =
List.filter
(\contact ->
List.foldl
(\name accum ->
if accum == True then
-- We already found a matching language.
True
else
let
-- Extract the language form the name.
(Name nameLanguage _) =
name
in
language == nameLanguage
)
False
contact.name
)
contacts
{-| Get the name by language. In case language doesn't exist, we show an emoty
string, but that should never be the case.
-}
getContactNameByLanguage : Language -> Contact -> String
getContactNameByLanguage language contact =
List.foldl
(\name accum ->
let
(Name nameLanguage nameString) =
name
in
if nameLanguage == language then
nameString
else
accum
)
""
contact.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.