KeysSetLookup elements by their unique aspects.
For a KeysSet with some elements
{ flag = "π¦πΊ", code = "AU", name = "Australia" }
{ flag = "π¦πΆ", code = "AQ", name = "Antarctica" }
{ flag = "π±π§", code = "LB", name = "Lebanon" }you can specify aspects that will be unique across all elements.
KeysSet.promising
[ unique .flag, unique .code ]If you have a key and the aspect to check if it matches, you can find the matching element:
KeysSet.at .flag "π¦πΆ"
--> Just { flag = "π¦πΆ", code = "AQ", name = "Antarctica" }
KeysSet.at .code "LB"
--> Just { flag = "π±π§", code = "LB", name = "Lebanon" }Β
operators =
KeysSet.promising
[ unique .symbol, unique .name ]
|> KeysSet.insertAll
[ { symbol = ">", name = "gt", kind = Infix }
, { symbol = "<", name = "lt", kind = Infix }
, { symbol = "==", name = "eq", kind = Infix }
, { symbol = "-", name = "negate", kind = Prefix }
]
infixOperators =
operators
|> KeysSet.when (.kind >> (==) Infix)
nameOfOperatorSymbol operatorSymbol =
operators
|> KeysSet.at .symbol operatorSymbolimport KeysSet.Uniqueness exposing (unique)
import KeysSet exposing (KeysSet)
type alias Account =
{ username : String
, email : String
, settings : Settings
}
type alias Model =
{ accounts : KeysSet Account
, currentUserName : String
}
initialModel =
{ accounts =
KeysSet.promising
[ unique .username, unique .email ]
}
update msg model =
case msg of
AccountSwitched username ->
{ model | currentUserName = username }
SettingsChanged updateSettings ->
{ model
| accounts =
model.accounts
|> KeysSet.update .username
model.currentUserName
updateSettings
}
Registered username email ->
if
model.accounts
|> KeysSet.any (.username >> (==) username)
then
-- username already taken
else if
model.accounts
|> KeysSet.any (.email >> (==) email)
then
-- email already taken
else
{ model
| accounts =
model.accounts
|> KeysSet.insert
{ username = username
, email = email
, settings = defaultSettings
}
}Β
answers =
KeysSet.promising [ unique .youSay ]
|> KeysSet.insertAll
[ { 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'"
}
]We will only ever lookup answers to what youSay
β use a Dict where it is more appropriate: Dicts are for one-way access.
translationsEnDe =
KeysSet.promising []
|> KeysSet.insertAll
[ { english = "elm", german = "Ulme" }
, { english = "git", german = "Schwachkopf" }
, { german = "RΓΌste", english = "elm" }
]A KeysSet is only effective when there is only one matching key.
Please take a look at elm-bidict instead!
partners =
KeysSet.promising
[ unique .partner, unique .partnerOfPartner ]
|> KeysSet.insertAll
[ { partner = "Ann", partnerOfPartner = "Alan" }
, { partner = "Alex", partnerOfPartner = "Alastair" }
, { partner = "Alan", partnerOfPartner = "Ann" }
-- wait, this is no duplicate and is inserted
]A KeysSet ony makes sense when the keys describe something different.