Skip to content

Instantly share code, notes, and snippets.

@yorickvP
Created January 1, 2012 00:59
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 yorickvP/1545839 to your computer and use it in GitHub Desktop.
Save yorickvP/1545839 to your computer and use it in GitHub Desktop.
compose using bindify
var b = require('bindify')
// first make a function that does \ a b -> \ c -> b(a c)
var compose_2 = b(b, null
, Function.prototype.call
, b._1
, b._wrap(b._this)
, nested_b(nested_b, null
, b._0
, b._wrap(b._this)
, b._wrap(b._argslice())))
// now make a function that does \ a b c ... -> \ z -> ... c (b (a z))
var compose = b(b, null
, compose_2(
b([].reduce, b._0
, nested_b(compose_2, null
, nested_b(b, null
, Function.prototype.apply
, b._wrap(b._1)
, b._this
, b._wrap(b._0))
, b([].slice, b._args))
, b._1)
, b([].shift, b._0))
, b._wrap(b._this), b._args, b._wrap(b._args))
function nested_b() {
var func = b.apply(b, arguments)
function NestedB(args, thisobj, arr, arridx) {
if (arridx != -1)
arr[arridx++] = func.apply(thisobj, args)
return 1 }
b.specialify(NestedB)
return NestedB }
// compose beta-equivalent to:
function small_compose() {
var a = arguments
return function() {
var t = this
return [].reduce.call(a, function(p, c) {
return [c.apply(t, p)] }, arguments)[0] }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment