Skip to content

Instantly share code, notes, and snippets.

Avatar

Travis Kaufman traviskaufman

View GitHub Profile
@traviskaufman
traviskaufman / scrape-timestamps.js
Created Oct 31, 2019
Timestamp scraping script used for r/dataisbeautiful October 2019 Challenge
View scrape-timestamps.js
const fs = require('fs');
const fetch = require('node-fetch');
const cheerio = require('cheerio');
main().catch(err => console.error(err));
async function main() {
const SCARE_RE = /^((\d{1,2}):(\d{1,2})(?::(\d{1,2}))?).*(.+)$/;
const $ = cheerio.load(fs.readFileSync('./notebooks/movielist.html', 'utf8'));
const $movieNames = $('td.column-1 > a');
@traviskaufman
traviskaufman / 03-02-demystifying-rxjs-schedulers.ts
Created Oct 28, 2019
Demystifying RxJS, Part III: Concrete Schedulers
View 03-02-demystifying-rxjs-schedulers.ts
const syncScheduler: Scheduler = {
schedule(work) {
work();
}
};
const asyncScheduler: Scheduler = {
schedule(work) {
setTimeout(work, 0);
}
@traviskaufman
traviskaufman / 03-01-demystifying-rxjs-scheduler-interface.ts
Created Oct 28, 2019
Demystifying RxJS, Part III: Scheduler Interface
View 03-01-demystifying-rxjs-scheduler-interface.ts
interface Scheduler {
schedule(work: Function): void;
}
@traviskaufman
traviskaufman / 02-08-demystifying-rxjs-concatMap-test.ts
Created Oct 25, 2019
Demystifying RxJS, Part II: concatMap test
View 02-08-demystifying-rxjs-concatMap-test.ts
outer
.pipe(concatMap(createInner))
.subscribe(x => console.log("[concatMap] outer:", x), undefined, () =>
console.log("[concatMap] done!")
);
@traviskaufman
traviskaufman / 02-07-demystifying-rxjs-concatMap.ts
Last active Nov 27, 2019
Demystifying RxJS, Part II: concatMap
View 02-07-demystifying-rxjs-concatMap.ts
function concatMap<T, R>(
project: (value: T, index: number) => Observable<R>
): OperatorFunction<T, R> {
let currentIndex = 0;
const buffer: Array<Observable<R>> = [];
const subscribeTo = (
projected: Observable<R>,
obs: Observer<R>,
subscriptions: Set<Subscription>
@traviskaufman
traviskaufman / 02-06-demystifying-rxjs-mergemap-test.ts
Last active Oct 25, 2019
Demystifying RxJS, Part II: MergeMap test
View 02-06-demystifying-rxjs-mergemap-test.ts
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function createInner(x: number) {
return new Observable(obs => {
(async () => {
for (let i = 0; i < 3; i++) {
if (i > 0) await sleep(500);
obs.next(10 * x);
@traviskaufman
traviskaufman / 02-05-demystifying-rxjs-mergemap.ts
Last active Nov 26, 2019
Demystifying RxJS, Part II: MergeMap
View 02-05-demystifying-rxjs-mergemap.ts
function mergeMap<T, R>(
project: (value: T, index: number) => Observable<R>
): OperatorFunction<T, R> {
let currentIndex = 0;
return source =>
new Observable(obs => {
const subscriptions = new Set<Subscription>();
const sub = source.subscribe(
x => {
const projected = project(x, currentIndex++);
@traviskaufman
traviskaufman / 02-04-demystifying-rxjs-custom-op.ts
Created Oct 25, 2019
Demystifying RxJS, Part II: Custom operators
View 02-04-demystifying-rxjs-custom-op.ts
function computeSquaredSum(): OperatorFunction<number, number> {
return source =>
source.pipe(
map(n => n * n),
reduce((s, n) => s + n, 0),
);
}
oneThroughTen
.pipe(computeSquaredSum())
.subscribe(x => console.log("Squared sum w/ custom op =", x));
@traviskaufman
traviskaufman / 02-03-demystifying-rxjs-sqsum-test-1.ts
Created Oct 25, 2019
Demystifying RxJS, Part II: Sum of Squares with pipe() / operators
View 02-03-demystifying-rxjs-sqsum-test-1.ts
const oneThroughTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
oneThroughTen
.pipe(
map(n => n * n),
reduce((s, n) => s + n, 0)
)
.subscribe(sqSum => console.log("Squared sum =", sqSum));
@traviskaufman
traviskaufman / 05-demystifying-rxjs-fromEvent.ts
Created Oct 18, 2019
Demystifying RxJS, Part I: fromEvent()
View 05-demystifying-rxjs-fromEvent.ts
function fromEvent<T extends Event = Event>(
target: EventTarget,
eventName: string
): Observable<T> {
return new Observable(obs => {
const listener: EventListener = (evt: T) => {
obs.next(evt);
};
target.addEventListener(eventName, listener);
return () => target.removeEventListener(eventName, listener);