Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created January 21, 2018 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoto-ogawa/2618e037978ec295a35d490b478ff83e to your computer and use it in GitHub Desktop.
Save naoto-ogawa/2618e037978ec295a35d490b478ff83e to your computer and use it in GitHub Desktop.
a hetero-dict sample

How to use hetero-dict.

Preparation

> :set -XDataKinds
> import Data.Proxy

Preparation for Keys

> :set -XDataKinds
> let k1 = Proxy :: Proxy "aaa"
> let k2 = Proxy :: Proxy "bbb"

Add key-values to a Map

> :set -XDataKinds
> let m1 = add k1 1 empty
> m1
DynDict {aaa = 1 :: Integer}
> let m2 = add k2 'c' m1
> m2
DynDict {bbb = 'c' :: Char, aaa = 1 :: Integer}

Retrive values

> get k1 m2
1
> :t get k1 m2
> v

> get k2 m2
'c'
> :t get k2 m2
get k2 m2 :: Char
>
> get (Proxy :: Proxy "bbb") m2
'c'

Overwrite data

> let m3 = add k1 9  m2

>:23:10: error:
    • Couldn't match type ‘'Data.Hetero.KVList.DuplicatedKey "aaa"’
                     with ‘'Data.Hetero.KVList.HasKey "aaa"’
        arising from a use of ‘add’
    • In the expression: add k1 9 m2
      In an equation for ‘m3’: m3 = add k1 9 m2
> let m3 = set k1 9  m2
> m3
DynDict {bbb = 'c' :: Char, aaa = 9 :: Integer}
>

Modify data

> let m4 = modify k1 (\x -> x + 1) m3
> m4
DynDict {bbb = 'c' :: Char, aaa = 10 :: Integer}
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment