Skip to content

Instantly share code, notes, and snippets.

@markknol
Forked from Avaq/combinators.js
Last active February 20, 2018 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markknol/48a1bbedcd9cfb4ff6204e2e04f16b68 to your computer and use it in GitHub Desktop.
Save markknol/48a1bbedcd9cfb4ff6204e2e04f16b68 to your computer and use it in GitHub Desktop.
Common combinators in Haxe 4
static final I = x -> x;
static final K = x -> y -> x;
static final A = f -> x -> f(x);
static final T = x -> f -> f(x);
static final W = f -> x -> f(x)(x);
static final C = f -> y -> x -> f(x)(y);
static final B = f -> g -> x -> f(g(x));
static final S = f -> g -> x -> f(x)(g(x));
static final P = f -> g -> x -> y -> f(g(x))(g(y));
Name # Haskell Ramda Sanctuary Signature
identity I id identity I a → a
constant K const always K a → b → a
apply A ($) call A (a → b) → a → b
thrush T (&) applyTo T a → (a → b) → b
duplication W join² unnest² join² (a → a → b) → a → b
flip C flip flip flip (a → b → c) → b → a → c
compose B (.), fmap² map² compose, map² (b → c) → (a → b) → a → c
substitution S ap² ap² ap² (a → b → c) → (a → b) → a → c
psi P on on (b → b → c) → (a → b) → a → a → c
fix-point¹ Y fix (a → a) → a

¹) In Haxe and other non-lazy languages, it is impossible to implement the Y-combinator. Instead a variant known as the applicative or strict fix-point combinator is implemented. This variant is sometimes rererred to as the Z-combinator.

²) Algebras like ap have different implementations for different types. They work like Function combinators only for Function inputs.

Note that when I use the word "combinator" in this context, it implies "function combinator in the untyped lambda calculus".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment