Skip to content

Instantly share code, notes, and snippets.

@vitaliiznak
Last active February 2, 2017 15:34
Show Gist options
  • Select an option

  • Save vitaliiznak/007b7706bb5e8e953b997597fe5d5ef8 to your computer and use it in GitHub Desktop.

Select an option

Save vitaliiznak/007b7706bb5e8e953b997597fe5d5ef8 to your computer and use it in GitHub Desktop.
"use strict"
function createEvaluator() {
let evaluator;
return {
add: ( function(fn, ...rest) {
const lastFn = fn.bind(null, ...rest), currenntFn = evaluator;
evaluator = evaluator ? (...finalArrgs) => lastFn(currenntFn(...finalArrgs)) : lastFn
return this;
}),
evaluate: arr => arr.map(el => evaluator(el))
}
}
createEvaluator()
.add(Math.abs)
.add(Math.sqrt)
.add((a, b) => a + b, -1)
.evaluate([-9, 36, 144])
// response [2, 5, 11]
const compose = (...fns) =>
fns.reduce(
(f, g) =>
(...args) =>
f(g(...args))
)
@vitaliiznak
Copy link
Author

Lazy Evaluation and Function composition

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