A Optional is a weaker Lens and a weaker Prism
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)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)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 addressModifies 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 addressModifies 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.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 with Optional b d to form Optional for the pairs ( a, b ) ( c, d )
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.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.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 string2IntPrismCasts Lens a b to Optional a b where getOption will return always Just
addressRegionOptional : Optional Address String
addressRegionOptional =
let
getOption a =
a.region
set r a =
{ a | region = Just r }
in
Optional getOption set
In order to create Optional we need to suply 2 functions: set and getOption