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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
mukhriddinweb profile image
Mukhriddin Khodiev (work)

like

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay