Skip to content

Instantly share code, notes, and snippets.

View martinomburajr's full-sized avatar
🦆
Relentlessly pursuing those carbs

Martin Ombura Jr. martinomburajr

🦆
Relentlessly pursuing those carbs
View GitHub Profile
@martinomburajr
martinomburajr / episode1.ts
Created September 21, 2017 12:23
RxJS Operator Series
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import {Observable} from 'rxjs/Rx'
import * as firebase from 'firebase/app';
import 'rxjs/add/operator/map'
@Component({
selector: 'app-episode1',
templateUrl: './episode1.component.html',
styleUrls: ['./episode1.component.scss']
// Example 1: Simple Reduce
console.log('Reduce');
//The Observable consumes an array of numbers and spews each number out individually to the reduce operator.
Observable.from([1, 2, 3, 4, 5])
//reduce accepts two parameters (accumulator,current,index) and an optional seed, which is just an initial value
//set to 0 in this case.
//the lambda function adds the current value to the accumulator and repeats for all numbers in the array
.reduce((accumulator, value, index) => (accumulator + value), 0)
.subscribe(response => {
console.log(response); //logs the response
/******************** DELAY **************************/
Observable.just("Hello Computation Scheduler")
.delay(1000, TimeUnit.MILLISECONDS)
.subscribe(stringObserver);
//Output
// E/RxJava2:Schedulers:: onSubscribe | main
// E/RxJava2:Schedulers:: onNext | Hello Computation Scheduler
// E/RxJava2:Schedulers:: onNext | RxComputationThreadPool-1
// E/RxJava2:Schedulers:: onComplete | RxComputationThreadPool-1
/******************** TIMER ***************************/
Observable.timer(2000L, TimeUnit.MILLISECONDS)
.subscribe(longObserver);
//Output
// E/RxJava2:Schedulers:: onSubscribe | main
// E/RxJava2:Schedulers:: onNext | 0
// E/RxJava2:Schedulers:: onNext | RxComputationThreadPool-2
// E/RxJava2:Schedulers:: onComplete | RxComputationThreadPool-2
/******************** INTERVAL ***************************/
Observable.interval(1000,2000, TimeUnit.MILLISECONDS)
.subscribe(longObserver);
//Output
// E/RxJava2:Schedulers:: onSubscribe | main
// E/RxJava2:Schedulers:: onNext | 0
// E/RxJava2:Schedulers:: onNext | RxComputationThreadPool-1
// E/RxJava2:Schedulers:: onNext | 1
// E/RxJava2:Schedulers:: onNext | RxComputationThreadPool-1
@martinomburajr
martinomburajr / medium-computation-scheduler:rxjava2_observers_for_operator.java
Created November 24, 2017 11:27
These are the observers used within the Observables that showcase the defualt use of the computation scheduler.
/************************** OBSERVERS ***************************************/
private Observer<String> stringObserver = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("RxJava2:Schedulers: " , "onSubscribe | " + Thread.currentThread().getName());
}
@Override
public void onNext(String message) {
@martinomburajr
martinomburajr / medium-computation-scheduler:rxjava2_observers_for_operator.java
Created November 24, 2017 11:27
These are the observers used within the Observables that showcase the defualt use of the computation scheduler.
/************************** OBSERVERS ***************************************/
private Observer<String> stringObserver = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("RxJava2:Schedulers: " , "onSubscribe | " + Thread.currentThread().getName());
}
@Override
public void onNext(String message) {
@martinomburajr
martinomburajr / medium-computation-scheduler:rxjava2_observers_for_operator.java
Created November 24, 2017 11:27
These are the observers used within the Observables that showcase the defualt use of the computation scheduler.
/************************** OBSERVERS ***************************************/
private Observer<String> stringObserver = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("RxJava2:Schedulers: " , "onSubscribe | " + Thread.currentThread().getName());
}
@Override
public void onNext(String message) {
static int Main(string[] args)
{
return 0;
}
int main() {
return 0;
}
//or
int main(int argc, char* argv[]) {
return 0;
}