Skip to content

Instantly share code, notes, and snippets.

View UserGalileo's full-sized avatar

Michele Stieven UserGalileo

View GitHub Profile
@UserGalileo
UserGalileo / fine-day1-academy-angular2-sporco.ts
Created May 6, 2022 20:35
Fine Giorno 1 - Academy Angular 2 (sporco)
import { Component, ViewChild } from '@angular/core';
import {
AbstractControl,
FormArray,
FormBuilder,
FormControl,
FormGroup,
FormGroupDirective, NgForm, ValidationErrors, ValidatorFn,
Validators
} from '@angular/forms';
@UserGalileo
UserGalileo / retry.interceptor.ts
Created November 14, 2021 19:32
Retry Interceptor
import {HttpContextToken, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs';
import {retry, tap} from 'rxjs/operators';
export const RETRY_COUNT = new HttpContextToken(() => 3);
export const ERROR_COUNT = new HttpContextToken(() => 0);
export class RetryInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
@UserGalileo
UserGalileo / cache.interceptor.ts
Created November 14, 2021 19:27
Cache Interceptor
import { Injectable } from '@angular/core';
import {
HttpRequest, HttpResponse,
HttpInterceptor, HttpHandler
} from '@angular/common/http';
import { of } from 'rxjs';
import { startWith, tap } from 'rxjs/operators';
@Injectable()
@UserGalileo
UserGalileo / angular-injectiontoken.ts
Created August 24, 2021 23:03
Angular InjectionToken
// Injection Token
const APP_CONFIG = new InjectionToken<AppConfig>('Your description!');
const appConfig: AppConfig = { ... }
// The provider's configuration.
// At this point, whoever injects APP_CONFIG gets this object.
// But APP_CONFIG is not a class! How do we grab it?...
@NgModule({
...,
providers: [{ provide: APP_CONFIG, useValue: appConfig }]
@UserGalileo
UserGalileo / functors_monoid.js
Created April 14, 2020 17:55
Functors Monoid
// a -> a (could return whatever)
const f = n => n + 1;
// a -> M a
const g = x => Identity(x);
// Composition gives us Identity(2)
g(1).map(f)
@UserGalileo
UserGalileo / monad_monoid_associativity.js
Last active April 14, 2020 17:46
Monad Monoid Associativity
// Associativity
const associativity1 = op(op(f,g), h);
const associativity2 = op(f, op(g,h));
// ...are the same, too!
inspect(associativity1('a')); // Identity(a123)
inspect(associativity2('a')); // Identity(a123)
/**
* We can say that:
@UserGalileo
UserGalileo / monad_identity_monoid.js
Last active April 14, 2020 18:24
Monad Identity Monoid
// Here's our unit function
const id = x => Identity(x);
// Left & Right Identity...
const identity1 = op(f, id);
const identity2 = op(id, f);
// ...are the same!
inspect(identity1('i')); // Identity(i1)
inspect(identity2('i')); // Identity(i1)
@UserGalileo
UserGalileo / monoid_falling_into_the_same_set.js
Created April 14, 2020 17:37
Monoid falling into the same set
const f = x => Identity(x + 1); // a -> M a
const g = x => Identity(x + 2); // a -> M a
const h = x => Identity(x + 3); // a -> M a
const f_g = op(f,g); // a -> M a
// We call it with an "a", we get back a "M a"
const Ma = f_g('whatever');
// Identity(whatever12)
@UserGalileo
UserGalileo / extracting_composition.js
Last active April 14, 2020 19:07
Extracting Composition
// Pointfree utility for "chain"
const chain = fn => a => a.chain(fn);
// generic composition operation
const op = (f1, f2) => compose(chain(f2), f1)
// just to demonstrate, here's the non-generic way of composing our two functions
const op2 = compose(chain(g), f);
// Now we can compose our functions!
@UserGalileo
UserGalileo / composing_functions_yielding_monadic_values.js
Created April 14, 2020 17:25
Composing Functions yielding monadic values
// These are our new elements
const f = x => Identity(x + 1);
const g = x => Identity(x + 2);
const h = x => Identity(x + 3);
// For now, let's just compose 2 of them.
const composed = f('a').chain(g);
// Identity(a12)
inspect(composed);