indique/elm-pairdict

lookup value-pairs from the left or the right


Keywords
data-structure, elm, associative, bidict, bidirectional-dictionary
License
MIT
Install
elm-package install indique/elm-pairdict 6.0.1

Documentation

usage DISCOURAGED: use lue-bird/elm-keysset, which allows for multiple keys

Β 

PairDict

Lookup value-pairs from the left or the right.

Let's compare

normal Dict

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

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

Going through while comparing your πŸ”‘... Ah! Here it is:

    🍐

PairDict

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

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

Going through while checking every pair, if πŸ—οΈ is equal, then, if πŸ”‘ is equal... Ah! Here they are:

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

Β 

πŸ‘ How to PairDict

Example: cased letters

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

lowerUppercaseLetters: PairDict CasedLetter Char Char
lowerUppercaseLetters=
  PairDict.empty .lowercase .uppercase
  |>PairDict.putIn { lowercase= 'a', uppercase= 'A' }
  |>PairDict.putIn { lowercase= 'b', uppercase= 'B' }
  |>PairDict.putIn { lowercase= 'c', uppercase= 'C' }

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

try in the ellie for the example cased letters

Example: periodic table

type Element=
  Hydrogen
  | Helium

elementAtomicNumberPairdict=
  PairDict.fromList .element .atomicNumber
    [ { element= Hydrogen, atomicNumber= 1 }
    , { element= Helium, atomicNumber= 2 }
    ]

atomicNumberByElement=
  PairDict.toDict
    elementAtomicNumberPairdict

Example: brackets

You have pairs that belong together:

brackets=
  PairDict.empty .opening .closing
  |>PairDict.putIn { opening= '(', closing= ')' }
  |>PairDict.putIn { opening= '{', closing= '}' }

typeChar character=
  brackets
  |>PairDict.access .open character
  |>Maybe.map
      (\{ closed }->
        String.fromList [ character, closed ]
      )
  |>Maybe.withDefault
      (brackets
      |>PairDict.access .closed character
      |>Maybe.map
          (\{ open }->
            String.fromList [ open, character ]
          )
      |>Maybe.withDefault
          (String.fromChar character)
      )

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

Β 

πŸ‘Ž How not to PairDict

Example: automatic answers

answers=
  PairDict.fromList .youSay .answer
    [ { 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=
  PairDict.fromList .english .german
    [ { english= "elm", german= "Ulme" }
    , { english= "git", german= "Schwachkopf" }
    , { german= "RΓΌster", english= "elm" }
    ]

A right β†’ left and backwards relationship is only fitting, when left or right don't have multiple translations.

Please take a look at elm-bidict

Example: partners, opposites...

Similar to the previous example:

partners=
  PairDict.empty
  |>PairDict.putIn { partner= "Ann", partnerOfPartner= "Alan" }
  |>PairDict.putIn { partner= "Alex", partnerOfPartner= "Alastair" }
  |>PairDict.putIn { partner= "Alan", partnerOfPartner= "Ann" }
      --wait, this is no duplicate and gets putIned?

A PairDict ony makes sense, when the left & right sides describe something different.