Flat-map nested optional
App Development4 posts385 views7 likesLast activity Nov 2021
RI
Richard_KapolnaiOP
Nov 2021Can 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.eOpt
I tried fmap and friens, but it keeps the nested optional type.
CO
cocreature
Nov 2021You can do this using monadic binds
let eOpt =
a.bOpt >>= \b ->
b.cOpt >>= \c ->
c.dOpt >>= \d ->
d.eOpt
Of course do notation looks a bit nicer here:
let eOpt = do
b <- a.bOpt
c <- b.cOpt
d <- c.dOpt
d.eOpt
CH
chris.norris
Nov 2021Is this a case where (Kleisli) composition of the arrows with >=> (from DA.Action) might make things a bit cleaner, or is it considered that this degrades readability?
let eOpt = (.bOpt) >=> (.cOpt) >=> (.dOpt) >=> (.eOpt) $ a
GA
Gary_Verhaegen
Nov 2021That 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 ()