Skip to content

Instantly share code, notes, and snippets.

@alpha123
Last active December 31, 2015 18:39
Show Gist options
  • Save alpha123/8028402 to your computer and use it in GitHub Desktop.
Save alpha123/8028402 to your computer and use it in GitHub Desktop.
"Dependently-typed" JS thing
// Syntax note: optionally whitespace-sensitive, \ is an alias for function,
// and -> at the end of a function means return the last value.
// Haskell-style (operator), which is implicit if an operator is the only
// argument to a function.
// The return value of a type function is compared to the value put in,
// and if they are === it type checks.
function Int(n) -> Math.floor(n)
function add(a: Int, b: Int) -> a + b
function Addable(value) -> value + value.constructor()
function add(a: Addable, b: Addable) -> a + b
function TypedArray(type) ->
function (value) -> value.every(\e -> type(e) === e)) ? value : NaN
function sum(array: TypedArray(Addable)) -> array.reduce(+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment