What is function composition and how does it work? Function composition क्या है और यह कैसे काम करता है?
Function composition is the process of combining multiple functions to produce a new function. The output of one function becomes the input of the next.
// Basic function composition
const add5 = x => x + 5;
const multiply2 = x => x * 2;
const subtract1 = x => x - 1;
// Without composition - nested calls
const result1 = subtract1(multiply2(add5(10)));
console.log(result1); // ((10 + 5) * 2) - 1 = 29
// With composition - using a compose helper
const compose = (f, g) => x => f(g(x));
const composedFunc = compose(subtract1, compose(multiply2, add5));
console.log(composedFunc(10)); // 29
// Compose multiple functions
const composeMany = (...fns) => x => fns.reduceRight((val, fn) => fn(val), x);
const pipeline = composeMany(subtract1, multiply2, add5);
console.log(pipeline(10)); // 29
// Pipe (left to right) vs Compose (right to left)
const pipe = (...fns) => x => fns.reduce((val, fn) => fn(val), x);
const leftToRight = pipe(add5, multiply2, subtract1);
console.log(leftToRight(10)); // (10 + 5) * 2 - 1 = 29
// Real-world example: string processing
const trim = str => str.trim();
const uppercase = str => str.toUpperCase();
const reverse = str => str.split('').reverse().join('');
const addExclamation = str => str + '!';
const processString = composeMany(addExclamation, reverse, uppercase, trim);
console.log(processString(' hello world ')); // '!DLROW OLLEHT'
// Composition with pure functions
const getUser = (id) => ({ id, name: 'John', email: 'john@gmail.com' });
const getEmail = user => user.email;
const toLowerCase = str => str.toLowerCase();
const extractDomain = email => email.split('@')[1];
const getUserDomain = composeMany(extractDomain, toLowerCase, getEmail);
console.log(getUserDomain(1)); // 'gmail.com'Interview tip: Function composition is a core concept in functional programming. Explain the difference between compose (right to left) and pipe (left to right). Mention that composition requires pure functions for predictable behavior and improves code reusability.
Function composition multiple functions को combine करके नया function बनाने की process है। एक function का output दूसरे का input बनता है।
// Basic composition
const add5 = x => x + 5;
const multiply2 = x => x * 2;
const subtract1 = x => x - 1;
// Nested calls - readable नहीं
const result = subtract1(multiply2(add5(10)));
// 29
// Compose helper से
const compose = (f, g) => x => f(g(x));
const composedFunc = compose(subtract1, compose(multiply2, add5));
console.log(composedFunc(10)); // 29
// Multiple functions को compose करना
const composeMany = (...fns) => x => fns.reduceRight((val, fn) => fn(val), x);
const pipeline = composeMany(subtract1, multiply2, add5);
// Pipe (left to right) vs Compose (right to left)
const pipe = (...fns) => x => fns.reduce((val, fn) => fn(val), x);
const leftToRight = pipe(add5, multiply2, subtract1);
// Real-world: string processing
const trim = str => str.trim();
const uppercase = str => str.toUpperCase();
const reverse = str => str.split('').reverse().join('');
const processString = composeMany(reverse, uppercase, trim);
console.log(processString(' hello ')); // 'OLLEH'इंटरव्यू टिप: Function composition functional programming का core concept है। Compose (right to left) और pipe (left to right) में अंतर समझाएं। Mention करें composition को pure functions की ज़रूरत है predictable behavior के लिए।
Was this answer clear?