DEV Community

sakethk
sakethk

Posted on

4 1

Simple js compose

The concept of compose is simple — it combines n functions. It’s a pipe flowing right-to-left, calling each function with the output of the last one.

Array.prototype.reduceRight = function (...args) {
  const _this = this
  return _this.reverse().reduce(...args)
}

const compose = (...args) => x => args.reduceRight((acc, currFn)=>{
return currFn(acc)
}, x)


const double = x => x * 2
const inc = x => x + 1


const incAndOct = compose(double, double, double, inc)

incAndOct(2) //24

Enter fullscreen mode Exit fullscreen mode
Explanation:

we are passing 2 to incAndOct function. First it will call inc method then the result will be 3 and next it will apply double method on 3 so result is 6 again one more double but this time on double(3) i.e 6 result is 12 now final double on 12 it is 24

Top comments (0)

Image of Timescale

PostgreSQL for Agentic AI — Build Autonomous Apps on One Stack 1️⃣

pgai turns PostgreSQL into an AI-native database for building RAG pipelines and intelligent agents. Run vector search, embeddings, and LLMs—all in SQL

Build Today

👋 Kindness is contagious

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay