Skip to content

Instantly share code, notes, and snippets.

@yanatan16
Created August 12, 2014 22:17
Show Gist options
  • Save yanatan16/35d3d44c3c5aa81eee63 to your computer and use it in GitHub Desktop.
Save yanatan16/35d3d44c3c5aa81eee63 to your computer and use it in GitHub Desktop.
functional.goooooal.js
// this a lazy evaluation of the delayed value
// If the delayed value is delayed (i.e. a function), then return a function that will resolve eventually
// If not, then resolve immediately and prefix
function resolve(prefix, delayed_value) {
if (typeof delayed_value === 'function')
return function (a) { return resolve(prefix, delayed_value(a)) }
else
return prefix + delayed_value
}
// If passed a value, return it
// otherwise return a recursive lazy evaluation prefixing 'o'
function oooos(a) {
if (a)
return a
else
return resolve('o', oooos)
}
// return an evaluated lazy evaluation of `oooos` prefixed with 'g'
function g(a) {
return resolve('g', oooos)(a)
}
function r(p,f) { return f.bind ? function (a) { return r(p,f(a)) } : p+f }
function z(a) { return a ? a : r('o',z) }
function g(a) { return r('g',z)(a) }
> g('al')
'gal'
> g()('al')
'goal'
> g()()()()('al')
'gooooal'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment