DEV Community

_Khojiakbar_
_Khojiakbar_

Posted on • Edited on

1

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Collapse
 
mukhriddinweb profile image
Mukhriddin Khodiev (work)

like