Skip to content

Instantly share code, notes, and snippets.

briancavalier /
Last active May 10, 2020
A technique for strongly-typed, heterogeneous variadic function types in Typescript

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]
gbaz / Generic.purs
Last active Feb 6, 2016
Generics in purescript (updated to version 7)
View Generic.purs
module Generic where
import Prelude
import Data.Maybe
import Data.Traversable
import Data.Array
data GenericSpine = SProd String (Array (Unit -> GenericSpine))
| SRecord (Array {recLabel :: String, recValue :: Unit -> GenericSpine})
| SNumber Number
sacundim / Shape.hs
Last active Apr 10, 2018
OOP Shape example in Haskell, using existentials, GADTs, Typeable and ConstraintKinds to support downcasts.
View Shape.hs
{-# LANGUAGE GADTs, ConstraintKinds, KindSignatures, DeriveDataTypeable #-}
{-# LANGUAGE TypeOperators, ScopedTypeVariables, FlexibleInstances #-}
module Shape where
import Control.Applicative ((<$>), (<|>))
import Data.Maybe (mapMaybe)
import Data.Typeable
import GHC.Exts (Constraint)
View Main.purs
module Main where
import Data.Maybe
import Debug.Trace
import Data.JSON
import Network.XHR
main = get defaultAjaxOptions
{ onReadyStateChange = onSuccess $ \response -> do
txt <- getResponseText response
paf31 /
Last active May 17, 2020
Reimplementing a NodeJS Service in Haskell


At DICOM Grid, we recently made the decision to use Haskell for some of our newer projects, mostly small, independent web services. This isn't the first time I've had the opportunity to use Haskell at work - I had previously used Haskell to write tools to automate some processes like generation of documentation for TypeScript code - but this is the first time we will be deploying Haskell code into production.

Over the past few months, I have been working on two Haskell services:

  • A reimplementation of an existing service, previously written for NodeJS using TypeScript.
  • A new service, which would interact with third-party components using standard data formats from the medical industry.

I will write here mostly about the first project, since it is a self-contained project which provides a good example of the power of Haskell. Moreover, the proces

tonymorris / Terminal.hs
Created May 22, 2014
Terminal I/O with Free
View Terminal.hs
{-# LANGUAGE RankNTypes #-}
data Free f a =
Done a
| More (f (Free f a))
instance Functor f => Functor (Free f) where
fmap f (Done a) =
Done (f a)
fmap f (More k) =
briancavalier / example.js
Last active Feb 29, 2020
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));
robotlolita /
Last active Jan 1, 2016
Monadic Futures specification

A future should provide the fork, of, chain, and orElse methods, subject to the following rules:

new Future(computation)

  1. The computation should fulfil the type (α → γ), (β → γ) → γ.
  2. The value should be stored in the internal field [[Computation]], as-is.
  3. If computation is not a function, the behaviour is undefined.

Future.prototype.fork(f, g)


Disclaimer: I work for Mozilla and am a Node.js core maintainer. The opinions expressed here do not reflect those of Mozilla or the Node.js project.

After watching the events unfold the last 2 days I decided to sit down, with my two cartons of Trader Joe's eggnog, and simply express with all humility and confidence what libuv@47d98b6 definitely was not.

Over the last year I have had the pleasure of being mentored by Ben. He is largely responsible for my initial major involvement writing performance improvements for Node, and has easily surpassed

You can’t perform that action at this time.