by wrong_variable · 10y ago · 0 comments
https://github.com/fantasyland/fantasy-land
Also - does anyone show me a practical usecase of the reader monad ? cant you just use partial functions ?
if I am not mistaken isn't reader monad just :
foobar.config ({'name':'hello'});
foobar.run ({'age':'34'});
function f() {
var m = getState().chain(state => {
// ...
})
return m;
}
or with generators acting like do syntax replacement: var f = wrapped(function* () {
var state = yield getState();
// ...
return;
});
At the end you get an object that contains an unexecuted sequencing of computations that you can run with any state. For example you could run it from a HTTP route handler with the current user contained in the "state": var m = f();
m.runWith({currentUser: request.user})
and now you have access to the user without passing it around everywhere.All the other analogies in this thread are wrong in terms of what the real API looks like. The real API looks almost exactly like Promises, with `chain` instead of `then`.
This in turn also explains why monad transformers are so important. Having access to just the current user w/o passing it around isn't very useful if e.g. you can't do IO, but having both - now we're talking!
It seems the reader monad just delays execution of some code until you call 'runWith' and you can define state variables throughout your code before calling runWith.
var result = m.runWith(...)
Other than that its almost exactly the same. And so are other monads. The Array monad (that is, if JS arrays actually had that method, which they don't) var m = [1,2,3]
.chain(x => [x, x * 10])
.chain(y => y < 2 ? [] : [y])
result: [10,2,20,3,30]Same API (monad), totally different functionality.
so anything that is like this :
values = ['coffee','water']
AfterWebSocket = values.sendToWebSocket()
AfterWebSocket.compute (eachValue => x*2)
is that it ?
Basically, you have some computation/object that isn't complete without some "setup info" which, from the point of view of the code being written, is "just there". Like if you had a program that needed access to a particular interface, but you didn't want to manually pass it to every function that cares about it; instead you'd make an object with that interface as its state and write your program as methods on that object. That's just Reader in disguise.
In fact, C++ methods are basically in the Reader monad; they all have an implicit "this" argument which the compiler plumbs around for you.
You use the reader monad the thread the same input to multiple functions that may also take additional intput, and also have their own outputs.
You have to keep in mind that since Monads are so common and useful, many languages have syntactic support for them. C#'s LINQ query syntax, scala for expressions and haskell 'do' are all 'monadic comprehension'.
The resulting class can also have other variables and inputs.
Is that a correct analogy ?
Inheritance is more similar to monad transformers.
But partial functions are not always as clean as you think they are. When composing with pure functions for example, you'll save a lot of boilerplate lambdas if you use the monad.