This file contains hidden or 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
import { Component, ViewChild } from '@angular/core'; | |
import { | |
AbstractControl, | |
FormArray, | |
FormBuilder, | |
FormControl, | |
FormGroup, | |
FormGroupDirective, NgForm, ValidationErrors, ValidatorFn, | |
Validators | |
} from '@angular/forms'; |
This file contains hidden or 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
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>> { |
This file contains hidden or 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
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() |
This file contains hidden or 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
// 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 }] |
This file contains hidden or 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
// 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) | |
This file contains hidden or 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
// 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: |
This file contains hidden or 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
// 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) |
This file contains hidden or 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 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) |
This file contains hidden or 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
// 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! |
This file contains hidden or 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
// 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); |
NewerOlder