Flat-map nested optional
- Richard_KapolnaiOP 1
Can you simplify this code?
let eOpt = case a.bOpt of None -> None Some b -> case b.cOpt of None -> None Some c -> case c.dOpt of None -> None Some d -> d.eOptI tried
fmapand friens, but it keeps the nested optional type. - cocreature 4
You can do this using monadic binds
let eOpt = a.bOpt >>= \b -> b.cOpt >>= \c -> c.dOpt >>= \d -> d.eOptOf course do notation looks a bit nicer here:
let eOpt = do b <- a.bOpt c <- b.cOpt d <- c.dOpt d.eOpt - chris.norris 1
Is this a case where (Kleisli) composition of the arrows with
>=>(fromDA.Action) might make things a bit cleaner, or is it considered that this degrades readability?let eOpt = (.bOpt) >=> (.cOpt) >=> (.dOpt) >=> (.eOpt) $ a - Gary_Verhaegen 1
That will highly depend on the audience for the code, so “check with your team”, I suppose.
Personally, if we’re going down that path, I’d prefer for the entire line to read “from left to right”, something like:
module Main where import Daml.Script data A = A with b : Optional B data B = B with c : Optional C data C = C with d : Optional D data D = D with e : Optional E data E = E Int (|>) : a -> (a -> b) -> b infix 0 |> (|>) arg f = f arg (>=>) : Action m => (a -> m b) -> (b -> m c) -> (a -> m c) (f >=> g) x = f x >>= g setup : Script () setup = script do let a = A None let e = a |> (.b) >=> (.c) >=> (.d) >=> (.e) return ()