Skip to content

Instantly share code, notes, and snippets.

View freddi301's full-sized avatar

Frederik Batuna freddi301

View GitHub Profile
@freddi301
freddi301 / transducers.ts
Created May 31, 2018 09:22
Transducers in TypeScript
type Reducer<Memo, Item> = (memo: Memo, item: Item) => Memo
type Transducer<Memo, Item, ToItem> = (reducing: Reducer<Memo, ToItem>) => Reducer<Memo, Item>
type Mapper<A, B> = (a: A) => B;
const mapping = <Memo, Item, ToItem>(mapper: Mapper<Item, ToItem>): Transducer<Memo, Item, ToItem> =>
reducing => (memo, item) =>
reducing(memo, mapper(item))
@freddi301
freddi301 / ypx.sh
Created May 22, 2018 11:00
ypx - npx for yarn
#!/usr/bin/env bash
package_name=$1
temp_dir="/tmp/ypx/$package_name/$(date +%s%N)"
mkdir -p $temp_dir
(cd $temp_dir; yarn add $package_name) && (PATH="$temp_dir/node_modules/.bin":$PATH; "$@")
rm -rf $temp_dir
@freddi301
freddi301 / dsl.ts
Created May 14, 2018 11:38
Typescript example DSL
interface MyProgramDSL<T> {
read: () => Promise<string>;
write: (value: string) => Promise<void>
done: (value: T) => Promise<T>
}
const MyProgram = async ({ read, write, done }: MyProgramDSL<string>) => {
await write("Hello, what is your name?");
const name = await read();
@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> => ({
@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 / 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 / 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 / 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 / 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 / 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 };