Question
Please help confirm or correct the understandings of what Monad is and its traits.
As Data Type
In my understanding, a Monad is:
- a container which can accommodate any type T and
- provides a bind interface that allows its client to apply a flat-map function and
- projects its content into another Monad of any type T'.
There need to be a return or unit interface to create a Monad of type T.
unit:= T -> M[T]
In Scala, List() or Set() are the examples of return interface, and any Scala sequence types (Array, List, Map, String) are Monad which provide flatMap interface which is bind.
Are these correct?
As Design Pattern
Software engineering provides ways to manage complexity or to structure software, such as Structured Programming without goto, UNIX pipe to pipeline transformation, Object Oriented to encapsulate data & control access, etc.
Is Monad a design pattern providing a way to structure a computation as a chain?
In other systems
UNIX commands
I suppose UNIX commands e.g. cat, grep are functions that can be chained but it does not mean they are Monad, and they are not Monad because they do not have return/unit nor they are not data type. Or is it still regarded e.g. IO Monad as in Monadic i/o and UNIX shell programming?
Python
I believe there is no bind or Scala flatMap equivalent in Python out of the box. Can I say Python does not have Monad feature out of the box?
bind
appliesf (A -> M[B])
toA
value(s) "in" theM[A]
value and produces one combinedM[B]
value. if we justmap
thatf
we end up withM[M[B]]
, but monadic bind "splices" thoseM[B]
s inside the one combinedM[B]
:twice_each [x,y,z] = [x,x] ++ [y,y] ++ [z,z]
ortwice_each xs: for x in xs: for a in [x,x]: yield a
. – Will Ness