functional programming in java 8 - exploiting lambdas
TRANSCRIPT
Functional Programming in Java 8 Lambda Functions
GANESH & HARI CODEOPS TECHNOLOGIES
[email protected]@codeops.tech
Adapt: Learn functional programming
Java meets functional programming (with lambdas)
Java is not your grandma’s language anymore!
Greek characters are scary!
He he, but lambdas are fun, not scary
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");Consumer<String> printString = string -> System.out.println(string); strings.forEach(printString);
Lambda functions!
But what are lambdas?
Lambdas is just a fancy name for functions
without a name!
What are lambda functions?
❖ (Java 8) One way to think about lambdas is “anonymous function” or “unnamed function” - they are functions without a name and are not associated with any class
❖ They don’t change external state
What is functional programming?
❖ Functional languages view programs as an entity—called a function—that accepts inputs and produces output
❖ Functions are connected together by their outputs to other functions’ inputs
❖ Underlying approach: “Evaluate an expression. Then use the results for something else.”
Perspective - for loops!List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");for(String string : strings) {
System.out.println(string);}
External Iteration
Perspective - for loops!
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");strings.forEach(string -> System.out.println(string));
Internal Iteration
Perspective - for loops!
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");strings.forEach(string -> System.out.println(string));
Internal Iteration
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");for(String string : strings) {
System.out.println(string);}
External Iteration
Perspective - for loops!
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");strings.forEach(string -> System.out.println(string));
Internal Iteration
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");for(String string : strings) {
System.out.println(string);}
External Iteration
Procedural thinking
Functional thinking
You can use lambdas for some amazing stuff
sediment
pre-carbon
ultra-filter
post-carbon
Filtered water
E.g., you can compose lambda functions as in pipes-and-filters
$ cat limerick.txt There was a young lady of Niger Who smiled as she rode on a tiger. They returned from the ride With the lady inside And a smile on the face of the tiger.
$ cat limerick.txt | tr -cs "[:alpha:]" "\n" | awk '{print length(), $0}' | sort | uniq
1 a 2 as 2 of 2 on 3 And 3 Who 3 she 3 the 3 was 4 They 4 With 4 face 4 from 4 lady 4 ride 4 rode 5 Niger 5 There 5 smile 5 tiger 5 young 6 inside 6 smiled 8 returned
List<String> lines = Files.readAllLines(Paths.get("./limerick.txt"), Charset.defaultCharset());
Map<Integer, List<String>> wordGroups = lines.stream() .map(line -> line.replaceAll("\\W", "\n").split("\n")) .flatMap(Arrays::stream) .sorted() .distinct() .collect(Collectors.groupingBy(String::length));
wordGroups.forEach( (count, words) -> { words.forEach(word -> System.out.printf("%d %s %n", count, word)); });
1 a 2 as 2 of 2 on 3 And 3 Who 3 she 3 the 3 was 4 They 4 With 4 face 4 from 4 lady 4 ride 4 rode 5 Niger 5 There 5 smile 5 tiger 5 young 6 inside 6 smiled 8 returned
Lambdas & streams help in productive programming!
public static void main(String []file) throws Exception { // process each file passed as argument
// try opening the file with FileReader try (FileReader inputFile = new FileReader(file[0])) { int ch = 0; while( (ch = inputFile.read()) != -1) { // ch is of type int - convert it back to char System.out.print( (char)ch ); } } // try-with-resources will automatically release FileReader object }
public static void main(String []file) throws Exception { Files.lines(Paths.get(file[0])).forEach(System.out::println); }
Existing APIs are enriched with lambdas and streams support
Java 8 is the new Groovy ;-)importjava.io.*;
classType{ publicsta7cvoidmain(String[]files){ //processeachfilepassedasargument for(Stringfile:files){ //tryopeningthefilewithFileReader try(FileReaderinputFile=newFileReader(file)){ intch=0; while((ch=inputFile.read())!=-1){ //chisoftypeint-convertitbacktochar System.out.print((char)ch); } }catch(FileNotFoundExcep7onfnfe){ System.err.prinR("Cannotopenthegivenfile%s",file); } catch(IOExcep7onioe){ System.err.prinR("Errorwhenprocessingfile%s;skippingit",file); } //try-with-resourceswillautoma7callyreleaseFileReaderobject } }}
args.each{printlnnewFile(it).getText()}
Lambdas in Java 8
Java 8 lambdas - “Hello world!”
interface LambdaFunction { void call(); }
class FirstLambda { public static void main(String []args) { LambdaFunction lambdaFunction = () -> System.out.println("Hello world"); lambdaFunction.call(); } }
Java 8 lambdas - “Hello world!”
interface LambdaFunction { void call(); }
class FirstLambda { public static void main(String []args) { LambdaFunction lambdaFunction = () -> System.out.println("Hello world"); lambdaFunction.call(); } }
Functional interface - provides signature for lambda functions
Lambda function/expression
Call to the lambda
Prints “Hello world” on the console when executed
Parts of a lambda expression
() -> System.out.println("Hello world");
No parameters, i.e., ()
Arrow operator that separates parameters and the body The lambda body
Return type “void” inferred from the body
Functional interfaces
@FunctionalInterface interface LambdaFunction { void call(); }
Functional interface
Abstract method providing the signature of the lambda function
Annotation to explicitly state that it is a functional interface
Using built-in functional interfaces// within Iterable interface default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t);
} }
// in java.util.function package @FunctionalInterface public interface Consumer<T> {
void accept(T t); // the default andThen method elided
}
Using built-in functional interfaces
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); Consumer<String> printString = string -> System.out.println(string); strings.forEach(printString);
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); strings.forEach(string -> System.out.println(string));
Method references
Method references - “syntactic sugar” for lambda functions
They “route” function parameters
arg -> System.out.println(arg)
System.out::println
Method references
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); Consumer<String> printString = System.out::println; strings.forEach(printString);
Method reference
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); Consumer<String> printString = string -> System.out.println(string); strings.forEach(printString);
Method references
Cannot use method references when lambda functions do more than“routing” function parameters
strings.forEach(string -> System.out.println(string.toUpperCase()));
More processing here than just “routing” parameters
Method references List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); Consumer<String> printString = System.out::println; strings.forEach(printString);
public static void printUpperCaseString(String string) { System.out.println(string.toUpperCase()); }
strings.forEach(MethodReference::printUpperCaseString);
Java 8 streams (and parallel streams):Excellent example of applying functional
programming in practice
But what are streams?
Arrays.stream(Object.class.getMethods()) .map(method -> method.getName()) .distinct() .forEach(System.out::println);
wait equals toString hashCode getClass notify notifyAll
Method[] objectMethods = Object.class.getMethods(); Stream<Method> objectMethodStream = Arrays.stream(objectMethods); Stream<String> objectMethodNames
= objectMethodStream.map(method -> method.getName()); Stream<String> uniqueObjectMethodNames = objectMethodNames.distinct(); uniqueObjectMethodNames.forEach(System.out::println);
Arrays.stream(Object.class.getMethods()) .map(method -> method.getName()) .distinct() .forEach(System.out::println);
Breaking up into separate (looong) statements for our understanding
stream pipelineStreamsource
Intermediateopera1ons
Terminalopera1on
stream
stream
Examples:IntStream.range(),Arrays.stream()
Examples:map(),filter(),dis1nct(),sorted()
Examples:sum(),collect(),forEach(),reduce()
DoubleStream.of(1.0,4.0,9.0) map(Math::sqrt) .peek(System.out::
println)
StreamSource(withelements1.0,4.0,and9.0)
IntermediateOpera=on1(mapsto
elementvalues1.0,2.0,and3.0)
IntermediateOpera=on2
(prints1.0,2.0,and3.0)
.sum();
TerminalOpera=on(returnsthesum6.0)
DoubleStream.of(1.0, 4.0, 9.0) .map(Math::sqrt) .peek(System.out::println) .sum();
IntStream.range(1, 6)
You can use range or iterate factory methods in the
IntStream interface
IntStream.iterate(1, i -> i + 1).limit(5)
1 2 3 4 5
1 4 9 16 25
map(i->i*i)
IntStream.range(1, 5).map(i -> i * i).forEach(System.out::println);
Using streams instead of imperative for i = 1 to 5, print i * i
Stream.of (1, 2, 3, 4, 5) .map(i -> i * i) .peek(i -> System.out.printf("%d ", i)) .count();
prints: 1 4 9 16 25
stream can be infinite
IntStream.iterate(0, i -> i + 2).forEach(System.out::println);
This code creates infinite stream of even numbers!
IntStream .iterate(0, i -> i + 2) .limit(5) .forEach(System.out::println);
Using the “limit” function to limit the stream to 5 integers
IntStream chars = "bookkeep".chars(); System.out.println(chars.count()); chars.distinct().sorted().forEach(ch -> System.out.printf("%c ", ch));
Cannot “reuse” a stream; this code throws IllegalStateException
Files.lines(Paths.get("FileRead.java")).forEach(System.out::println);
This code prints the contents of the file “FileRead.java” in the
current directory
Pattern.compile(" ").splitAsStream("java 8 streams").forEach(System.out::println);
This code splits the input string “java 8 streams” based on whitespace and hence
prints the strings “java”, “8”, and “streams” on the console
new Random().ints().limit(5).forEach(System.out::println);
Generates 5 random integers and prints them on the console
"hello".chars().sorted().forEach(ch -> System.out.printf("%c ", ch));
Extracts characters in the string “hello”, sorts the chars and prints the chars
Parallel Streams
race conditions
deadlocks
I really really hate concurrency problems
Parallel code
Serial code
long numOfPrimes = LongStream.rangeClosed(2, 100_000) .filter(PrimeNumbers::isPrime) .count();
System.out.println(numOfPrimes);
Prints 9592
2.510 seconds
Parallel code
Serial code
Let’s flip the switch by calling parallel() function
long numOfPrimes = LongStream.rangeClosed(2, 100_000) .parallel() .filter(PrimeNumbers::isPrime) .count();
System.out.println(numOfPrimes);
Prints 9592
1.235 seconds
Wow! That’s an awesome flip switch!
Internally, parallel streams make use of fork-join framework
import java.util.Arrays;
class StringConcatenator { public static String result = ""; public static void concatStr(String str) { result = result + " " + str; } }
class StringSplitAndConcatenate { public static void main(String []args) { String words[] = "the quick brown fox jumps over the lazy dog".split(" "); Arrays.stream(words).forEach(StringConcatenator::concatStr); System.out.println(StringConcatenator.result); } }
Gives wrong results with with parallel() call
Adapt: Learn functional programming
Upcoming bootcamps/workshops
Modern Software Architecture - July 2 Modern Programming with Java 8 - July 16 Software Refactoring in Practice - July 23
Image credits❖ http://mayhemandmuse.com/wp-content/uploads/2013/04/This-optical-illusion-drawing-by-WE-
Hill-shows-both-his-wife-and-his-mother-in-law.jpg
❖ http://www.webtrafficroi.com/wp-content/uploads/2012/10/mahatma-gandhi-apple-think-different.jpg
❖ http://rexx-language-association-forum.44760.x6.nabble.com/file/n2236/Ruby-lambda-function.jpg
❖ http://www.ibm.com/developerworks/library/j-jn16/figure1.png
❖ http://www.ibm.com/developerworks/library/j-jn16/figure2.png
❖ http://img.viralpatel.net/2014/01/java-lambda-expression.png
❖ http://www.codercaste.com/wp-content/uploads/2011/01/animals.png
❖ http://blog.takipi.com/wp-content/uploads/2014/03/blog_lambada_2.png
❖ http://quotespictures.com/wp-content/uploads/2014/01/it-is-not-the-strongest-of-the-species-that-survive-nor-the-most-intelligent-but-the-one-most-responsive-to-change-charles-darwin.jpg
❖ http://7-themes.com/data_images/out/27/6859733-surfing-wallpaper.jpg
[email protected] @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/sgganesh