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
/** | |
* This interceptor ensures that the app makes requests | |
* with relative paths correctly server-side. | |
* Requests which start with a dot (ex. ./assets/...) | |
* or relative ones ( ex. /assets/...) will be converted | |
* to absolute paths | |
*/ | |
import { Inject, Injectable, Injector, PLATFORM_ID } from '@angular/core'; | |
import { isPlatformServer } from '@angular/common'; | |
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; |
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
import { NgModule, ModuleWithProviders, InjectionToken, Inject, Injector } from '@angular/core'; | |
import { CommonModule } from '@angular/common'; | |
export const INSTANTIATED_SERVICES = new InjectionToken<any[]>('instantiated services'); | |
/** | |
* This module takes an array of services | |
* and instantiates them for you. They'll be | |
* singletons for the injector of the importing module. | |
* |
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
// These are our elements | |
const f = x => x + 1; | |
const g = x => x + 2; | |
const h = x => x + 3; | |
// This is our neutral element, a function which does nothing | |
const id = x => x; | |
// This is the composition | |
f(g(h(1))) // 7 |
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
// Let's just use the simplest monad, Identity | |
import { Identity } from 'wherever'; | |
// These are our new elements | |
const f = x => Identity(x + 1); | |
const g = x => Identity(x + 2); | |
const h = x => Identity(x + 3); |
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
// 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); |
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
// 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 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 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 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 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) | |
OlderNewer