Functional Programming-compose and pipe

Source: Internet
Author: User

One pattern in functional programming is to implement a combination function by combining functions of multiple functions. A tool library that generally supports functional programming implements this pattern, which is commonly referred to as compose and pipe. For example, the Ramda tool Library, known as the functional type.

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)) // 7console.log(fun2(-2, 3)) // 9

As can be seen from the above example, assuming f , g and h representing three functions respectively, the compose(f,g,h) returned function accomplishes (...args) => f(g(h(...args))) a similar function. That is, right-to-left combination of multiple functions, the return value of the preceding function as the parameters of the next function; pipe(f,g,h) The returned function accomplishes a similar (...args) => h(g(f(...args))) function, that is, to combine multiple functions from left to right, the return value of the preceding function as the parameter of the next function, the first function expected to accept any argument, and the subsequent function expects to accept only one parameter. Put compose in front because it embodies the mathematical meaning of the right-to-left operation.
reduxThe application of the compose function to enhancestore

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 function to receive a sequence of functions, pipe and return a function, using an array of reduce methods can be easily implemented by the two functions, the following is the redux source code in the implementation of the compose 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 the implementation of the es6+, and it is easy to write the ES5 implementation method in the code above.

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)}

Implement the method compose , only need to change a few places can implement the pipe 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 directly with the compose method.pipe

function pipe(...funcs){  if(funcs.length === 0) {    return arg => arg  }  return compose(...funcs.reverse())}

The concept of composition comes from mathematics, which has an important characteristic is the binding law

// 结合律(associativity)var associative = compose(f, compose(g, h)) == compose(compose(f , g), h); // true

Compliance with the law means that it doesn't matter whether you put it in g h a group or put it in a group f g . In the actual development process, we can minimize the functions of the function, which is also consistent with a single principle, and then through the combination and combination to achieve large functional requirements.

Functional Programming-compose and pipe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.