Hacker News new | ask | show | jobs
by elbenshira 4298 days ago
That article was what inspired me to write this blog post.

In short, the key insight of the blog post is this (via untested javascript):

You can define transforming functions like map and filter via reduce (e.g. foldleft or, for Ruby, inject) instead. For example,

  map([...], function (x) { return x + 1; });
Can be written as:

  // append (x + 1) to the result collection
  reduce([...], [], function (result, x) { return append(result, plusOne(x)); });
But notice that, above, we explicitly chose `plusOne` and `append`. If we allow a user to pass in any function instead, then we have a higher-order function:

  function mapping(f) {
    return function (reducing) {
      return function (result, input) {
        return reducing(result, f(input));
      }
    };
  }
Now we can do:

  var xform = mapping(plusOne)(append);
  
  reduce([...], [], xform);
`xform` is a transducer.