Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix composable function typos. Clarify wording. #3252

Merged
merged 7 commits into from
Mar 19, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,16 @@ <h3 id="Difference_between_reduce_and_reduceRight">Difference between <code>redu
console.log(left); // "12345"
console.log(right); // "54321"</pre>

<h3 id="Defining_Composible_Function">Defining Composible Function</h3>

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

<pre class="brush: js">/**
 * Function Composition is way in which result of one function can
 * be passed to another and so on.
 *
 * h(x) = f(g(x))
 *
 * Function execution happens right to left
 *
 * https://en.wikipedia.org/wiki/Function_composition
 */

const compose = (...args) =&gt; (value) =&gt; args.reduceRight((acc, fn) =&gt; fn(acc), value)
<h3>Defining composable functions</h3>

<p>Function composition is a mechanism for combining functions, in which the
output of each function is passed into the next one, and the output of the last
function is the final result. In this example we use <code>reduceRight()</code>
to implement function composition.</p>

<p>See also <a href="https://en.wikipedia.org/wiki/Function_composition_(computer_science)">Function composition</a> on Wikipedia.

<pre class="brush: js">const compose = (...args) =&gt; (value) =&gt; args.reduceRight((acc, fn) =&gt; fn(acc), value)

// Increment passed number
const inc = (n) =&gt; n + 1
Expand Down