Skip to content

Instantly share code, notes, and snippets.

View freddi301's full-sized avatar

Frederik Batuna freddi301

View GitHub Profile
import sourcecode
case class Symbol(name: String)(implicit line: sourcecode.Line)
case class Struct(tag: Symbol, entries: Map[Symbol, Struct])
sealed trait TypeTerm
case class Union(cases: Map[Symbol, Map[Symbol, TypeTerm]]) extends TypeTerm
@freddi301
freddi301 / force.tsx
Created May 21, 2019 09:39
Force simulation
const ns = [
{ name: "a", x: 200.01, y: 200.01 },
{ name: "b", x: 220, y: 220 },
{ name: "c", x: 180, y: 200.01 },
{ name: "d", x: 182, y: 201 },
{ name: "e", x: 230, y: 170 },
{ name: "f", x: Math.random() * 400, y: Math.random() * 400 },
{ name: "g", x: Math.random() * 400, y: Math.random() * 400 },
];
@freddi301
freddi301 / pattern-match.ts
Created March 25, 2019 14:05
Typescript Pattern matching
const match = <T>(item: T) => <
Matchers extends ((item: T) => Match<any> | undefined)[]
>(
...cases: Matchers
): Exclude<ReturnType<Matchers[number]>, undefined>["value"] => {
for (const matcher of cases) {
const result = matcher(item) as ReturnType<Matchers[number]>;
if (result) {
return result.value;
}
@freddi301
freddi301 / validate.ts
Created March 6, 2019 11:12
TypeScript Validation
class DataValid<T, E> {
constructor(public data: T) {}
}
class DataInvalid<T, E> {
constructor(public error: E, public got: unknown) {}
}
type DataValidity<T, E> = DataValid<T, E> | DataInvalid<T, E>;
type DataValidator<T, E> = (data: unknown) => DataValidity<T, E>;
type DataValidatorData<
@freddi301
freddi301 / function-composition.ts
Created March 5, 2019 16:51
TypeScript function composition
function pipe<A, B>(f: (a: A) => B) {
return {
end: f,
pipe<C>(g: (b: B) => C) {
return pipe((a: A) => g(f(a)));
}
};
}
function compose<A, B>(f: (a: A) => B) {
@freddi301
freddi301 / tagged-union-pattern-match.ts
Created January 16, 2019 15:24
TypeScript taggedUnion pattern match
class TaggedUnionValue<
TaggedUnion extends { [K in keyof TaggedUnion]: any[] },
Tag extends keyof TaggedUnion
> {
constructor(
private readonly tag: Tag,
private readonly value: TaggedUnion[Tag],
) {}
}
@freddi301
freddi301 / Promise.js
Created January 9, 2019 10:58
naive javascript promise
const status = Symbol();
const value = Symbol();
const set = Symbol();
const listeners = Symbol();
class Promise {
constructor(callback) {
this[listeners] = [];
this[status] = "pending";
const resolve = value => {
@freddi301
freddi301 / I18n.tsx
Created January 4, 2019 16:27
React TypeScript i18n
import React from "react";
type Dictionary = Record<string, string | ((arg: any) => string)>;
export type L12n<T extends Dictionary> = T;
export function makeI18n<T extends Dictionary>() {
const Context = React.createContext<T | undefined>(undefined);
const I18ContextConsumer = Context.Consumer;
const I18ContextProvider = Context.Provider;
function I18n<K extends keyof T>(
@freddi301
freddi301 / ts-ql.ts
Last active January 7, 2019 22:43
grapql in typescript
type DataTypeUnion = ObjectType<any> | ArrayType<any> | PrimitiveTypeUnion;
type PrimitiveTypeUnion = typeof string | typeof number | typeof boolean;
interface QueryType<
Params extends { [key: string]: PrimitiveTypeUnion },
T extends DataTypeUnion
> {
kind: "query";
params: Params;
@freddi301
freddi301 / frontend-architecture.txt
Last active November 27, 2018 12:58
Frontend Architecture
view:
component: composition of
helper: prepare data for template so no code goes into the tempalte
template: does only view, can dispatch events
container: composition of
injector: grabs data from state
wireframe: conecptual representation of ui, it's a projections of data to display and possible actions
domain: