Skip to content

Instantly share code, notes, and snippets.

View benjchristensen's full-sized avatar

Ben Christensen benjchristensen

View GitHub Profile
@benjchristensen
benjchristensen / RxScalaSamples.scala
Created February 8, 2013 17:19
Basic RxJava-Scala examples
@Test def runTests() {
// using closure
Observable.toObservable("1", "2", "3")
.take(2)
.subscribe((callback: String) => {
println(callback)
})
// using Map of closures
Observable.toObservable("1", "2", "3")
@benjchristensen
benjchristensen / VideoExample-snippet.groovy
Last active December 11, 2015 23:48
Example of using RxJava in Groovy to compose nested asynchronous calls.
/**
* Demonstrate how Rx is used to compose Observables together
* such as how a web service would to generate a JSON response.
*
* The simulated methods for the metadata represent different
* services that are often backed by network calls.
*
* This will return a sequence of dictionaries such as this:
*
* [id:1000, title:video-1000-title, length:5428, bookmark:0,
@benjchristensen
benjchristensen / VideoExample.groovy
Last active February 14, 2016 11:20
Example of using RxJava in Groovy to compose nested asynchronous calls.
package rx.examples.groovy;
import rx.observables.Observable
import rx.observables.Observer;
import rx.observables.Subscription;
import rx.util.functions.Func1;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@benjchristensen
benjchristensen / CallbackB.java
Last active January 16, 2024 15:09
CallbackB.java Example of using Callbacks for nested calls showing the incidental complexity that results and how eventually the asynchronous paths need to be synchronized together.
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
public class CallbackB {
/**
@benjchristensen
benjchristensen / simpleRxComposition.groovy
Last active December 11, 2015 23:28
Simple example of applying Rx operators to an asynchronous Observable sequence.
/**
* Asynchronously calls 'customObservableNonBlocking' and defines
* a chain of operators to apply to the callback sequence.
*/
def simpleComposition() {
// fetch an asynchronous Observable<String>
// that emits 75 Strings of 'anotherValue_#'
customObservableNonBlocking()
// skip the first 10
.skip(10)
@benjchristensen
benjchristensen / simpleRxComposition.clj
Last active December 11, 2015 23:28
Simple example of applying Rx operators to an asynchronous Observable sequence.
(defn simpleComposition []
"Asynchronously calls 'customObservableNonBlocking' and defines a
chain of operators to apply to the callback sequence."
(->
; fetch an asynchronous Observable<String>
; that emits 75 Strings of 'anotherValue_#'
(customObservableNonBlocking)
; skip the first 10
(.skip 10)
; take the next 5
@benjchristensen
benjchristensen / ObservableMethod.groovy
Created January 30, 2013 18:39
Observable Method that optionally uses a thread to fetch data if not available in memory. Either way the response type is an Observable callback that can be composed using Rx operators.
/**
* Non-blocking method that immediately returns the value
* if available or uses a thread to fetch the value and
* callback via `onNext()` when done.
*/
def Observable<T> getData(int id) {
if(availableInMemory) {
// if data available return immediately with data
return Observable.create({ observer ->
observer.onNext(valueFromMemory);
@benjchristensen
benjchristensen / FuturesB-snippet.java
Last active December 11, 2015 22:39
Snippet of FuturesB.java Example of using Futures for nested calls showing how it blocks inefficiently.
ExecutorService executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
// get f3 with dependent result from f1
Future<String> f1 = executor.submit(new CallToRemoteServiceA());
Future<String> f3 = executor.submit(new CallToRemoteServiceC(f1.get()));
/* The work below can not proceed until f1.get()
completes even though there is no dependency */
// also get f4/f5 after dependency f2 completes
@benjchristensen
benjchristensen / FuturesB.java
Last active December 19, 2024 23:11
FuturesB.java Example of using Futures for nested calls showing how it blocks inefficiently.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@benjchristensen
benjchristensen / FuturesA-snippet.java
Created January 30, 2013 05:54
Snippet of FuturesA.java (https://gist.github.com/4670979) Simple example of using Futures.
ExecutorService executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
Future<String> f1 = executor.submit(new CallToRemoteServiceA());
Future<String> f2 = executor.submit(new CallToRemoteServiceB());
System.out.println(f1.get() + " - " + f2.get());