arturopala / elm-monocle / 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 supply 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 =
    compose addressRegionOptional (fromPrism string2IntPrism)

composeLens : Optional a b -> Monocle.Lens.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 =
    composeLens addressRegionOptional (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

modify2 : Optional a b -> Optional c d -> (( b, d ) -> ( b, d )) -> ( a, c ) -> ( a, c )

Modifies given function (b,d) -> (b,d) to be (a,c) -> (a,c) using Optional a b and Optional c d

Function will be invoked ONLY when for ALL arguments `a` and `c` method `Optional.getOption` returns some value.

modify3 : Optional a b -> Optional c d -> Optional e f -> (( b, d, f ) -> ( b, d, f )) -> ( a, c, e ) -> ( a, c, e )

Modifies given function ( b, d, f ) -> ( b, d, f ) to be ( a, c, e ) -> ( a, c, e ) using Optional a b and Optional c d and Optional e f

Function will be invoked ONLY when for ALL arguments `a`,`c`,`f` method `Optional.getOption` returns some value.

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 )

tuple : Optional a b -> Optional a c -> Optional a ( b, c )

Tuple Optional a b with Optional a c and returns Optional a (b,c)

Method `Optional.getOption` returns pair of values only when both given optionals return some value.

tuple3 : Optional a b -> Optional a c -> Optional a d -> Optional a ( b, c, d )

Tuple Optional a b with Optional a c with Optional a d and returns Optional a (b,c,d)

Method `Optional.getOption` returns triple of values only when all given optionals return some value.

Conversion

fromPrism : Monocle.Prism.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 : Monocle.Lens.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