This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
outer | |
.pipe(concatMap(createInner)) | |
.subscribe(x => console.log("[concatMap] outer:", x), undefined, () => | |
console.log("[concatMap] done!") | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function of<T>(...values: T[]): Observable<T> { | |
return new Observable(obs => { | |
for (const value of values) { | |
obs.next(value); | |
} | |
obs.complete(); | |
}); | |
} | |
// Logs: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const observable = new Observable(subscriber => { | |
subscriber.next(1); | |
subscriber.next(2); | |
subscriber.next(3); | |
setTimeout(() => { | |
subscriber.next(4); | |
subscriber.complete(); | |
}, 1000); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Observer<T> { | |
next(value: T): void; | |
error(err: any): void; | |
complete(): void; | |
} | |
interface Subscription { | |
unsubscribe(): void; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ipython-utils/researchenv/magics.py | |
from IPython.core.magic import Magics, magics_class, cell_magic, needs_local_scope | |
from IPython.core.magic_arguments import magic_arguments, argument, parse_argstring | |
from IPython.core.interactiveshell import InteractiveShell | |
from IPython.core.display import display | |
from google.cloud import bigquery | |
@magics_class | |
class ResearchEnvMagics(Magics): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ipython-utils/researchenv/__init__.py | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
from IPython.core.interactiveshell import InteractiveShell | |
def load_ipython_extension(ipython: InteractiveShell): | |
print('%matplotlib inline') |