Created
August 23, 2016 13:45
-
-
Save blogscot/85b3a9442ee8fc77bde074c18275668c to your computer and use it in GitHub Desktop.
Experimenting with Elm Dictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (text, div, br) | |
import Dict exposing (Dict) | |
times3 : Maybe Int -> Maybe Int | |
times3 x = | |
case x of | |
Just num -> Just <| num * 3 | |
_ -> Nothing | |
times3' : Char -> Int -> Int | |
times3' key value = | |
value * 3 | |
a : Dict Char Int | |
a = Dict.insert 'a' 1 Dict.empty | |
b : Dict Char Int | |
b = Dict.update 'a' times3 a | |
c : Dict Char Int | |
c = Dict.insert 'b' 2 b | |
d : Dict Char Int | |
d = Dict.map times3' c | |
e : Dict Char Int | |
e = Dict.map (\key value -> value * 4) c | |
f = Dict.foldl (\key x acc -> acc + x) 0 e | |
main = | |
div | |
[] | |
[ text ("a is: " ++ toString a) | |
, br [] [] | |
, text ("b is: " ++ toString b) | |
, br [] [] | |
, text ("c is: " ++ toString c) | |
, br [] [] | |
, text ("d is: " ++ toString d) | |
, br [] [] | |
, text ("e is: " ++ toString e) | |
, br [] [] | |
, text ("f is: " ++ toString f) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment