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 = addressRegionOptional `compose` (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 = addressRegionOptional `composeLens` (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 addressCasts Prism a b to Optional a b
string2IntPrism : Prism String Int
string2IntPrism =
Prism (String.toInt >> Result.toMaybe) toString
stringIntOptional: Optional String Int
stringIntOptional = fromPrism string2IntPrismaddressRegionOptional : 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