Pipe / Curry / HOF

__khojiakbar__

_Khojiakbar_

Posted on June 13, 2024

Pipe / Curry / HOF

PIPE

Image description

A pipe function is used to compose multiple functions into a single function, where the output of one function becomes the input of the next. It's a way to create a sequence of function calls.

let first = (i) => i + 10;
let second = (i) => i * 2;
let third = (i) => i + 2;
let res = third(second(first(2)))

console.log(res);
Enter fullscreen mode Exit fullscreen mode

CURRY

Image description

Currying is a technique of transforming a function that takes multiple arguments into a sequence of functions, each taking a single argument. This can make functions more reusable and flexible.


function curry1(a) {
    return(b) => {
        return(c) => {
            return(d) => {
                return a+b+c+d
            }
        }
    }
}
console.log(curry1(2)(10)(20)(30));

let curry2 = (a) => (b) => (c) => (d) => a+b+c+d

console.log(curry2(1)(2)(3)(4));


Enter fullscreen mode Exit fullscreen mode

HOF

Higher-order functions are functions that take other functions as arguments or return functions as their result. They are a key feature of functional programming.


function app (func) {
    return func
}
function childFunc() {
    return ['one', 'two', 'three']
}
let res = app(childFunc())

console.log(res);
Enter fullscreen mode Exit fullscreen mode

Differences

Pipe vs. Curry:

Pipe: Composes multiple functions where the output of one function is passed as input to the next. It creates a function pipeline.
Curry: Transforms a function with multiple arguments into a sequence of functions each taking a single argument. It allows partial application of functions.

Higher-Order Functions (HOFs):

HOFs: Functions that either take other functions as arguments or return functions as their results. They are a broader concept that includes techniques like pipe and curry.

Summary

Pipe Function: Used for function composition, creating a pipeline of functions.
Curry Function: **Used for transforming a function to take arguments one at a time.
**Higher-Order Functions (HOFs):
Functions that operate on other functions, either by taking them as arguments or returning them.

💖 💪 🙅 🚩
__khojiakbar__
_Khojiakbar_

Posted on June 13, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Pipe / Curry / HOF
pipe Pipe / Curry / HOF

June 13, 2024