How to compose functions of varying arity using Lodash flow?

孤街醉人 提交于 2019-11-28 11:47:19

In the following images, I use {_} as a placeholder for a value. Think of it as a hole in the code where we pass something in.

Ok let's imagine what your function would have to do...

  • Does this seems like a generic transformation? ie, do you think we can use this in many places? – functional programming promotes building functions which are highly reusable and can be combined in various ways.
  • What is the difference between f1 and f2? f1 is a unary function which will only get one arg, f2 is a binary function which will get two. Are you going to remember which one goes in which place?
  • What governs the position that f1(x) gets placed in f2?
    • Compare f2(y,f1(x)) ...
    • to f2(f1(x),y)
    • is one of those more useful than the other?
    • are you going to remember which position f1 gets?

Recall that function composition should be able to chain as many functions together as you want. To help you understand the futility of someFunc, let's imagine it accepting up to 3 functions and 3 arguments.

  • Is there even a pattern here? Maybe, but you still have the awkward unary function f1 that only gets one arg, while f2 and f3 each get 2
  • Is it true that f2 and f3 are going need the value of the previous function calls on the right side always ?
    • Compare f3(z,f2(y,f1(x)))
    • to f3(f2(y,f1(x)),z)
    • Maybe f3 needs to chain left, but f2 chains from the right?
    • I can't imagine your entire API of binary functions would magically need chained arguments in the same place
  • You've already mixed unary with binary functions in your composition; why arbitrarily limit it to just functions of those type then? What about a function of 3 or more arguments?

The answer is self-realizing

Function composition is being misused here. Function composition pretty much only works when you're composing unary functions exclusive (functions accepting 1 argument each). It immediately breaks down and cannot be generalised when mixing in functions of higher arity.

Going back to your code now, if f3 needs a name and it is the combination of f1, f2, and two parameters, it should be plainly expressed as …

const f3 = (x,y) => f1(x, f2(y))

Because it makes so many arbitrary choices, it cannot be generalized in any useful way. Just let it be as it is.


"So is there any way to compose functions of varying arity?"

Sure, there are a couple techniques of varied practicality. I'll demonstrate use of the highly practical partial function here

const partial = (f,...xs) => (...ys) => f(...xs, ...ys)

const add = (x,y) => x + y

const mult = (x,y) => x * y

const sq = x => mult (x,x)

// R.I.P. lodash.flowRight
const compose = ([f,...fs]) => x =>
  f === undefined ? x : f (compose (fs) (x))
                  
let f = compose([partial(add, 1), sq, partial(mult, 3)])

console.log(f(2))
// add(1, square(mult(3, 2)))
// add(1, square(6))
// add(1, 36)
// => 37

Oh, by the way, we replaced Lodash's flowRight (wrapper of the complex flow) with a single line of code.

It sounds like you have a very specific requirement that may not have a lodash equivalent.

Why not just write your own helper function for this?

function composeFuncs(f1, f2) {
  return function(x, y) {
    return f1.call(this, x, f2.call(this, y));
  };
}

var myObj = {
  add: function(val1, val2) {
    return this.myVal + val1 + val2
  },
  mult: function(val) {
    return this.myVal * val
  },
  myVal: 7
};

myObj.newFunc = composeFuncs(myObj.add, myObj.mult);

// 7 + 1 + 7 * 2 = 22
console.log(myObj.newFunc(1, 2));

Edit: Updated to handle this the same way _.flowRight does.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!