Skip to content

Instantly share code, notes, and snippets.

@fvilante
fvilante / PatternMatch.ts
Last active October 30, 2020 09:55
ReasonML inspired pattern match for Typescript
// ========================================================================================
// Utilities types
// ========================================================================================
type TU11 = '(T extends U) and (U extends T)'
type TU10 = '(T extends U) and (U NOT extends T)'
type TU01 = '(T NOT extends U) and (U extends T)'
type TU00 = '(T NOT extends U) and (U NOT extends T)'
type Ext<T,U,A,B> = T extends U ? A : B // In type system jargon we can say that "T is assignable to U".
@fvilante
fvilante / Aggregate.ts
Created October 14, 2020 07:48
Aggregate Full-Typed
type Message<N extends string = string, D = unknown> = { name: N, data: D }
type Context<K0 extends string = string, K1 extends string = string, > = {
contextName: K0
aggregateName: K1
aggregateId: string
}
interface Command<N extends string = string, D = unknown> extends Message<N,D> { }
@fvilante
fvilante / immerJS-Alternative.ts
Last active October 9, 2020 12:57
ImmerJs Alternative To - Using Lenses and Atomic Reference
// This is the implementation of the ImmerJs concept but using Lenses and Atomic References.
// See bellow to see the immerJs use case I based on:
// https://www.smashingmagazine.com/2020/06/better-reducers-with-immer/
// ------------------ LIB ----------------------
// List Module
type List<A> = {
@fvilante
fvilante / SimpleHttpServerHelloWorld.ts
Created May 15, 2020 18:57
Deno Introduction Many files
import { serve } from "https://deno.land/std@0.50.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
req.respond({ body: "Hello World\n" });
}
@fvilante
fvilante / tuple.ts
Last active May 11, 2020 00:24
TS Tuple - manage a defined collection of tuples
interface FixedArray<Size extends number, A> extends ArrayLike<A> { length: Size }
type MyTriple = FixedArray<3,number>
const ok_case: MyTriple = [1,2,3] as const // ok !
const erro_case1: MyTriple = [1,2] as const // fewer elements
const erro_case2: MyTriple = [1,2,3,4] as const // many elements
const erro_case3: MyTriple = [1,2,3] // not constant in static-time
const erro_case4: MyTriple = [] as const // well, it's empty my son!
@fvilante
fvilante / turing_machine.ts
Created April 28, 2020 05:56
AST Exploration
// The Essence of A Turing Machine -
// AST explorer studies
import { hello } from './juca'
hello()
const arr = [2,hello,3]
type Reader<A> = {
@fvilante
fvilante / result.ts
Created April 3, 2020 20:14
Result<E,A>
import { foldLeftArray } from "../array/foldLeftArray"
import { Maybe } from "./maybe"
import { identity } from "./identity"
import { Either } from "./either"
/** Results in a successful value of type A or an error of type E. This type works like Either, but the semantics enforces the Error Success reasoning */
export type Result<E,A> = {
readonly kind: 'Result'
readonly unsafeRun: () => (E|A) extends void ? void : E extends void ? A : A | E
@fvilante
fvilante / propose1.ts
Created January 27, 2020 13:36
Datalink generic protocol
import { ZIO, ZIO_ } from "../zio/zio"
// STREAM
// async steam
type Stream<E,A> = {
readonly kind: 'Stream'
readonly identity: () => Stream<E,A>
readonly take: (n: number) => Stream<E,readonly A[]>
readonly map: <B>(f: (_:A) => B) => Stream<E,B>
@fvilante
fvilante / version1.ts
Created January 21, 2020 18:19
Among Algebraic type
import { Result } from "../result"
// ------------------------------------------------
// pseudo-type
// ------------------------------------------------
// todo: Similar concept of Maybe.Nothing. Reuse the nothing of maybe ? Or should be the other way around, make Nothing sub type of PseudoType
// todo: should be minimized and reused by Nothing
// purpose of this type is to save a type and use it in static-time but without have to
@fvilante
fvilante / tube2.ts
Created January 21, 2020 18:13
Draft pseudo-type and message
// ===== PseudoType ==========
export type PseudoType<A> = {
readonly kind: 'PseudoType'
readonly __UNSAFE__TYPE: A //ATTENTION: type trick (at run-time is undefined, but at static-time is A)
}
export const PseudoType = <A>():PseudoType<A> => ({kind: 'PseudoType', __UNSAFE__TYPE: undefined as unknown as A})