Hacker News new | ask | show | jobs
by muxator 1061 days ago
Humble question:

> Maybe (optionals) - chain steps and abort if any step is empty.

Could this be (remotely) akin to the following shell pattern?

    set -o pipefail
    cmd1 | cmd2 | ...
This would either return the result of a successful execution of tje whole pipe, or return at the first command erroring out.

Does it make any sense?

1 comments

Yes that’s exactly what the Maybe monad does! It would look like this in Haskell code

    doStuff :: Maybe X
    doStuff = do
      r1 <- cmd1
      r2 <- cmd2 r1
      …
      return rX
Which is syntactic sugar for:

    doStuff = cmd1 >>= cmd2 >>= …
A sometimes important difference is that every process in the pipeline is spawned at the start, and may operate on partial input. A function returning Maybe needs to complete before we know whether it returns Just or Nothing, and we can't start the next function until we have the Just in hand, as that's the input to the function.