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
Dict
normal You want the
π
where theπ
is1
?
< π= 0, π = π³ |
β < π= 1, π = π |
< π= 2, π = π |
Going through while comparing your
π
... Ah! Here it is:
π
PairDict
You want the pair where
ποΈ
is1
and the pair whereπ
is0
?
β < π= 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: Dict
s 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.