indique/elm-keysdict/1.0.0

NOTE: the latest version is 2.0.3

KeysDict

Have many keys to lookup values.

Let's compare

normal Dict

You want the 🏠 where the πŸ”‘ is 1?

    < πŸ”‘= 0, 🏠= 🌳 |
  β†’ < πŸ”‘= 1, 🏠= 🍐 |
    < πŸ”‘= 2, 🏠= 🍐 |


    (πŸ”‘ is unique)

Going through while comparing your πŸ”‘.

    🍐 where `πŸ”‘` is `1`

KeysDict

You want the pair where πŸ—οΈ is 1 and the pair where πŸ”‘ is 0?

  β†’ < πŸ”‘= 0, 🏠= 🍐, πŸ—οΈ= 2 >
    < πŸ”‘= 2, 🏠= 🌳, πŸ—οΈ= 0 >
    < πŸ”‘= 1, 🏠= 🍐, πŸ—οΈ= 1 > ←


    (πŸ”‘ & πŸ—οΈ are unique)

Going through while checking every pair, if πŸ—οΈ is equal, then, if πŸ”‘ is equal.

    πŸ”‘= 1, πŸ—οΈ= 1 where πŸ—οΈ is 1    πŸ”‘= 0, πŸ—οΈ= 2 where πŸ”‘ is 0

Β 

πŸ‘ How to KeysDict

Example: cased letters

type alias CasedLetter=
  { lowercase: Char
  , uppercase: Char
  }

lowerUppercaseLetters: KeysDict CasedLetter
lowerUppercaseLetters=
  KeysDict.empty [ unique .lowercase, unique .uppercase ]
  |>KeysDict.putIn { lowercase= 'a', uppercase= 'A' }
  |>KeysDict.putIn { lowercase= 'b', uppercase= 'B' }
  |>KeysDict.putIn { lowercase= 'c', uppercase= 'C' }

uppercase char=
  KeysDict.access .lowercase char
    lowerUppercaseLetters
  |>Maybe.map .uppercase

try in the ellie for the example cased letters (always a version behind)

Example: periodic table

type Element=
  Hydrogen
  | Helium

elementAtomicNumberKeysDict=
  KeysDict.fromList
    [ unique .atomicNumber, unique .element ]
    [ { element= Hydrogen, atomicNumber= 1 }
    , { element= Helium, atomicNumber= 2 }
    ]

atomicNumberByElement=
  KeysDict.toDict .element .atomicNumber
    elementAtomicNumberKeysDict

Example: brackets

You have pairs that belong together:

brackets=
  KeysDict.empty [ unique .opening, unique .closing ]
  |>KeysDict.putIn { opening= '(', closing= ')' }
  |>KeysDict.putIn { opening= '{', closing= '}' }

typeChar character=
  case KeysDict.access .open character brackets of
    Just { closed }->
      String.fromList [ character, closed ]

    Nothing->
      case KeysDict.access .closed character brackets of
        Just \{ open }->
          String.fromList [ open, character ]
          
        Nothing->
          String.fromChar character

"Typing (: " ++(typeChar '(') ++". Even }: " ++(typeChar '}')

Β 

πŸ‘Ž How not to KeysDict

Example: automatic answers

answers=
  KeysDict.fromList [ unique .youSay ]
    [ { youSay= "Hi", answer= "Hi there!" }
    , { youSay= "Bye", answer=  "Ok, have a nice day and spread some love." }
    , { youSay= "How are you", answer= "I don't have feelings :(" }
    , { youSay= "Are you a robot"
      , answer= "I think the most human answer is 'Haha... yes'"
      }
    ]

please use a Dict where it is more appropriate: Dicts are for one-way access

Example: translation, synonymes...

englishGerman=
  KeysDict.fromList []
    [ { english= "elm", german= "Ulme" }
    , { english= "git", german= "Schwachkopf" }
    , { german= "RΓΌster", english= "elm" }
    ]

A KeysDict is only effective, when there is only one unique key.

Please take a look at elm-bidict instead!

Example: partners, opposites...

Similar to the previous example:

partners=
  KeysDict.empty [ unique .partner, unique .partnerOfPartner ]
  |>KeysDict.putIn { partner= "Ann", partnerOfPartner= "Alan" }
  |>KeysDict.putIn { partner= "Alex", partnerOfPartner= "Alastair" }
  |>KeysDict.putIn { partner= "Alan", partnerOfPartner= "Ann" }
      --wait, this is no duplicate and is inserted

A KeysDict ony makes sense, when the keys describe something different.