pages tagged functional_programming http://meng6net.localhost/tag/functional_programming/ <p><small>Copyright © 2005-2020 by <code>Meng Lu &lt;lumeng3@gmail.com&gt;</code></small></p> Meng Lu's home page ikiwiki Tue, 16 May 2017 23:59:39 +0000 Munging data streams with functional programming and lambda expressions in Java 8 http://meng6net.localhost/blog/munging_data_streams_with_functional_programming_and_lambda_expressions_in_java/ http://meng6net.localhost/blog/munging_data_streams_with_functional_programming_and_lambda_expressions_in_java/ computing functional programming java note programming stream Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <p>With the advent of Java 8 and <a href= "http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html"> <code>java.util.stream</code></a> and <a href= "http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html"> lambda expressions</a> in it, one can do data munging in Java 8 as the following:</p> <p>Java 8:</p> <pre><code>public static void main(String[] args) { HashMap&lt;Integer, Character&gt; nucleobases = new HashMap&lt;&gt; (); nucleobases.put(1, 'A'); nucleobases.put(2, 'G'); nucleobases.put(3, 'T'); nucleobases.put(4, 'C'); range(0, 100) // generate a stream containing random strings of length 10 .mapToObj(i -&gt; randomNucleicAcidSequence(new Random(), nucleobases, 10)) // sort the elements in the stream to natural ordering .sorted() // group strings into sub-lists and wrap them into a stream .collect(groupingBy(name -&gt; name.charAt(0))) // print each sub-list's common initial letter and the constituent strings .forEach((letter, names) -&gt; System.out.println(letter + "\n\t" + names.stream().collect(joining("\n\t")))); } public static String randomNucleicAcidSequence(Random r, Map&lt;Integer, Character&gt; map, int length) { return r.ints(1, 4).limit(length).mapToObj( x -&gt; Character.toString(map.get(x))).collect(Collectors.joining()); } </code></pre> <p><a href= "https://github.com/lumeng/repogit-java-stream-basic-example">↑ Code in GitHub</a></p> <p>This is remarkbly similar to a program written in Mathematica using the <a href= "http://meng6net.localhost/computing/munging_style_for_mathematica_code_layout">munging style</a> I use all the time:</p> <pre><code>(* There is not a built-in RandomString[…] *) nucleobases = {"A", "G", "T", "C"}; randomNucleicAcidSequence[length_Integer] := StringJoin[nucleobases[[#]] &amp; /@ RandomInteger[{1, 4}, length]] Composition[ Print[StringJoin[{#[[1]],":\n",StringJoin[Riffle[#[[2]],"\n"]]}]]&amp; /@ # &amp;, (First[Characters[Part[#, 1]]]-&gt; #) &amp; /@ # &amp;, GatherBy[#, First[Characters[#]]&amp;]&amp;, Sort, Map[Function[{i}, randomNucleicAcidSequence[10]], #] &amp; ][Range[0, 100]] </code></pre> <p>The output of both program print the mucleic acid sequences grouped by initial neuclobase:</p> <pre><code>A: AAAATACCTC AAATAATCAT AACAGATACG ACAACTACGG ACCATCAAAT ... C: CAACGGGGTT CAAGAAGAGC CACACCCACA CACACTCTAC CAGGACCGGA ... G: GAACGTTTCA GAACTAAGCG GACCAGTTCT GAGAACACGT GAGCCGCCAC ... T: TAAAATTGCC TAAGGTGAGG TAGCCGGTTA TAGGCGGTGA TAGTTCGAGC ... </code></pre> <p>Data streams and <a href= "https://en.wikipedia.org/wiki/Streaming_algorithm">algorithms</a> for processing them is a recently hot research area in computer science. It seems to me it will be natural for Java standard library to include more and more stream algorithms in future.</p> <p><big>Related</big>:</p> <ul> <li><a href= "http://meng6net.localhost/tag/computing/munging_style_for_mathematica_code_layout/">Munging style for Mathematica code layout</a></li> </ul> /blog/munging_data_streams_with_functional_programming_and_lambda_expressions_in_java/#comments