Skip to content

Instantly share code, notes, and snippets.

Brian Cavalier briancavalier

Block or report user

Report or block briancavalier

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@briancavalier
briancavalier / simple-promise-retry.js
Created Feb 24, 2011
A few general patterns for retries using promises
View simple-promise-retry.js
function keepTrying(otherArgs, promise) {
promise = promise||new Promise();
// try doing the important thing
if(success) {
promise.resolve(result);
} else {
setTimeout(function() {
keepTrying(otherArgs, promise);
View tiny closure Promise.js
function Promise() {
var callbacks = [],
promise = {
resolve: resolve,
reject: reject,
then: then,
safe: {
then: function safeThen(resolve, reject) {
promise.then(resolve, reject);
}
@briancavalier
briancavalier / serialize-function.js
Created Feb 10, 2015
Serializing / deserializing JavaScript functions *with free variables*
View serialize-function.js
var a = 123, b = 'hello';
function test(x, y) {
console.log(this);
return a + x + b + y;
}
// Serialize a function *with its captured environment*
var sf = serialize(test, { a: a, b: b });
// Deserialize with captured environment
@briancavalier
briancavalier / promise-monad-proof.js
Created Aug 8, 2012
A proof that Promises/A is a Monad
View promise-monad-proof.js
//-------------------------------------------------------------
//
// Hypothesis:
//
// Promises/A is a Monad
//
// To be a Monad, it must provide at least:
// - A unit (aka return or mreturn) operation that creates a corresponding
// monadic value from a non-monadic value.
// - A bind operation that applies a function to a monadic value
@briancavalier
briancavalier / typescript-strongly-typed-variadic-2.md
Last active May 24, 2019
A technique for strongly-typed, heterogeneous variadic function types in Typescript
View typescript-strongly-typed-variadic-2.md

Simpler Variadic Function Types in TypeScript (part 2): Higher-order Variadic Functions

In part 1 we looked at a technique that allows expressing strongly-typed variadic functions in a way that solves problems with the current most common approach. It reduces code size and repetition, and it scales to any arity.

I mentioned that the technique can also be extended to higher-order variadic functions, such as the typical zipWith. Let’s explore how to do that.

ZipWith Example

As we did in part 1, let’s start with an example, a typical zipWith on arrays.

@briancavalier
briancavalier / typescript-strongly-typed-variadic-1.md
Last active May 24, 2019
A technique for strongly-typed, heterogeneous variadic function types in Typescript
View typescript-strongly-typed-variadic-1.md

Simpler Variadic Function Types in TypeScript (part 1)

Variadic functions, such as the common zip function on arrays, are convenient and remove the need for lots of specific arity-function variants, e.g., zip2, zip3, zip4, etc. However, they can be difficult and tedious to type correctly in TypeScript when the return type depends on the parameter types, and the parameter types are heterogeneous.

Zip Example

Given a typical zip on arrays:

const a: number[] = [1, 2, 3]
@briancavalier
briancavalier / example.js
Last active May 17, 2019
Infinite "lists" in es6 using generators and yield
View example.js
// Requires traceur
import { iterate, take, map, foldl } from './list-out-of-yield';
// Sum of squares of integers 1..100
var values = take(100, iterate(x => x + 1, 1));
var result = foldl((x, y) => x + y, 0, map(x => x * x, values));
console.log(result);
@briancavalier
briancavalier / fx.ts
Last active Mar 17, 2019
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
View 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 Aug 19, 2018
Flow type-level naturals and addition
View type-nat.js
// @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
Convert node async functions into most.js streams
View example.js
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
You can’t perform that action at this time.