From 37bc6788a849b072e3f0ae7656214fba8c86e68d Mon Sep 17 00:00:00 2001 From: David Chambers Date: Sat, 19 Sep 2015 14:45:07 -0700 Subject: [PATCH] correct the definition of higher-order functions, and update examples --- readme.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 2318509..aca6440 100644 --- a/readme.md +++ b/readme.md @@ -19,16 +19,28 @@ console.log(arity); ``` --- -## Higher Order Functions (HOF) +## Higher-Order Functions (HOF) -> A function for which both the input and the output are functions. +> A function which takes a function as an argument and/or returns a function. ```js -let greet = (name) => () => `Hello ${name}!`; +const filter = (pred, xs) => { + const result = []; + for (var idx = 0; idx < xs.length; idx += 1) { + if (pred(xs[idx])) { + result.push(xs[idx]); + } + } + return result; +}; ``` ```js -greet("HOF")(); // Hello HOF! +const is = type => x => Object(x) instanceof type; +``` + +```js +filter(is(Number), [0, '1', 2, null]); //=> [0, 2] ``` ## Partial Application