This is the sixth day of my participation in Gwen Challenge

What is a combination of functions

  • Pure functions and Currization are easy to write onion codeh(g(f(x)))
    • Take the last element of the array and convert it to uppercase,_.toUpper(_.first(_.reverse(array)))

  • Function composition allows us to recombine fine-grained functions to generate a new function

The pipe

Let’s take a look at the pipeline.

The following figure shows the process of using functions to process data in the program. The fn function is given parameter A and the result b is returned. You can think of data A going through a pipe to get data B.When the fn function is complicated, we can divide the function fn into several smaller functions, and there are more m and N generated in the intermediate operation process. In the figure below, it can be imagined that the pipe FN is split into three pipes F1, F2 and F3. Data A obtains the result M through pipe F3, m obtains the result N through pipe F2, and N obtains the final result B through pipe F1

fn = compose(f1, f2, f3)
b = fn(a)
Copy the code

Function composition

  • Compose: If a function needs to be processed by multiple functions to get the final value, you can combine the intermediate functions into a single function
    • A function is like a pipeline of data, and a combination of functions connects these pipes, allowing data to pass through multiple pipes to form the final result
    • Function combinations are executed from right to left by default
// combine functions
function compose (f, g) {
    return function (x) {
        return f(g(x))
    }
}
function first (arr) {
    return arr[0]}function reverse (arr) {
    return arr.reverse()
}
// Run from right to left
let last = compose(first, reverse)
console.log(last([1.2.3.4]))
Copy the code
  • Combinatorial functions in Lodash
    • Lodash combines the flow() or flowRight() functions, both of which can combine multiple functions
    • Flow () runs from left to right
    • FlowRight () runs from right to left and is used more often
const _ = require('lodash')
const toUpper = s= > s.toUpperCase()
const reverse = arr= > arr.reverse()
const first = arr= > arr[0]
const f = _.flowRight(toUpper, first, reverse)
console.log(f(['one'.'two'.'three']))
Copy the code
  • Simulate the flowRight method for implementing LoDash
// Multiple function combinations
function compose (. fns) {
    return function (value) {
    return fns.reverse().reduce(function (acc, fn) {
            return fn(acc)
        }, value)
    }
}
// ES6
const compose = (. fns) = > value= > fns.reverse().reduce((acc, fn) = > fn(acc), value)
Copy the code
  • The combination of functions must satisfy associativity:
    • We can either combine g with H, or f with g, and it’s the same thing
// Associativity
let f = compose(f, g, h)
let associative = compose(compose(f, g), h) == compose(f, compose(g, h))
// true
Copy the code
  • So the code could also look like this
const _ = require('lodash')
// const f = _.flowRight(_.toUpper, _.first, _.reverse)
// const f = _.flowRight(_.flowRight(_.toUpper, _.first), _.reverse)
const f = _.flowRight(_.toUpper, _.flowRight(_.first, _.reverse))
console.log(f(['one'.'two'.'three']))
// => THREE
Copy the code