“结合"以功能方式在 javascript 中运行? [英] To "combine" functions in javascript in a functional way?
问题描述
我正在学习函数式编程,我想知道是否有一种方法可以像这样组合"函数:
I'm learning functional programming and I wonder if there is a way to "combine" functions like this:
function triple(x) {
return x * 3;
}
function plusOne(x) {
return x + 1;
}
function isZero(x) {
return x === 0;
}
combine(1); //1
combine(triple)(triple)(plusOne)(1); // 10
combine(plusOne)(triple)(isZero)(-1); // true
如果para是一个函数,它会将该函数组合"到自身中,如果不是则返回最终结果.谢谢!
If the para is a function, it "combines" the function into itself, and if not it will return the final result. Thanks!
推荐答案
传承
这是一个数学概念,称为函数组合.
This is a concept from maths called function composition.
f(x) = y
g(y) = z
g(f(x)) = z
(g•f)(x) = z
最后一行读作x 中的 f 的 g 等于 z".组合函数的优点在于消除了点.注意在 g(f(x)) = z
中,我们接受一个 x
输入并得到一个 z
输出.这会跳过中间点 y
.
That last line is read "g of f of x equals z". What's great about composed functions is the elimination of points. Notice in g(f(x)) = z
we take an x
input and get a z
output. This skips the intermediate point, y
.
组合是创建高阶函数并保留代码的好方法闪闪发光的干净.很明显为什么我们希望在我们的 Javascript 程序中使用它.
Composition is a great way to create higher-order functions and keep your code sparkly clean. It's plain to see why we'd want this in our Javascript programs.
补偿
JavaScript 是一种多范式语言,具有丰富的函数支持.我们可以创建一个简单的 comp
函数,它结合了两个输入函数,g
和 f
,并产生一个 new 功能 -
JavaScript is a multi-paradigm language with rich support for functions. We can create a simple comp
function, which combines two input functions, g
and f
, and results in a new function -
function triple(x) {
return x * 3
}
function plusOne(x) {
return x + 1
}
function comp(g, f) {
return function(x) {
return g(f(x)) // "g of f of x"
}
}
const myfunc =
comp(triple, plusOne)
console.log(myfunc(1))
评价
triple(plusOne(1))
triple(2)
6
撰写
正如问题所暗示的那样,我们很可能想要组合两个以上的功能.下面我们编写 compose
,它接受 all
输入函数,并使用上面的简单 comp
将它们reduce
.如果没有给出函数,我们返回空函数,identity
-
Just as the question suggests, it's likely we will want to combine more than two functions. Below we write compose
which takes all
of the input functions and reduce
s them using our simple comp
from above. If no functions are given, we return the empty function, identity
-
const triple = (x) =>
x * 3
const plusOne = (x) =>
x + 1
const comp = (g, f) =>
x => g(f(x)) // "g of f of x"
const identity = (x) =>
x
const compose = (...all) =>
all.reduce(comp, identity)
const myfunc =
compose(triple, triple, plusOne) // any amount of funcs
console.log(myfunc(1))
评价
triple(triple(plusOne(1)))
triple(triple(2))
triple(6)
18
管道
您可以随心所欲地发挥创造力.下面,我们编写 pipe
允许我们的程序以舒适的从左到右的方向阅读 -
You can be as creative as you like. Below, we write pipe
which allows our programs to read in a comfortable left-to-right direction -
const triple = (x) =>
x * 3
const plusOne = (x) =>
x + 1
const pipe = x =>
f => pipe(f(x))
pipe(1)(plusOne)(triple)(triple)(console.log) // 18
pipe(3)(triple)(plusOne)(triple)(plusOne)(console.log) // 31
表达式一的评估-
f => pipe(f(1))
pipe(plusOne(1))
f => pipe(f(2))
pipe(triple(2))
f => pipe(f(6))
pipe(triple(6))
f => pipe(f(18))
pipe(console.log(18))
18
和表达式二 -
f => pipe(f(3))
pipe(triple(3))
f => pipe(f(9))
pipe(plusOne(9))
f => pipe(f(10))
pipe(triple(10))
f => pipe(f(30))
pipe(plusOne(31))
f => pipe(f(31))
pipe(console.log(31))
31
相关技术
柯里化函数和部分应用是与函数组合结合的概念.上面的 pipe
在 another Q&A 中介绍为 $
和这里再次演示 -
Curried functions and partial application are concepts that gel with function composition. pipe
above is introduced in another Q&A as $
and demonstrated again here -
const $ = x => // "pipe", or whatever name you pick
k => $ (k (x))
const add = x => y => // curried add
x + y
const mult = x => y => // curried mult
x * y
$ (1) // 1
(add (2)) // + 2 = 3
(mult (6)) // * 6 = 18
(console.log) // 18
$ (7) // 7
(add (1)) // + 1 = 8
(mult (8)) // * 8 = 64
(mult (2)) // * 2 = 128
(mult (2)) // * 2 = 256
(console.log) // 256
这篇关于“结合"以功能方式在 javascript 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!