{"id":61262,"date":"2024-04-18T10:48:21","date_gmt":"2024-04-18T05:18:21","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=61262"},"modified":"2024-04-22T11:20:12","modified_gmt":"2024-04-22T05:50:12","slug":"is-currying-in-javascript-a-chain-of-functions","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/is-currying-in-javascript-a-chain-of-functions\/","title":{"rendered":"Is Currying in JavaScript, A Chain of Functions?"},"content":{"rendered":"<p>In this article, we are going to discuss &#8220;<strong>Is Currying in JavaScript a Chain of Functions?<\/strong>&#8220;. Here, we will see how currying works like a chain of functions and how it will be useful for developers. We will also see the conversion of an existing function into the current version. Let&#8217;s start our topic, i.e., Is Currying in JavaScript a Chain of Functions?<\/p>\n<p><strong>Yes, Currying in JavaScript is a chain of functions.<\/strong> Currying is an advanced technique commonly used in functional programming that allows you to transform a function with multiple arguments into a sequence of functions, each taking only one argument at a time.<\/p>\n<pre><code><strong>log(a,b,c) =&gt; log(a)(b)(c)<\/strong><\/code><\/pre>\n<p>A functional programming technique involves breaking down a function that takes multiple arguments into a series of smaller functions, each taking one argument. This creates a chain of functions, where each function returns another function until the final result is achieved.<\/p>\n<p>Currying in JavaScript is named after the mathematician <strong>Haskell Curry<\/strong>, who introduced it in the 1930s. Some say that currying calls a function, but it doesn\u2019t. Currying doesn\u2019t call a function. It just transforms it. It\u2019s also used in other languages as well. Let&#8217;s start with the code, later on, will be discussing the advantages.<\/p>\n<h3><strong>Starting with the standard example of an\u00a0addition\/sum function,<\/strong><\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-61258 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.39.18-AM-2.png\" alt=\"\" width=\"1008\" height=\"212\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.39.18-AM-2.png 1008w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.39.18-AM-2-300x63.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.39.18-AM-2-768x162.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.39.18-AM-2-624x131.png 624w\" sizes=\"(max-width: 1008px) 100vw, 1008px\" \/><\/p>\n<h3><strong>Converting it into a currying function,<\/strong><\/h3>\n<p><code><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61259 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.54.21-AM-1024x699.png\" alt=\"\" width=\"625\" height=\"427\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.54.21-AM-1024x699.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.54.21-AM-300x205.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.54.21-AM-768x524.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.54.21-AM-1536x1049.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.54.21-AM-2048x1399.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-1.54.21-AM-624x426.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><br \/>\n<\/code><\/p>\n<p id=\"ember53\" class=\"ember-view reader-content-blocks__paragraph\">In this curried version of the <strong>addition<\/strong> function, each function takes one argument and returns another function until all the arguments are collected and the final sum is calculated.<\/p>\n<p id=\"ember54\" class=\"ember-view reader-content-blocks__paragraph\">Here&#8217;s how the currying works step by step:<\/p>\n<ol>\n<li><strong>addition(1)<\/strong> returns a function that takes the second number.<\/li>\n<li><strong>addition(1)(2) \/ sumResult1(3) <\/strong>returns a function that takes the third number.<\/li>\n<li><strong>addition(1)(2)(3)<\/strong> returns the final result, which is the sum of all three numbers.<\/li>\n<\/ol>\n<p id=\"ember56\" class=\"ember-view reader-content-blocks__paragraph\">This way, you can call the curried <strong>addition<\/strong> function with each argument separately, creating a more modular and composable function. We can reuse this function throughout our codebase without duplicating the logic. Another advantage of currying is the ability to create higher-order functions that enhance composability.<\/p>\n<h2 id=\"ember61\" class=\"ember-view\">Infinite Currying?<\/h2>\n<p>Via using some of the recursive nature techniques, we can convert the function code to have an infinite argument. This allows you to create an infinite currying chain, where you can keep adding numbers as needed, and when you&#8217;re ready to get the final result, you just need to call the curried function with no arguments to compute the sum.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-61261 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-2.38.18-AM-1024x751.png\" alt=\"\" width=\"625\" height=\"458\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-2.38.18-AM-1024x751.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-2.38.18-AM-300x220.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-2.38.18-AM-768x563.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-2.38.18-AM-1536x1126.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-2.38.18-AM-2048x1502.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/04\/Screenshot-2024-04-12-at-2.38.18-AM-624x458.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2>Let&#8217;s dry-run it:<\/h2>\n<ol>\n<li>The <strong>addition<\/strong> function is defined to accept a single argument <strong>val1<\/strong>.<\/li>\n<li>Inside the <strong>addition<\/strong>\u00a0function, an inner function is returned. This inner function takes an argument <strong>val2<\/strong>.<\/li>\n<li>If <strong>val2<\/strong> is provided (i.e., it&#8217;s truthy), the inner function returns a recursive call to <strong>addition<\/strong>\u00a0with the sum of <strong>val1<\/strong> and <strong>val2<\/strong>.<\/li>\n<li>If <strong>val2<\/strong> is not provided (i.e., it&#8217;s falsy, like <strong>undefined<\/strong> or <strong>0<\/strong>), the inner function returns the accumulated <strong>val1<\/strong>.<\/li>\n<li>When the curried <strong>addition<\/strong>\u00a0function is called, it can be immediately invoked with the next argument, and this process can be repeated for as many arguments as needed.<\/li>\n<li>The final invocation of the curried <strong>addition<\/strong>\u00a0function is done with an empty set of parentheses <strong>()<\/strong>. This results in computing the accumulated sum of all previously provided numbers.<\/li>\n<li>The result is <strong>3<\/strong>, which is the sum of <strong>1<\/strong> and <strong>2<\/strong>.<\/li>\n<\/ol>\n<h2>Summary<\/h2>\n<p><em>Currying<\/em>\u00a0is a transform that makes\u00a0<code>f(a,b,c)<\/code>\u00a0callable as\u00a0<code>f(a)(b)(c)<\/code>. JavaScript implementations usually keep the function callable normally and return the partial if the arguments count is insufficient.<\/p>\n<p>Currying allows us to get the partials easily. As we\u2019ve seen in the <strong>addition()<\/strong>\u00a0 example, after currying the three arguments function <code>addition(val1, val2, val3)<\/code>\u00a0gives us partials when called with one argument (like\u00a0<code>addition(val1)<\/code>) or two arguments (like\u00a0<code>addition(val1, val2)<\/code>).<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to discuss &#8220;Is Currying in JavaScript a Chain of Functions?&#8220;. Here, we will see how currying works like a chain of functions and how it will be useful for developers. We will also see the conversion of an existing function into the current version. Let&#8217;s start our topic, i.e., [&hellip;]<\/p>\n","protected":false},"author":1210,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":47},"categories":[1439,4684,1185,3038,4670],"tags":[5820],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61262"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1210"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=61262"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61262\/revisions"}],"predecessor-version":[{"id":61382,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/61262\/revisions\/61382"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=61262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=61262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=61262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}