Elm Dict Package /elm-lang/core/1.0.0/Dict
The Dict api is useful when situation comes to store key , pair value.It is Similar to Map / Dictionary in some other language.
Here you can find some basic function to play with Dict Api.
Use elm rector
to execute this samples.
elm rector
to execute this samples.Dict.insert
Kind of put operation to store key , value pair in Dict
import Html exposing (Html,text)
import Dict exposing (Dict)
main = text (toString (insertIntoDict "Apple" 1 Dict.empty))
insertIntoDict: String -> Int -> Dict String Int -> Dict String Int
insertIntoDict key input myMap =
let
newMap = Dict.insert key input myMap
in
newMap
Repl
> import Dict
> Dict.insert "A" 1 Dict.empty
Dict.fromList [("A",1)] : Dict.Dict String number
Dict.insert
function expection three parameter
- Key
- Value
- Dict (This dict can be empty dict for the first time)
import Html exposing (Html,text)
import Dict exposing (Dict)
main = text (toString (insertIntoDict "Apple" 1 Dict.empty))
insertIntoDict: String -> Int -> Dict String Int -> Dict String Int
insertIntoDict key input myMap =
let
newMap = Dict.insert key input myMap
in
newMap
> import Dict
> Dict.insert "A" 1 Dict.empty
Dict.fromList [("A",1)] : Dict.Dict String number
Dict.insert
function expection three parameterDict.update
Update need a handler to check before performing the update action, That check function is Higher order function that too accpet Maybe type parameter.
import Html exposing (Html,text)
import Dict exposing (Dict)
main = text (toString view)
view =
let
appleDict = insertIntoDict "Apple" 1 Dict.empty
in
insertIntoDict "Apple" 2 appleDict
process : Int -> Maybe Int -> Maybe Int
process x y = case y of
Just val -> Just x
Nothing -> Just 1
insertIntoDict: String -> Int -> Dict String Int -> Dict String Int
insertIntoDict key input myMap=
let
newMap = case (Dict.get key myMap) of
Nothing -> Dict.insert key input myMap
Just val -> Dict.update key (process input) myMap
in
newMap
Dict.update key (process input) myMap
Dict Update function expect second parameter to be a higer order function and That should have signature of
Maybe a -> Maybe a
Int this process
function we are updating the newValue = 2 to the "Apple" key which already exist in Dict with oldValue = 1.
This process
function acctualy expecting first paramter as Int which is new value that to be updated on Dict. This Int value curried out and applied as new value (clousure) to the higher order function. Find the process Signature
process : Int -> Maybe Int -> Maybe Int
import Html exposing (Html,text)
import Dict exposing (Dict)
main = text (toString view)
view =
let
appleDict = insertIntoDict "Apple" 1 Dict.empty
in
insertIntoDict "Apple" 2 appleDict
process : Int -> Maybe Int -> Maybe Int
process x y = case y of
Just val -> Just x
Nothing -> Just 1
insertIntoDict: String -> Int -> Dict String Int -> Dict String Int
insertIntoDict key input myMap=
let
newMap = case (Dict.get key myMap) of
Nothing -> Dict.insert key input myMap
Just val -> Dict.update key (process input) myMap
in
newMap
Dict.update key (process input) myMap
Maybe a -> Maybe a
process
function we are updating the newValue = 2 to the "Apple" key which already exist in Dict with oldValue = 1.process
function acctualy expecting first paramter as Int which is new value that to be updated on Dict. This Int value curried out and applied as new value (clousure) to the higher order function. Find the process Signature process : Int -> Maybe Int -> Maybe Int
Dict.get
Get function to read the value based on the key from Dict
import Html exposing (Html,text)
import Dict exposing (Dict)
main = text (toString view)
view =
let
appleDict = insertIntoDict "Apple" 1 Dict.empty
appleDict2 = insertIntoDict "Apple" 2 appleDict
in
readFromDict "Apple" appleDict2
process : Int -> Maybe Int -> Maybe Int
process x y = case y of
Just val -> Just x
Nothing -> Just 0
insertIntoDict: String -> Int -> Dict String Int -> Dict String Int
insertIntoDict key input myMap=
let
newMap = case (Dict.get key myMap) of
Nothing -> Dict.insert key input myMap
Just val -> Dict.update key (process input) myMap
in
newMap
readFromDict : String -> Dict String Int -> Int
readFromDict key map =
case (Dict.get key map) of
Nothing -> 0
Just v -> v
Dict.get
function return the Maybe. Which need to be further processed to extract the value. This sample use the pattern matching (case
) to process the Int value.
case (Dict.get key map) of
Nothing -> 0
Just v -> v
import Html exposing (Html,text)
import Dict exposing (Dict)
main = text (toString view)
view =
let
appleDict = insertIntoDict "Apple" 1 Dict.empty
appleDict2 = insertIntoDict "Apple" 2 appleDict
in
readFromDict "Apple" appleDict2
process : Int -> Maybe Int -> Maybe Int
process x y = case y of
Just val -> Just x
Nothing -> Just 0
insertIntoDict: String -> Int -> Dict String Int -> Dict String Int
insertIntoDict key input myMap=
let
newMap = case (Dict.get key myMap) of
Nothing -> Dict.insert key input myMap
Just val -> Dict.update key (process input) myMap
in
newMap
readFromDict : String -> Dict String Int -> Int
readFromDict key map =
case (Dict.get key map) of
Nothing -> 0
Just v -> v
Dict.get
function return the Maybe. Which need to be further processed to extract the value. This sample use the pattern matching (case
) to process the Int value.case (Dict.get key map) of
Nothing -> 0
Just v -> v