Skip to content

Instantly share code, notes, and snippets.

View freddi301's full-sized avatar

Frederik Batuna freddi301

View GitHub Profile
@freddi301
freddi301 / do_not_cache.http.headers
Created June 15, 2017 08:26
Do not cache HTTTP headers
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
@freddi301
freddi301 / TypeFaster.html
Last active September 25, 2019 09:29
Type Faster
<html>
<head>
<style>
.type-chars {
font-family: monospace;
font-size: 30px;
}
</style>
</head>
<body>
@freddi301
freddi301 / curryByNam.js
Created August 30, 2017 08:10
JavaScript + Flow Named Curried Functions
/* @flow */
const createPerson = ({}: { name: string, birth: Date, pets: number }): [] => []
const a = {};
//createPerson(a);
const b = { ...a, name: 'fred' };
//createPerson(b);
@freddi301
freddi301 / veawco.js
Created September 19, 2017 15:12
version aware programming proof of concept
// @flow
/// as first we declare the contract our program will have with external world
type v1Api = {
getPerson(id: string): v1Person,
addPerson(person: v1Person): void
};
type v1Person = { id: string, name: string, age: number };
@freddi301
freddi301 / index.js
Last active November 3, 2017 13:19
Observables (alternative)
// alternative observables, knowledge about Observables implemetation is required (ex: RxJs)
// this is fully type-annoteable (typescript, flowtype)
// An observer is a function that takes an item from the stream
// and must return a function that will be called with the next item in the stream
// this way stateful observers can mantain state in clojures and every instance of them if replayable.
// observers should not have side effects
// type Observer<T> = (item: T) => Observer<T>
@freddi301
freddi301 / retry.ts
Created January 4, 2018 12:01
Generic retry function decorator (typescript)
export function retrySync<Fun extends (...args: any[]) => any, State>({
fun,
predicate,
nextState,
state
}: {
fun: Fun;
state: State;
nextState: (state: State) => State;
predicate: (state: State) => boolean;
@freddi301
freddi301 / singleton.ts
Created February 27, 2018 11:03
singleton
export function singleton<T>(builder: () => T): () => T {
let getter = () => {
const value = builder();
getter = () => value;
return value;
}
return () => getter()
}
const x = singleton(() => { console.log("creating 5"); return 5 })
@freddi301
freddi301 / glider-avatar.svg
Last active March 14, 2018 11:12
My avatar
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@freddi301
freddi301 / do-await.ts
Created May 4, 2018 11:05
TypeScript do notation using await
class Nothing<T> implements PromiseLike<T> {
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2> {
return (onrejected as any)(this);
}
}
class Just<T> implements PromiseLike<T> {
constructor(private value: T) { }
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2> {
return (onfulfilled as any)(this.value);
}
@freddi301
freddi301 / lens.ts
Last active December 11, 2018 14:53
Lens in typescript
export const get = Symbol("get");
export const set = Symbol("set");
interface Lens<T, V> {
[get]: (o: T) => V;
[set]: (v: V) => (t: T) => T;
}
const identity = <T>(): Lens<T, T> => ({