Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

javascript - How to compose functions of varying arity using Lodash flow?

I want to do some function composition. I know already this:

If f3(x) shall be the same as f1(f2(x)) then f3 = _.flowRight(f1,f2);

If f3(x,y) shall be the same as f1(x, f2(y)) then …?

(The use case is the composition of node.js/express middleware functions.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...

a bad dream

  • 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.

a nigthmare

  • 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...