<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yagyam Patidar on Medium]]></title>
        <description><![CDATA[Stories by Yagyam Patidar on Medium]]></description>
        <link>https://medium.com/@yagyampatidar19?source=rss-45eaeb64500c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XaSokOqDVbblQK5w0-imTg.jpeg</url>
            <title>Stories by Yagyam Patidar on Medium</title>
            <link>https://medium.com/@yagyampatidar19?source=rss-45eaeb64500c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 21:49:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yagyampatidar19/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Make Functions Flexible: Currying, Partial Application, and Composition in JS]]></title>
            <link>https://medium.com/@yagyampatidar19/make-functions-flexible-currying-partial-application-and-composition-in-js-a94f24606bff?source=rss-45eaeb64500c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a94f24606bff</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[javascript-currying]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Yagyam Patidar]]></dc:creator>
            <pubDate>Sun, 24 Aug 2025 09:37:38 GMT</pubDate>
            <atom:updated>2025-08-24T09:37:38.074Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*x2wn7ZKDxshNeXZb90q8RQ.png" /></figure><p>In JavaScript, functions are more powerful than they first appear. One such power is <strong>currying</strong> — a technique where a function that normally takes multiple arguments is broken down into a <strong>sequence of functions</strong>, each taking one argument at a time.</p><p>Currying is not just a “cool trick.” It’s a practical way to make your functions more <strong>reusable, flexible, and composable</strong>. With currying, you can:</p><ul><li><strong>Partially apply arguments</strong> — fix some inputs and reuse the function in multiple contexts.</li><li><strong>Compose cleaner pipelines</strong> — chain functions step by step instead of nesting them.</li><li><strong>Improve readability</strong> — functions become small, predictable, and easier to test.</li></ul><h3>Let’s Understand Currying</h3><p>Now that we know what currying means, it’s time to see it in action. A simple example will make the idea crystal clear.</p><pre>// Normal function<br>function add(a, b, c) {<br>  return a + b + c;<br>}<br><br>console.log(add(1, 2, 3)); // 6<br><br>// Curried version<br>function curriedAdd(a) {<br>  return function(b) {<br>    return function(c) {<br>      return a + b + c;<br>    };<br>  };<br>}<br><br>console.log(curriedAdd(1)(2)(3)); // 6<br></pre><p>Here, instead of passing all arguments at once, we supply them one by one. Each function “remembers” the previous argument (thanks to closures) until all are provided.</p><p>💡<strong>Think of currying as splitting one big bite into smaller, digestible bites.</strong></p><h3>Why Do We Use Currying?</h3><p>Currying is not just a syntactic trick — it gives us flexibility and reusability when working with functions. The two main benefits are:</p><h4>1. Partial Application</h4><p>Instead of passing all arguments at once, we can “lock in” some arguments and reuse the function later.</p><pre>function multiply(a) {<br>  return function(b) {<br>    return a * b;<br>  };<br>}<br><br>const double = multiply(2);  <br>console.log(double(5)); // 10  <br>console.log(double(7)); // 14</pre><p>Here, we partially applied a = 2 to create a reusable double function.</p><h4>2. Function Composition</h4><p>Currying makes it easier to chain functions together, where the output of one becomes the input of the next.</p><p>Before diving into the curried version of function composition, let’s look at how <strong>composition usually works</strong>.</p><h4>1. Normal Function Composition</h4><p>Normally, composition chains functions together like this:</p><pre>const add = (a, b) =&gt; a + b;<br>const multiply = (a, b) =&gt; a * b;<br>const square = (x) =&gt; x * x;<br><br>// Without currying<br>const composed = (a, b, c) =&gt; square(multiply(add(a, b), c));<br><br>console.log(composed(2, 3, 4)); // 400</pre><p>✅ Works fine, but notice:</p><ul><li>We had to <strong>manually wire the order</strong> (square(multiply(add(...)))).</li><li>It looks <strong>nested and less readable</strong>.</li><li>Not very flexible if the number of functions grows.</li></ul><h4>2. Curried Composition</h4><p>With currying, we can <strong>pipe arguments step by step</strong>, making the flow clearer:</p><pre>function compose(...callbacks) {<br>  // first: Calculate how many arguments in total are needed<br>  let totalArgs = callbacks.reduce((acc, curr) =&gt; acc + curr.length, 0);<br>  // Second: Adjust arguments because after each call we &quot;compress&quot; arguments into 1<br>  let finalArgs = totalArgs - (callbacks.length - 1);<br><br>  // Third: Return a curried function<br>  return function curried(...args) {<br>    // Case 1: When enough arguments are provided<br>    if (args.length &gt;= finalArgs) {<br>      callbacks.forEach((callback) =&gt; {<br>        let currentArgs = args.slice(0, callback.length); // Pick arguments needed for this callback<br>        let res = callback(...currentArgs);               // Call function<br>        args.splice(0, callback.length, res);             // Replace used args with result<br>      });<br>      return args[0]; // Final result<br>    }<br><br>    // Case 2: Not enough args yet → return another function<br>    return function (...nextArgs) {<br>      return curried(...args, ...nextArgs);<br>    };<br>  };<br>}<br><br>const add = (a, b) =&gt; a + b;<br>const multiply = (a, b) =&gt; a * b;<br>const square = (x) =&gt; x * x;<br><br>const composed = compose(add, multiply, square);<br><br>// Using currying step by step<br>console.log(composed(2)(3)(4)); // ((2+3) * 4)^2 = 400<br><br>// Or all at once<br>console.log(composed(2, 3, 4)); // 400</pre><h4>Let’s See How It Works 🚀</h4><pre>compose(2, 3, 4)  <br>   → add(2, 3) = 5  <br>   → multiply(5, 4) = 20  <br>   → square(20) = 400</pre><p>The magic? Each function hands its result to the next one — like passing a baton in a relay race.</p><p>✅ Advantages:</p><ol><li>No nesting → execution order is natural: add → multiply → square.</li><li>More <strong>flexible</strong> → call all at once or one step at a time.</li><li>Scales easily → just pass more functions into compose.</li></ol><p>In other words:</p><ul><li><strong>Normal composition</strong> = nesting calls manually.</li><li><strong>Curried composition</strong> = building a reusable pipeline where arguments flow automatically.</li></ul><p>After <em>Function Composition</em>, the natural next topic is <strong>Partial Application</strong>.</p><h3>3. Partial Application</h3><p>Partial application is the process of <strong>fixing some arguments of a function</strong> now, and creating a new function that takes the remaining arguments later.</p><p>It’s like pre-filling part of a function, so you don’t have to repeat those arguments every time.</p><pre>function multiply(a) {<br>  return function (b) {<br>    return a * b;<br>  };<br>}<br><br>const multiplyByFour = multiply(4);<br><br>console.log(multiplyByFour(10)); // 40<br>console.log(multiplyByFour(4));  // 16</pre><p>✅ Here’s what happens:</p><ol><li>multiply(4) locks the first argument a = 4.</li><li>It returns a new function that takes the next argument b.</li><li>When called (multiplyByFour(10)), it computes 4 * 10.</li></ol><p>This is exactly <strong>partial application</strong> in action — fixing one argument now and reusing it later.</p><p>💡 <strong>Why is this useful?</strong></p><ul><li>Removes repetition</li><li>Creates reusable, specialized functions</li><li>Helps in cleaner and more modular code</li></ul><h3>️ 4. Currying vs Partial Application</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ZX1cNBw_QhfXpDuI4PznQ.png" /><figcaption>Comparison between Currying and Partial Application</figcaption></figure><h3>5. Real-World Example: Processing Orders in an E-Commerce App</h3><p>Imagine you have an order object and you want to:</p><ol><li>Apply a discount</li><li>Calculate tax</li><li>Format the final price</li></ol><p>We can use <strong>partial application</strong>, <strong>currying</strong>, and <strong>composition</strong> to make this clean and reusable.</p><p><strong>Step 1</strong>: Curried functions</p><pre>function applyDiscount(discount) {<br>  return function(order) {<br>    order.price = order.price * (1 - discount);<br>    return order;<br>  };<br>}<br><br>function applyTax(taxRate) {<br>  return function(order) {<br>    order.price = order.price * (1 + taxRate);<br>    return order;<br>  };<br>}<br><br>function formatPrice(order) {<br>  order.price = &#39;$&#39; + order.price.toFixed(2);<br>  return order;<br>}</pre><ul><li>applyDiscount and applyTax are <strong>curried</strong>: they take one argument first and return a function that processes the order.</li><li>formatPrice formats the price nicely.</li></ul><p><strong>Step 2</strong>: Partial application</p><pre>// Pre-fill discount and tax values<br>const apply10PercentDiscount = applyDiscount(0.1);<br>const apply8PercentTax = applyTax(0.08);</pre><ul><li>We <strong>partially applied</strong> the discount and tax rates.</li><li>Now apply10PercentDiscount and apply8PercentTax are ready-to-use functions that just need an order.</li></ul><p><strong>Step 3</strong>: Compose functions</p><pre>function compose(functions) {<br>  return function(order) {<br>    var result = order;<br>    for (var i = 0; i &lt; functions.length; i++) {<br>      result = functions[i](result);<br>    }<br>    return result;<br>  };<br>}</pre><ul><li>compose takes an array of functions and executes them <strong>in order</strong>, passing the updated order each time.</li></ul><p><strong>Step 4</strong>: Build the pipeline</p><pre>const processOrder = compose([<br>  apply10PercentDiscount,<br>  apply8PercentTax,<br>  formatPrice<br>]);</pre><p><strong>Step 5</strong>: Use the pipeline</p><pre>const order = { id: 1, price: 100 };<br>const finalOrder = processOrder(order);<br>console.log(finalOrder); // { id: 1, price: &quot;$97.20&quot; }</pre><h3>Step-by-Step Explanation:</h3><ol><li>apply10PercentDiscount(order) → price becomes 100 * 0.9 = 90</li><li>apply8PercentTax(order) → price becomes 90 * 1.08 = 97.2</li><li>formatPrice(order) → price becomes &quot;$97.20&quot;</li></ol><p>✅ All functions are <strong>small, reusable, and composable</strong>.</p><h3>Summary</h3><ul><li><strong>Currying</strong>: Breaks a function into nested single-argument calls.</li><li><strong>Partial application</strong>: Pre-fills some arguments to create reusable functions.</li><li><strong>Composition</strong>: Chains functions together cleanly, especially with currying.</li></ul><p>That’s all, folks! 🎉<br> If you found this helpful, <strong>give it a clap</strong> 👏 and share your <strong>feedback or suggestions</strong> — they mean a lot!</p><p>Happy coding! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a94f24606bff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>