Simple bidirectional mapping between Strings and custom types; sorting and comparison
Initializes a Bimap. You will need to pass a pattern-matching function
with one parameter per variant, plus an extra parameter for the "value".
type Response
= Yes
| No
bimap : Bimap Response
bimap =
Bimap.init
(\yes no value ->
case value of
Yes ->
yes
No ->
no
)
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
|> Bimap.buildAdds a map between a String and a value of your custom type.
... -- a pattern-matching function goes before the variants
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
... -- a call to Bimap.build goes afterResolves your maps and pattern-matching function into
a Bimap that you can use.
... -- calls to Bimap.variant go before the call to Bimap.build
|> Bimap.buildReturns the mapped custom type value for a given String,
if the String has been mapped.
type Response
= Yes
| No
bimap : Bimap Response
bimap =
Bimap.init
(\yes no value ->
case value of
Yes ->
yes
No ->
no
)
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
|> Bimap.build
Bimap.fromString bimap "Yes" --> Yes
Bimap.fromString bimap "Maybe" --> NothingReturns the mapped String for a given custom type value.
type Response
= Yes
| No
bimap :
Bimap Response
bimap =
Bimap.init
(\yes no value ->
case value of
Yes ->
yes
No ->
no
)
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
|> Bimap.build
Bimap.toString No --> "No"Returns a List of tuples of all String keys and custom type values, in the same order that they were added.
type Response
= Yes
| No
bimap : Bimap Response
bimap =
Bimap.init
(\yes no value ->
case value of
Yes ->
yes
No ->
no
)
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
|> Bimap.build
Bimap.values bimap -- [ ( "Yes", Yes ), ( "No", No ) ]Returns the Int index of a custom type value. Values are indexed in the same order that they were added.
type Response
= Yes
| No
bimap : Bimap Response
bimap =
Bimap.init
(\yes no value ->
case value of
Yes ->
yes
No ->
no
)
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
|> Bimap.build
Bimap.toIndex bimap Yes -- 0
Bimap.toIndex bimap No -- 1Returns Just a for a custom type value at a given Int index, if one exists; Nothing otherwise. Values are indexed in the same order that they were added.
type Response
= Yes
| No
bimap : Bimap Response
bimap =
Bimap.init
(\yes no value ->
case value of
Yes ->
yes
No ->
no
)
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
|> Bimap.build
Bimap.fromIndex bimap 0 -- Just Yes
Bimap.fromIndex bimap 1 -- Just No
Bimap.fromIndex bimap 2 -- NothingCompares two custom type values in a Bimap. Very useful for sorting when partially applied and passed to List.sortWith.
type Response
= Yes
| No
bimap : Bimap Response
bimap =
Bimap.init
(\yes no value ->
case value of
Yes ->
yes
No ->
no
)
|> Bimap.variant "Yes" Yes
|> Bimap.variant "No" No
|> Bimap.build
sort : List Response -> List Response
sort =
List.sortWith (Bimap.compare bimap)
sort [ No, Yes ] -- [ Yes, No ]
A bidirectional map between a
Stringand a custom type.