I had intended to spend this episode on the history of the relationship between SelectMany
and the LINQ query expression syntax, but before I do that I want to briefly address a point raised in the comments of the previous episode.
We know that the key bits of the monad pattern are (1) the ability to construct a monad value that “wraps” an underlying value, and (2) the ability to apply a function from A
to M<R>
to a value of type M<A>
. The first operation is traditionally called unit
. The second operation is traditionally called bind
, but is called SelectMany
in LINQ.
We saw how you could build Select
out of only SelectMany
and a helper method that effectively just calls unit
— and indeed, you can build a Select
for any monad because all you need is the unit
and bind
; this operation is traditionally called fmap
.
I also showed how you could build Where
— an inefficient Where
to be sure — out of SelectMany
and a helper method, but reader “Medo” reasonably pointed out that there is no general monadic where
because my implementation actually required three operations: unit
, bind
and make an empty sequence.
This criticism is entirely justified; though every monad has an analog of the sequence monad’s Select
and SelectMany
, not every monad has an analog of Where
.