Last active
January 15, 2018 19:36
-
-
Save jethrolarson/baeebc8f164ddfb584cd to your computer and use it in GitHub Desktop.
combinators in es6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [BCKW](https://en.wikipedia.org/wiki/B,C,K,W_system) | |
// compose | |
var B = f => g => a => f(g(a)) | |
// flip | |
var C = f => a => b => f(b)(a) | |
// Const | |
var K = a => b => a | |
// apply arg twice | |
var W = f => a => f(a)(a) | |
// [SKI Combinators](https://en.wikipedia.org/wiki/SKI_combinator_calculus) | |
// Identity | |
var I = a => a | |
// K is same as above | |
// "substitution combinator" | |
// "S applies x to y after first substituting z into each of them. Or put another way, | |
// x is applied to y inside the environment z." | |
var S = a => b => c => a(c)(b(c)) | |
// technically you can define w with tacitly using the other combinators. | |
var W = C(S)(I) | |
// Technically you can define all the other combinators with only S and K. Hit the wiki | |
// for deets. | |
// lets test W | |
// var add = a => b => a + b | |
// console.log(W(add)(1)) | |
//:: (a -> a -> b) -> (a -> b) | |
var U = (fn) => fn(fn) | |
// Honestly, I'm not sure what this is useful for. U(U) is the easiest way to create an | |
// infinite loop mathematically... | |
// Y is basically a anonymous function vender. It takes a function that it will call | |
// passing a dynamically created function. Since a new function is created for each | |
// iteration of the recursion the stack doesn't grow. | |
var Y = (F) => | |
( (x) => | |
F((y) => x(x)(y)) | |
)( (x) => | |
F((y) => x(x)(y)) | |
) | |
module.exports = {U,B,K,W,C,I,Y} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment