-
Notifications
You must be signed in to change notification settings - Fork 123
Functional approach
The presentation can be found at Purely Functional I/O in Scala.
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
- 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
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
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
Check out at https://github.com/kaushikgopal/RxJava-Android-Samples#14-pagination-with-rx-using-subjects




