arturopala/elm-monocle/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

Monocle.Optional

A Optional is a weaker Lens and a weaker Prism

Definition

type alias Optional a b =
{ getOption : a -> Maybe b
, set : b -> a -> a
}

In order to create Optional we need to suply 2 functions: set and getOption

Derived methods

compose : Optional a b -> Optional b c -> Optional a c

Composes Optional a b with Optional b c and returns Optional a c

string2IntPrism : Prism String Int
string2IntPrism = Prism (String.toInt >> Result.toMaybe) toString


addressRegionIntOptional: Optional Address Int
addressRegionIntOptional = addressRegionOptional `compose` (fromPrism string2IntPrism)
composeLens : Optional a b -> Lens b c -> Optional a c

Composes Optional a b with Lens b c and returns Optional a c

string2CharListIso : Iso String (List Char)
string2CharListIso = Iso String.toList String.fromList


addressRegionListCharOptional: Optional Address (List Char)
addressRegionListCharOptional = addressRegionOptional `composeLens` (fromIso string2CharListIso)
modifyOption : Optional a b -> (b -> b) -> a -> Maybe a

Modifies given function (b -> b) to be (a -> Maybe a) using Optional a b

    modifyRegion: String -> String
    modifyRegion region = String.reverse region


    modifyAddressRegion: Address -> Maybe Address
    modifyAddressRegion address = Optional.modifyOption addressRegionOptional modifyRegion address
modify : Optional a b -> (b -> b) -> a -> a

Modifies given function (b -> b) to be (a -> a) using Optional a b

    modifyRegion: String -> String
    modifyRegion region = String.reverse region


    modifyAddressRegion: Address -> Address
    modifyAddressRegion address = Optional.modify addressRegionOptional modifyRegion address
zip : Optional a c -> Optional b d -> Optional ( a, b ) ( c, d )

Zip Optional a c with Optional b d to form Optional for the pairs ( a, b ) ( c, d )

Conversion

fromPrism : Prism a b -> Optional a b

Casts Prism a b to Optional a b

string2IntPrism : Prism String Int
string2IntPrism =
    Prism (String.toInt >> Result.toMaybe) toString


stringIntOptional: Optional String Int
stringIntOptional = fromPrism string2IntPrism
fromLens : Lens a b -> Optional a b

Casts Lens a b to Optional a b where getOption will return always Just

Example

addressRegionOptional : Optional Address String
addressRegionOptional =
    let
        getOption a = a.region


        set r a = { a | region = Just r }
    in
        Optional getOption set