Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean that RACStream represents a monad?

From the doc RACStream represents a "Monad"? Could somebody explain what this specifically means in the context of RACStream. I looked up the functional meaning on wiki but I am having difficulties seeing how it benefits Reactive-Cocoa and why this pattern was chosen?

like image 796
jack Avatar asked Mar 09 '14 16:03

jack


1 Answers

In the context of Objective-C, the fact that RACStream is a monad basically means it's following a pattern. In this case, RACStream has the -bind: method, which is a more "raw" version of the more frequently used -flattenMap:, and these methods are essentially what makes RACStream a monad.

(Have a look at the documentation for -flattenMap: for more information on its use.)

Given a block B which takes a value and returns a RACStream (i.e. a block that maps values to streams), and given a stream A, calling [A flattenMap:B] will result in an "aggregate" stream that includes all the values from the collective set of streams returned by the block B (which is invoked for each value in A). (EDIT: This narrow explanation doesn't describe the timing of the values on the resulting stream, nor about error propagation. If you have questions about these, I'm happy to answer them.)

A couple examples can be found in ReactiveCocoa's Basic Operators documentation.

As for the benefits, I think the fact that streams are monads is mostly only beneficial to those who have experience with monads. On the flip side, monads are hardly mentioned in the documentation and more importantly, ReactiveCocoa requires no knowledge of monads at all, so there's no downside to not being familiar monads.

like image 193
Dave Lee Avatar answered Oct 21 '22 09:10

Dave Lee