One functional programming model is implemented by the function of a combined function of a combination of a plurality of functions. General functional programming support tool magazine have achieved this mode, which is generally referred to compose the pipe. As a function of the type known Ramda tool library, for example.
const R = require('ramda');
function inc (num) {
return ++num;
}
const fun1 = R.compose(Math.abs, inc, Math.pow)
const fun2 = R.pipe(Math.pow, Math.abs, inc)
console.log(fun1(-2, 3))
console.log(fun2(-2, 3))
As can be seen from the above example, it is assumed f
, g
, h
represent three functions, the compose(f,g,h)
function returns perform similar (...args) => f(g(h(...args)))
functions. I.e., a combination of a plurality of functions from right to left, the return parameters as a function of the value of a function of the foregoing; pipe(f,g,h)
function returns perform similar (...args) => h(g(f(...args)))
functions, i.e. from left to right combination of a plurality of functions, the return value as a function of the previous function parameter; function expected to be executed first can accept any number of parameters, estimated after the function takes a single argument. Put compose
in front of talk because it reflects the more right-to-left operation on the mathematical meaning. redux
That is in the compose
application function to enhance thestore
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import DevTools from './containers/DevTools'
import reducer from '../reducers'
const store = createStore(
reducer,
compose(
applyMiddleware(thunk),
DevTools.instrument()
)
)
In general, compose
and the pipe
function of receiving function sequence, and returns a function, using an array of the reduce
method can be easily implemented these two functions, the following is the redux
source code for the compose
implementation of the method:
function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
The above code is ES6 + implementation, code is easy to follow the above write-implemented method of ES5
function _compose(f, g) {
return function() {
return f.call(this, g.apply(this, arguments));
};
}
function compose() {
var args = Array.prototype.slice.call(arguments)
if (args.length === 0) {
return function(arg){
return arg
}
}
if (args.length === 1) {
return args[0]
}
return args.reduce(_compose)
}
To achieve a compose
method, only we need to change a few places will be able to implement pipe
the method.
function pipe(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => b(a(...args)))
}
Or by direct compose
way to achievepipe
function pipe(...funcs){
if(funcs.length === 0) {
return arg => arg
}
return compose(...funcs.reverse())
}
The concept comes from the combination of mathematics, which has an important feature is associative
var associative = compose(f, compose(g, h)) == compose(compose(f , g), h);
In line with the associative law means that no matter you are putting g
and h
assigned to a group, or to f
and g
assigned to a group are not important. In the actual development process, we can minimize as much as possible the performance function, which is also in line with the principle of a single, and then to complete the functional requirements through a combination of large and combinations.
https://www.cnblogs.com/star91/p/han-shu-shi-bian-chengcompose-yupipe.html