4 ms·
If you are interested in learning about Algebraic structures and group theory from a very practical standpoint I highly recommend https://github.com/fantasylan
by wrong_variable 10y ago
If you are interested in learning about Algebraic structures and group theory from a very practical standpoint I highly recommend
https://github.com/fantasyland/fantasy-land 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'});
- girvo 10y agoAnd check out ramda-fantasy while you're there!
- marcosdumay 10y agoYes, you can just use partial functions. 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.
- runT1ME 10y agowell, a 'reader' monad is a monad over a Function[A, B]. So yes, when you say 'the reader monad', what you (probably?) mean is the instance it operates on, which would be a function. 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'.
- wrong_variable 10y agoSo A reader Monad is similar to class inheritance. Where you provide a dependency ( like a inner class ). The resulting class can also have other variables and inputs. Is that a correct analogy ?
- greydius 10y agoI think a better analogy with OO languages would be a Function object for which you've provided one of the arguments in its constructor. Inheritance is more similar to monad transformers.
- ryani 10y agoReader fulfills a bunch of OO patterns. I like to think of it as "Environment", "Capability", "Configuration", etc., and is related in a way to Dependency Injection. 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.
- cantankerous 10y agoInterestingly enough "Environment" is another name for the Reader Monad.
- cantankerous 10y agoThe Reader Monad lets you abstract away local state. You're on the right track with partial functions, but using a monad is cleaner (at least in Haskell...and I'd guess anywhere else). Essentially using the monad instead will let you remove all of the redundant function application you'd have to do otherwise. If you've managed to abstract this step away into a library, take a look at your abstraction. It's probably equivalent to Reader.
- spion 10y agoThe reader monad is like read-only thread-local storage. (State or ST would be thread local storage). It provides a command to get the "current state" 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!
- wrong_variable 10y agoI do not understand how the reader monad is different than promise. 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.
- spion 10y agoThats precisely what it does, yes. Its different than a promise because the implementation is different (everything is done synchronously, and getState() returns an action object which then the implementation works on. The result of doing runWith() is also immediately available (not possible with async IO): 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.
- wrong_variable 10y ago
- JackMorgan 10y agoI wrote a simple post showing the 3 main ways to pass state. Check it out! http://deliberate-software.com/haskell-state/ http://deliberate-software.com/haskell-state/