Skip to content

Functional approach

Thang Chung edited this page Feb 24, 2018 · 7 revisions

Free monads

The presentation can be found at Purely Functional I/O in Scala.

Map (Select) & FlatMap (SelectMany)

public class PhoneNumber
{
    public string Number { get; set; }
}

public class Person
{
    public IEnumerable<PhoneNumber> PhoneNumbers { get; set; }
    public string Name { get; set; }
}

IEnumerable<Person> people = new List<Person>();

// Select gets a list of lists of phone numbers
IEnumerable<IEnumerable<PhoneNumber>> phoneLists = people.Select(p => p.PhoneNumbers);

// SelectMany flattens it to just a list of phone numbers.
IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers);

// And to include data from the parent in the result: 
// pass an expression to the second parameter (resultSelector) in the overload:
var directory = people
   .SelectMany(p => p.PhoneNumbers,
               (parent, child) => new { parent.Name, child.Number });

Reference at https://stackoverflow.com/questions/958949/difference-between-select-and-selectmany

Push vs Pull model

Subject Types in Reactive Programming

  • Subject: get every event that this Subject emits after you have subscribed (including complete)

  • BehaviorSubject: receives the last event that occurred before the subscription

  • ReplaySubject: receives all past events when it subscribes

  • AsyncSubject: wait for complete to emit the last event and then the complete event

Check out https://medium.com/front-end-hacking/creating-an-observable-with-angular-part-ii-the-4-different-types-3d8fd2835850.

Arity

Arity is the number of arguments that a function takes. We say that the arity of f(x: Int) is 1, or that is a unary function, or that is a function with one argument. Therefore, the arity of a function can be expressed with a number or the spring term. Unary, binary, ternary, etc... Are words that come from Latin, but mathematicians usually use Greek instead of Latin, so, usually, they interchange those words for the same ones coming from Greek. We can say as well that the arity of a function is monadic, dyadic, or triadic.

Reference to Functional Programming: Functions

flatMap vs compose

The difference is that compose() is a higher level abstraction: it operates on the entire stream, not individually emitted items. In more specific terms:

  • compose() is the only way to get the original Observable from the stream. Therefore, operators that affect the whole stream (like subscribeOn() and observeOn()) need to use compose(). In contrast, if you put subscribeOn()/observeOn() in flatMap(), it would only affect the Observable you create in flatMap() but not the rest of the stream.

  • compose() executes immediately when you create the Observable stream, as if you had written the operators inline. flatMap() executes when its onNext() is called, each time it is called. In other words, flatMap() transforms each item, whereas compose() transforms the whole stream.

  • flatMap() is necessarily less efficient because it has to create a new Observable every time onNext() is called. compose() operates on the stream as it is.

Reference to Don't break the chain: use RxJava's compose() operator

Use-cases to use Reactive Extension methods

Check out at https://github.com/kaushikgopal/RxJava-Android-Samples#14-pagination-with-rx-using-subjects

Clone this wiki locally