This is a methodological question with two parts:
How to use monadic programming in Mathematica?
Why use monadic programming in Mathematica?
In my opinion the questions are inter-related -- we cannot answer one of them without answering the other.
It would be nice to get concrete monads implementations and/or reasons to use monads, but I am also interested in personal opinions and experiences. (Like this one.)
Many people are puzzled by monads or wonder why others make big deal about them. This question is looking for relevant answers for those concerns.
(Also, in the MSE monad discussions I have seen people say they want to use or implement monads in Mathematica but not why.)
Simply put, monads provide a generalized interface to sequential computation. Hence monads are a way to impose or enforce a certain regular behavior in building computations.
Monads (in programming) are defined in this dedicated Wikipedia article.
The most detailed of the definitions given in the linked Wikipedia article is based on the operators 'return' and 'bind' -- let us call it the "Haskell definition". There is also an alternative definition (outlined in the linked Wikipedia article) that uses 'fmap' and 'join' -- let us call it the "Scala definition".
The Haskell definition -- Mathematica-localized
Here are operators for a monad associated with a certain symbol
- monad unit function ("return" in Haskell notation) is
Unit[x_] := M[x];
- monad bind function (">>=" in Haskell notation) is a rule like
Bind[M[x_], f_] := f[x]with
(See the monad Maybe for an example.)
Here is an illustration formula showing a monad pipeline:
From the definition and formula it should be clear that if for the result
Bind the test
True then the result is ready to be fed to the next binding operation in monad's pipeline.
The monad laws
The monad laws definitions are taken from https://wiki.haskell.org/Monad_laws. In the monad laws given below the symbol "⟹" is for monad's binding operation and ↦ is for a function in anonymous form.
Here is a table with the laws:
Guide to answers
Please consider the following questions while giving an answer.
Why do you want to use monads?
- Because of FP studies, coming to Mathematica from another FP language, architectural concerns, convenience, etc.
How often do you use monads?
- Even if you (dis-)like them?
When do you use monads?
At exploratory phases of programming projects.
For code reuse purposes.
To impose rigid structures of a program design.
For readability of code.
For highly specialized, well understood tasks.
Why you do not use monads in Mathematica?
- But generally would...
What is the simplest implementation (in Mathematica) that you use or programmed?
Does the implementation in your answer use the Haskell or the Scala definition?
- Why the used definition was chosen?
Missing+1now head Plus rather than the more desirable
Missing[Failed,Plus]. Even if
DeleteMissingworks w/ levelspec, often it's a question of filtering levels that stack up in complex ways. // Not so much Modad as Duad etc >> "Pythagoras: His Life and Teachings") $\endgroup$