Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / multiple-subscriptions.ts
Last active March 21, 2020 20:35
Manually unsubscribing from multiple observables
const pokemonSubscription = pokemon$.subscribe(pokemon => {
// Do something with pokemon
});
const trainerSubscription = trainer$.subscribe(trainer => {
// Do something with trainer
});
const numberSubscription = number$.subscribe(number => {
// Do something with number
@NyaGarcia
NyaGarcia / subscriptions-array.ts
Created February 29, 2020 12:23
Storing subscriptions in an array for easier tracking
const trainerSubscription = trainer$.subscribe(trainer => {
// Do something with trainer
});
const pokemonSubscription = pokemon$.subscribe(pokemon => {
// Do something with pokemon
});
const numberSubscription = number$.subscribe(number => {
// Do something with number
@NyaGarcia
NyaGarcia / destroy-subject.ts
Last active March 21, 2020 21:21
Using takeUntil to force observables to complete
const stop$ = new Subject<void>();
trainer$
.pipe(takeUntil(stop$)).subscribe(trainer => {
// Do something with trainer
});
pokemon$
.pipe(takeUntil(stop$)).subscribe(pokemon => {
// Do something with pokemon
@NyaGarcia
NyaGarcia / cold-observable.ts
Last active March 23, 2020 20:14
Duplicating http request with cold observables
pokemon$ = http.get(/* make an http request here*/);
/*Every time we subscribe to pokemon$, an http request will be made*/
pokemon$
.pipe(
flatMap(pokemon => pokemon),
filter(({ type }) => type === "Fire")
)
.subscribe(pokemon => {
// Do something with pokemon
@NyaGarcia
NyaGarcia / logic-in-subscribe.ts
Last active May 7, 2021 17:55
Misuse of subscribe by placing logic inside it
pokemon$.subscribe((pokemon: Pokemon) => {
if (pokemon.type === "Water") {
return;
}
const pokemonStats = getStats(pokemon);
logStats(pokemonStats);
saveToPokedex(pokemonStats);
});
@NyaGarcia
NyaGarcia / pipeable-operators.ts
Last active May 7, 2021 18:01
Performing logic with pipeable operators
pokemon$
.pipe(
filter(({ type }) => type !== "Water"),
map(pokemon => getStats(pokemon)),
tap(stats => logStats(stats))
)
.subscribe(stats => saveToPokedex(stats));
@NyaGarcia
NyaGarcia / share.ts
Last active March 23, 2020 20:15
Using the share operator to turn cold Observables hot
pokemon$ = http.get(/* make an http request here*/).pipe(share());
/*The pokemon$ Observable is now hot, we won't have multiple http requests*/
pokemon$
.pipe(
flatMap(pokemon => pokemon),
filter(({ type }) => type === "Fire")
)
.subscribe(pokemon => {
// Do something with pokemon
@NyaGarcia
NyaGarcia / loaddataset.r
Created March 18, 2020 15:15
Loading a built-in R dataset
> data("mtcars")
> mtcars
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
@NyaGarcia
NyaGarcia / gearsTable.r
Created March 18, 2020 15:18
Creating a table to show the gears each car in the mtcars dataset has
> table(mtcars$gear)
3 4 5
15 12 5
@NyaGarcia
NyaGarcia / cylinders.r
Last active March 18, 2020 15:39
Showing the number of cylinders for each car in the mtcars dataset
> mtcars$cyl
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
> table(mtcars$cyl)
4 6 8
11 7 14