# Flat-map nested optional

**URL:** <https://forum.canton.network/t/flat-map-nested-optional/3456>\
**Category:** App Development\
**Tags:** daml\
**Created:** [November 15, 2021, 4:22pm UTC](https://forum.canton.network/t/flat-map-nested-optional/3456 "2021-11-15T16:22:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Kapolnai](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/richard_kapolnai/32/89_2.png) [@Richard\_Kapolnai](https://forum.canton.network/u/Richard_Kapolnai)\
**Post date:** [November 15, 2021, 4:22pm UTC](https://forum.canton.network/t/flat-map-nested-optional/3456/1 "2021-11-15T16:22:43Z")

</div>

Can you simplify this code?

```haskell
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.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [November 15, 2021, 4:37pm UTC](https://forum.canton.network/t/flat-map-nested-optional/3456/2 "2021-11-15T16:37:25Z")

</div>

You can do this using monadic binds

```haskell
let eOpt = 
      a.bOpt >>= \b ->
      b.cOpt >>= \c ->
      c.dOpt >>= \d ->
      d.eOpt

```

Of course do notation looks a bit nicer here:

```haskell
let eOpt = do
      b <- a.bOpt
      c <- b.cOpt
      d <- c.dOpt
      d.eOpt

```

---

<div class="post-metadata">

**Author:** ![chris.norris](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/chris.norris/32/688_2.png) [@chris.norris](https://forum.canton.network/u/chris.norris)\
**Post date:** [November 23, 2021, 10:26am UTC](https://forum.canton.network/t/flat-map-nested-optional/3456/3 "2021-11-23T10:26:26Z")

</div>

Is 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?

```haskell
let eOpt = (.bOpt) >=> (.cOpt) >=> (.dOpt) >=> (.eOpt) $ a

```

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [November 23, 2021, 11:52am UTC](https://forum.canton.network/t/flat-map-nested-optional/3456/4 "2021-11-23T11:52:47Z")

</div>

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:

```haskell
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 ()

```
