Moved to lue-bird/elm-keysset
Have many keys to lookup values.
Let's compare
You want the value where the key is
1
?
0 : 🏡
--> 1 : 🏚
2 : 🏚
the key is unique among all items
Going through while comparing your key.
the value is 🏚 where the key is `1`
First of all, get used to some names
- a "house" contains aspects that make it unique & aspects that can be shared across all houses
keepInParserPipelineOperator=
{ operator= "|=" --only one operator should have this symbol!
, origin= ElmRepoPackage "parser" --is the same for |.
}
- a "door" describes a unique aspect across all houses.
door .operator
--is a promise, that the operator value is never the same for two houses.
So... If you have a key and the type of door it could match, you can find the only matching house and enter.
You want to enter the house where
🗝️
is1
?
🔑= 0, 🏠= 🏚, 🗝️= 2
🔑= 2, 🏠= 🏡, 🗝️= 0
🔑= 1, 🏠= 🏚, 🗝️= 1 <--
🔑, 🗝️: doors you can "open" with a key unique across all houses
Going through while checking every house, if the
🗝️
matches.
🔑= 1, 🏠= 🏚, 🗝️= 1 where 🗝️ is 1
You want to enter the house where
🔑
is0
?
--> 🔑= 0, 🏠= 🏚, 🗝️= 2
🔑= 2, 🏠= 🏡, 🗝️= 0
🔑= 1, 🏠= 🏚, 🗝️= 1
🔑, 🗝️: doors you can "open" with a key unique across all houses
Going through while checking every house, if the
🔑
matches.
🔑= 0, 🏠= 🏚, 🗝️= 2 where 🔑 is 0
Try the ellie for some examples (always a version behind).
type alias CasedLetter=
{ lowercase: Char
, uppercase: Char
}
lowerUppercaseLetters: KeysDict CasedLetter
lowerUppercaseLetters=
KeysDict.enterableBy
[ door .lowercase, door .uppercase ]
|>putUp { lowercase= 'a', uppercase= 'A' }
|>putUp { lowercase= 'b', uppercase= 'B' }
|>putUp { lowercase= 'c', uppercase= 'C' }
uppercase char=
lowerUppercaseLetters
|>enterBy { door= .lowercase, key= char }
|>Maybe.map .uppercase
It's recommended to
import KeysDict.Uniqueness exposing (door)
import KeysDict exposing
( KeysDict
, houses, enterBy
, putUp, tearDown
, foldHouses, countHouses --only if you use them
)
They avoid clutter and shouldn't cause name conflicts.
type Element=
Hydrogen
| Helium
elementAtomicNumberKeysDict=
KeysDict.enterableBy
[ door .atomicNumber, door .element ]
|>putUp { element= Hydrogen, atomicNumber= 1 }
|>putUp { element= Helium, atomicNumber= 2 }
atomicNumberByElement=
KeysDict.toDict { key= .element, value= .atomicNumber }
elementAtomicNumberKeysDict
brackets=
KeysDict.enterableBy
[ door .opening, door .closing ]
|>putUp { opening= '(', closing= ')' }
|>putUp { opening= '{', closing= '}' }
typeChar character=
case access { door= .open, key= character } brackets of
Just { closed }->
String.fromValues [ character, closed ]
Nothing->
case access { door= .closed, key= character } brackets of
Just { open }->
String.fromValues [ open, character ]
Nothing->
String.fromChar character
"Typing (: " ++(typeChar '(') ++". Even }: " ++(typeChar '}')
answers=
KeysDict.enterableBy [ door .youSay ]
|>putUp { youSay= "Hi", answer= "Hi there!" }
|>putUp { youSay= "Bye", answer= "Ok, have a nice day and spread some love." }
|>putUp { youSay= "How are you", answer= "I don't have feelings :(" }
|>putUp
{ 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.
englishGerman=
KeysDict.enterableBy []
|>putIn { english= "elm", german= "Ulme" }
|>putIn { english= "git", german= "Schwachkopf" }
|>putIn { german= "Rüste", english= "elm" }
A KeysDict
is only effective, when there is only one unique key for each door.
Please take a look at elm-bidict instead!
Similar to the previous example:
partners=
KeysDict.enterableBy
[ door .partner, door .partnerOfPartner ]
|>putUp { partner= "Ann", partnerOfPartner= "Alan" }
|>putUp { partner= "Alex", partnerOfPartner= "Alastair" }
|>putUp { partner= "Alan", partnerOfPartner= "Ann" }
--wait, this is no duplicate and is inserted
A KeysDict
ony makes sense, when the keys describe something different.