JavaScript
  • JavaScript Introduction
  • JS Engine
  • V8 Engine
  • First-class function
  • Optimized Code
  • Call Stack & Memory heap
  • Single Thread
  • JavaScript RunTime
  • Nodejs
  • Context and Environment
  • Hoisting
  • Functions
  • Arguments
  • Variables
  • Scope
  • IIFE
  • this
  • call(), apply() and bind()
  • currying
  • Types
  • Type Coercion
  • Functions as Object
  • HOF (Higher Order Function)
  • Two pillars of Javascript
  • Closures
  • Prototypal Inheritance
  • OOP and FP
  • OOP
    • 4 principles of OOP
  • FP
    • Pure function
    • Imperative vs Declarative
    • Immutability
    • HOF and Closures
    • Currying
    • Partial Application
    • Compose and Pipe
  • Composition vs Inheritance
  • OOP vs FP
  • JS working
  • Promises
  • Async Await
  • ES5 - ECMAScript 2009
  • ES6 - ECMAScript 2015
  • ES7 - ECMAScript 2016
  • ES8 - ECMAScript 2017
  • ES9 - ECMAScript 2018
  • ES10 - ECMAScript 2019
  • ES11 - ECMAScript 2020
  • ES12 - ECMAScript 2021
  • JOB Queue
  • Promises Execution
Powered by GitBook
On this page

Was this helpful?

  1. FP

Compose and Pipe

Compose:

Composing or Composition is the idea that any sort of data transformation that we do should be obvious.

In a factory, we have a data that gets processed by a function and outputs a data and processed by another function and outputs a data and so for.

data --> function --> data --> function -->

Composability is a system design principle, that deals with the relationship between components.

Ex:
const compose = (f, g) => (data) => { f(g(data)) };
const multiplyBy3 = (num) => num * 3;
const makePositive = (num) => Math.abs(num);
const multiplyBy3AndAbsolute = compose(multiplyBy3, makePositive);
 multiplyBy3AndAbsolute(-50); // 150

Pipe:

Pipe is same as compose, instead going from right to left, it goes left to right.

Ex:
const pipe = (f, g) => (data) => { g(f(data)) };
const multiplyBy3 = (num) => num * 3;
const makePositive = (num) => Math.abs(num);
const multiplyBy3AndAbsolute = pipe(multiplyBy3, makePositive);
 multiplyBy3AndAbsolute(-50); // 150
 // copose and pipe gives the same result

Arity:

Arity is simply the number of arguments that a function takes.

PreviousPartial ApplicationNextComposition vs Inheritance

Last updated 5 years ago

Was this helpful?