Skip to content

Instantly share code, notes, and snippets.

View alphafoobar's full-sized avatar

James alphafoobar

View GitHub Profile
@alphafoobar
alphafoobar / weightedMedian.js
Created October 7, 2020 01:11 — forked from stekhn/weightedMedian.js
Weighted median in JavaScript
function weightedMedian(values, weights) {
var midpoint = 0.5 * sum(weights);
var cumulativeWeight = 0;
var belowMidpointIndex = 0;
var sortedValues = [];
var sortedWeights = [];
@alphafoobar
alphafoobar / healthcheck.sh
Created February 14, 2020 02:51
Healthcheck to emulate load-balancer behaviour
#!/bin/bash
for (( ; ; ))
do
sleep 10 && echo "$(date): $(wget -q -S -O - http://localhost:8079/actuator/health 2>&1 | grep HTTP)"
done
@alphafoobar
alphafoobar / index.html
Created September 21, 2018 11:19
React JS FX Tickers
<div id="app"></div>
@alphafoobar
alphafoobar / AkkaReactiveStream.scala
Created July 17, 2018 12:28
Reactive stream in Scala with Akka
factorials
.zipWith(Source(0 to 100))((num, idx) ⇒ s"$idx! = $num")
.throttle(1, 1.second)
.runForeach(println)
val o = Observable.interval(200 millis).take(5)
o.subscribe(n => println("n = " + n))
Observable.just(1, 2, 3, 4).reduce(_ + _)
@alphafoobar
alphafoobar / RxHelloWorld.java
Created July 11, 2018 12:00
RxJava HelloWorld
public class HelloWorld {
public static void main(String[] args) {
Flowable.just("Hello world").subscribe(System.out::println);
}
}
const { Observable, Subject, ReplaySubject, from, of, range } = require('rxjs');
const { map, filter, switchMap } = require('rxjs/operators');
range(1, 200)
.pipe(filter(x => x % 2 === 1), map(x => x + x))
.subscribe(x => console.log(x));
from rx import Observable
Observable.of("Alpha", "Beta", "Gamma", "Delta", "Epsilon") \
.map(lambda s: len(s) >= 5) \
.subscribe(on_next=lambda value: print("Received {0}".format(value)),
on_completed=lambda: print("Done!"),
on_error=lambda error: print("Error Occurred: {0}".format(error))))
val list = listOf("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
list.toObservable() // extension function for Iterables
.filter { it.length >= 5 }
.subscribeBy( // named arguments for lambda Subscribers
onNext = { println(it) },
onError = { it.printStackTrace() },
onComplete = { println("Done!") }
)
it, _ := iterable.New([]interface{}{1, 2, 3, 4, errors.New("bang"), 5})
source := observable.From(it)
sub := source.Subscribe(watcher)