Skip to content

Instantly share code, notes, and snippets.

View UserGalileo's full-sized avatar

Michele Stieven UserGalileo

View GitHub Profile
@UserGalileo
UserGalileo / universal.interceptor.ts
Last active June 15, 2022 13:34
Angular Universal - Interceptor
/**
* 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';
@UserGalileo
UserGalileo / instantiator.module.ts
Last active December 13, 2018 18:41
Angular - InstantiatorModule
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.
*
@UserGalileo
UserGalileo / functions_monoid.js
Created April 14, 2020 17:15
Functions under composition form a Monoid
// 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
@UserGalileo
UserGalileo / functions_yielding_monadic_values.js
Created April 14, 2020 17:21
Functions yielding monadic values
// 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);
@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);
@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 / 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 / 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 / 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 / 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)