<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mariano</title>
    <description>The latest articles on DEV Community by Mariano (@msosto).</description>
    <link>https://dev.to/msosto</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F21723%2F3d0b5110-2f7f-4077-9715-8ad23aaa5fc4.jpeg</url>
      <title>DEV Community: Mariano</title>
      <link>https://dev.to/msosto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/msosto"/>
    <language>en</language>
    <item>
      <title>Java: Nesting functions with Stream Reduce</title>
      <dc:creator>Mariano</dc:creator>
      <pubDate>Thu, 18 Apr 2019 02:56:20 +0000</pubDate>
      <link>https://dev.to/msosto/java-nesting-functions-with-stream-reduce-4bgk</link>
      <guid>https://dev.to/msosto/java-nesting-functions-with-stream-reduce-4bgk</guid>
      <description>

&lt;h1&gt;
  
  
  Java: Nesting Functions with Stream Reduce
&lt;/h1&gt;

&lt;p&gt;One of the less known stream operators is &lt;em&gt;Reduce&lt;/em&gt;. I've studyied Haskell during University and &lt;em&gt;reduce&lt;/em&gt; strongly reminds of Haskell's function &lt;em&gt;&lt;a href="https://wiki.haskell.org/Fold"&gt;fold&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is &lt;em&gt;reduce&lt;/em&gt; ?
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Given a list of elements and a function (aka &lt;strong&gt;accumulator&lt;/strong&gt;), get a single resulting value.&lt;/em&gt;&lt;br&gt;
The easy example: make the sum of a list of Integers.&lt;br&gt;
&lt;code&gt;Integer result = Stream.of(1, 2, 3, 4, 5).reduce(0, (a, b) -&amp;gt; a + b);&lt;/code&gt;&lt;br&gt;
That will make: &lt;strong&gt;1+2+3+4+5+0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;However, Streams api already solves that:&lt;br&gt;
&lt;code&gt;Integer result = IntStream.of(1, 2, 3, 4, 5).sum();&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Composite functions with Reduce
&lt;/h1&gt;

&lt;p&gt;Suppose we have a list of functions and we want to build a function which contains all the ones from the list. Let see:&lt;br&gt;
First we have:&lt;br&gt;
&lt;code&gt;List&amp;lt;Function&amp;gt; functions = [f1,f2,f3,f4,f5]&lt;/code&gt;&lt;br&gt;
And we want a function like this:&lt;br&gt;
&lt;code&gt;Function resultFunction = (X) -&amp;gt; f5( f4( f3 ( f2( f1(X) ) ) )&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How can we easily build a composite function ?
&lt;/h2&gt;

&lt;p&gt;First, we'll use &lt;em&gt;Function::andThen&lt;/em&gt; method to wrap each function in another.&lt;br&gt;
Then, we call &lt;em&gt;reduce&lt;/em&gt; and the first argument is the &lt;em&gt;starting value&lt;/em&gt;, as we are composing functions we use the &lt;strong&gt;idempotent&lt;/strong&gt; element which, in this case, is the &lt;a href="a%20-&amp;gt;%20a"&gt;Identity Function&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; List&amp;lt;Function&amp;gt; functions = [f1,f2,f3,f4,f5];
 Function compositeFunction = functions.stream().reduce(a -&amp;gt; a, Function::andThen)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;What if we want to composite in the reverse order of the list ?&lt;/strong&gt;&lt;br&gt;
Remember the array is:&lt;br&gt;
&lt;code&gt;List&amp;lt;Function&amp;gt; functions = [f1,f2,f3,f4,f5]&lt;/code&gt;&lt;br&gt;
And now we need a function like this:&lt;br&gt;
&lt;code&gt;Function resultFunction = (X) -&amp;gt; f1( f2( f3 ( f4( f5(X) ) ) )&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have to change the &lt;em&gt;accumulator&lt;/em&gt; to Function::compose&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; List&amp;lt;Function&amp;gt; functions = [f1,f2,f3,f4,f5];
 Function compositeFunction = functions.stream().reduce(a -&amp;gt; a, Function::compose)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Altogether &lt;em&gt;Reduce&lt;/em&gt; , &lt;em&gt;Function::compose&lt;/em&gt; and &lt;em&gt;Function::andThen&lt;/em&gt; conceive a powerfull tool to compose functions which is of great use.&lt;/p&gt;


</description>
      <category>functional</category>
      <category>java</category>
    </item>
  </channel>
</rss>
