Skip to content

Instantly share code, notes, and snippets.

View briancavalier's full-sized avatar

Brian Cavalier briancavalier

  • Pittsburgh
View GitHub Profile
@briancavalier
briancavalier / fx.ts
Last active March 17, 2019 23:55
Small encoding of algebraic effects in Typescript (probably works in Flow, too, with syntax tweaks) used in helicopter: https://github.com/briancavalier/helicopter/blob/master/packages/core/src/fx.ts
export type Cancel = void | ((k: (r: void) => void) => void)
export const runCancel = (c: Cancel, k: (r: void) => void): void =>
c ? c(k) : k()
export type Fx<H, A> = (handler: H, k: (a: A) => void) => Cancel
export type Pure<A> = Fx<{}, A>
export const handle = <H0, H1, A>(fx: Fx<H0 & H1, A>, h1: H1): Fx<H0, A> =>
(h0, k) => fx({ ...h0, ...h1 } as H0 & H1, k)
@briancavalier
briancavalier / type-nat.js
Last active August 19, 2018 02:02
Flow type-level naturals and addition
// @flow
// Type-level naturals
type Zero = 0
interface Succ {
<N>(N): [1, N]
}
// Hey look, we can do type-level pattern matching
@briancavalier
briancavalier / example.js
Last active May 26, 2018 18:25
Convert node async functions into most.js streams
import { readFile } from 'fs'
// Create a stream-returning version of fs.readFile
const readFileS = fromNode(readFile)
// Use it to create a stream containing the files contents
// map(String) to convert Buffer to string
const s = readFileS('./a/file.txt').map(String)
// Observe the contents
@briancavalier
briancavalier / type-list.js
Created April 6, 2018 13:46
Flow type-level lists
// @flow
// Type-level lists
type Empty = []
// Type-level tail function
type Tail<L> = $Call<FTail, L>
interface FTail {
(Empty): Empty,
<H, T>([H, T]): T
@briancavalier
briancavalier / lazy-pair.js
Last active October 12, 2017 13:23
Fun with a Lazy type and pairs
// Pair helpers
// Map the first element of a pair
const first = <A, B, C> (f: A => B, [a, c]: [A, C]): [B, C] =>
[f(a), c]
// Map the second element of a pair
const second = <A, B, C> (f: B => C, [a, b]: [A, B]): [A, C] =>
[a, f(b)]
@briancavalier
briancavalier / except.js
Last active September 7, 2017 15:52
A simple Either with added exception semantics, like Haskell ExceptT
// @flow
import { curry2, curry3 } from '@most/prelude'
export type Except<E, A> = Exception<E, A> | Result<E, A>
export const result = <E, A> (a: A): Except<E, A> =>
new Result(a)
export const throwError = <E, A> (e: E): Except<E, A> =>
new Exception(e)
@briancavalier
briancavalier / Maybe.js
Created December 31, 2013 16:55
JavaScript Maybe monad
module.exports = Maybe;
function Maybe(x) {
this._value = x;
}
Maybe.of = of;
Maybe.empty = empty;
var nothingValue = void 0;
@briancavalier
briancavalier / esnextbin.md
Created May 2, 2017 01:36 — forked from TylorS/esnextbin.md
esnextbin sketch
import { tap, take, skip, concat, fromArray, runEffects } from '../packages/core/src/index'
import { newDefaultScheduler } from '../packages/scheduler/src/index'
import { EventEmitter } from 'events'
import multicastStream from '@most/multicast'
import { fromEvent } from 'most'
const emitter = new EventEmitter()
const scheduler = newDefaultScheduler()
const observe = (f, stream) => runEffects(tap(f, stream), scheduler)
@briancavalier
briancavalier / esnextbin.md
Created February 9, 2017 14:12
esnextbin sketch