jmpavlick/bimap/1.0.1

NOTE: the latest version is 1.2.0

Bimap

Simple bidirectional mapping between Strings and custom types.

type Bimap a

A bidirectional map between a String and a custom type.

init : match -> Builder match v

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.build
variant : String -> v -> Builder (String -> b) v -> Builder b v

Adds 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 after
build : Builder (a -> String) a -> Bimap a

Resolves 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.build
fromString : Bimap a -> String -> Maybe a

Returns 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" --> Nothing
toString : Bimap a -> a -> String

Returns 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"
values : Bimap a -> List ( String, a )

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 ) ]