SlideShare uma empresa Scribd logo
1 de 67
Baixar para ler offline
Lets Get Funky with Functional Progamming
Ganesh Samarthyam
ganesh@konfhub.com
Adapt: Learn functional
programming
Programming paradigms
Langauge
Paradigm
Declarative
Languages
Imperative
Languages
Logic
Languages
Functional
Languages
Procedural
Languages
Object-Oriented
Languages
e.g. Prolog e.g. Haskell e.g. C e.g. Java
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
Lambdas
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");
Consumer<String> printString = string -> System.out.println(string);
strings.forEach(printString);
Lambda
functions!
Lambdas
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");
Consumer<String> printString = string -> System.out.println(string);
strings.forEach(printString);
Capture in a
variable
Execute later
What are lambda functions?
❖ 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.”
Lambdas added in Java 8
Productive programming with lambdas
import java.io.*;
class Type {
public static void main(String []files) {
// process each file passed as argument
for(String file : files) {
// try opening the file with FileReader
try (FileReader inputFile = new FileReader(file)) {
int ch = 0;
while( (ch = inputFile.read()) != -1) {
// ch is of type int - convert it back to char
System.out.print( (char)ch );
}
} catch (FileNotFoundException fnfe) {
System.err.printf("Cannot open the given file %s ", file);
}
catch(IOException ioe) {
System.err.printf("Error when processing file %s; skipping it", file);
}
// try-with-resources will automatically release FileReader object
}
}
}
args.each { println new File(it).getText() }
Workshop overview
❖ Assumes that you already
know Java
❖ You’ll know how to use Java
lambdas and streams after this
session
❖ Try out the programs in your
machine
Lambdas in Java
Java 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 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
`
() -> 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);
“Effectively final” variables
import java.util.Arrays;
import java.util.List;
class PigLatin {
public static void main(String []args) {
String suffix = "ay";
List<String> strings = Arrays.asList("one", "two", "three", "four");
strings.forEach(string -> System.out.println(string + suffix));
}
}
Accessing “local variable” suffix
here; hence it is considered
“effectively final”
“Effectively final” variables
import java.util.Arrays;
import java.util.List;
class PigLatin {
public static void main(String []args) {
String suffix = "ay";
List<String> strings = Arrays.asList("one", "two", "three", “four");
suffix = "e"; // assign to suffix variable
strings.forEach(string -> System.out.println(string + suffix));
}
}
PigLatinAssign.java:9: error: local variables referenced from a
lambda expression must be final or effectively final
strings.forEach(string -> System.out.println(string + suffix));
^
1 error
Stream API
First stream example
"hello".chars().sorted().forEach(ch -> System.out.printf("%c ", ch));
The chars() method in String
results in a Stream; sorted() sorts
the entries in the stream
// prints e h l l o
Stream pipeline
Stream pipeline example
Arrays.stream(Object.class.getMethods()) // source
.map(method -> method.getName()) // intermediate op
.distinct() // intermediate op
.forEach(System.out::println); // terminal operation
Stream pipeline example
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);
One-liner #1
Files.lines(Paths.get("FileRead.java")).forEach(System.out::println);
This code prints the contents of
the file “FileRead.java” in the
current directory
One-liner #2
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
One-liner #3
new Random().ints().limit(5).forEach(System.out::println);
Generates 5 random integers and prints
them on the console
One-liner #4
"hello".chars().sorted().forEach(ch -> System.out.printf("%c ", ch));
Extracts characters in the string “hello”,
sorts the chars and prints the chars
Stream pipeline illustration
DoubleStream.of(1.0, 4.0, 9.0)
.map(Math::sqrt)
.peek(System.out::println)
.sum();
Stream sources
IntStream.range(1, 6)
You can use range or iterate
factory methods in the
IntStream interface
IntStream.iterate(1, i -> i + 1).limit(5)
Stream sources
Arrays.stream(new int[] {1, 2, 3, 4, 5})
Arrays.stream(new Integer[] {1, 2, 3, 4, 5})
You can use the stream() method in
java.util.Arrays class to create a
stream from a given array
Stream sources
Stream.of(1, 2, 3, 4, 5)
Stream.of(new Integer[]{1, 2, 3, 4, 5})
We can also create streams using factories
and builders (e..g, of() and build() methods
in the Stream interface)
Stream.builder().add(1).add(2).add(3).add(4).add(5).build()
Stream sources
‱ The lines() method in java.nio.file.Files class
‱ The splitAsStream() method in java.util.regex.Pattern class
‱ The ints() method in java.util.Random class
‱ The chars() method in java.lang.String class
There are numerous classes/interfaces in
the Java library that return a stream
Intermediate operations
Stream<T> filter(Predicate<? super T>
check)
Removes the elements for which the check predicate returns false.
<R> Stream<R> map(Function<? super T,?
extends R> transform)
Applies the transform() function for each of the elements in the
stream.
Stream<T> distinct()
Removes duplicate elements in the stream; it uses the equals()
method to determine if an element is repeated in the stream.
Stream<T> sorted()
Stream<T> sorted(Comparator<? super T>
compare)
Sorts the elements in its natural order. The overloaded version
takes a Comparator – you can pass a lambda function for that.
Stream<T> peek(Consumer<? super T>
consume)
Returns the same elements in the stream, but also executes the
passed consume lambda expression on the elements.
Stream<T> limit(long size)
Removes the elements if there are more elements than the given
size in the stream.
Terminal operations
void forEach(Consumer<?
super T> action)
Calls the action for every element in the
stream.
Object[] toArray()
Returns an Object array that has the elements
in the stream.
Optional<T> min(Comparator<?
super T> compare)
Returns the minimum value in the stream
(compares the objects using the given
compare function).
Optional<T>
max(Comparator<? super T>
compare)
Returns the maximum value in the stream
(compares the objects using the given
compare function).
long count() Returns the number of elements in the stream.
Using “range” instead of “for” loop
IntStream.range(1, 10).map(i -> i * i).forEach(System.out::println);
Using streams instead of imperative for i = 1 to 1, print i * i
Prints:
1
4
9
16
25
36
49
64
81
“Mapping” elements in a stream
Streams can be “infinite”
“Generating” even numbers
IntStream.iterate(0, i -> i + 2).forEach(System.out::println);
The problem is it creates infinite number of even numbers!
“Generating” limited 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
“Generating” limited even numbers
IntStream
.iterate(0, i -> i + 1)
.filter(i -> (i % 2) == 0)
.limit(5)
.forEach(System.out::println);
You can also use the “filter” method
“Reduction” using “sum” function
System.out.println(IntStream.rangeClosed(0, 10).sum());
Sum of integers from 1 to 10 using “implicit reduction”
“Reduction” using “sum” function
System.out.println(
IntStream
.rangeClosed(1, 5)
.reduce((x, y) -> (x * y))
.getAsInt());
Factorial of 5 using “explicit reduce”
Using “map” function
List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");
strings.stream().map(value -> value.toUpperCase()).forEach(System.out::println);
public static void printUpperCaseString(String string) {
System.out.println(string.toUpperCase());
}
strings.forEach(MethodReference::printUpperCaseString);
Using “collect” function
String boxedString =
Arrays
.asList("eeny", "meeny", "miny", "mo")
.stream()
.collect(Collectors.joining(“ ,", "[", "]"));
System.out.println(boxedString);
Prints: [eeny, meeny, miny, mo]
Using Files.list()
Files.list(Paths.get("."))
.map(path -> path.toAbsolutePath())
.forEach(System.out::println);
Built-in interfaces
Predicate<T> Checks a condition and returns a
boolean value as result
In filter() method in
java.util.stream.Stream which is
used to remove elements in the
stream that don’t match the given
condition (i.e., predicate) as
argument.
Consumer<T> Operation that takes an argument but
returns nothing
In forEach() method in
collections and in
java.util.stream.Stream; this
method is used for traversing all
the elements in the collection or
stream.
Function<T,
R>
Functions that take an argument and
return a result
In map() method in
java.util.stream.Stream to
transform or operate on the passed
value and return a result.
Supplier<T> Operation that returns a value to the
caller (the returned value could be
In generate() method in
java.util.stream.Stream to
Built-in interfaces
Streams are “lazy”
Cannot “reuse” a stream!
Important stream interfaces
map vs. flatMap methods
String limerick = "There was a young lady named Bright " +
"who traveled much faster than light " +
"She set out one day " +
"in a relative way " +
"and came back the previous night ";
IntSummaryStatistics wordStatistics =
Pattern.compile(" ")
.splitAsStream(limerick)
.mapToInt(word -> word.length())
.summaryStatistics();
System.out.printf(" Number of words = %d n Sum of the length of the words = %d n" +
" Minimum word size = %d n Maximum word size %d n " +
" Average word size = %f n", wordStatistics.getCount(),
wordStatistics.getSum(), wordStatistics.getMin(),
wordStatistics.getMax(), wordStatistics.getAverage());
Data calculation methods in stream
Final (longer) example
import java.util.stream.*;
import java.nio.file.*;
import java.util.*;
import java.nio.charset.Charset;
import java.io.IOException;
class ReadFile {
public static void main(String []args) throws IOException {
List<String> lines = Files.readAllLines(Paths.get("./Ulysses.txt"), Charset.def
Map<Integer, List<String>> wordGroups
= lines.parallelStream()
.map(line -> line.replaceAll("W", " ").split(" "))
.flatMap(Arrays::stream)
.filter(str -> str.length() > 7)
.distinct()
.sorted()
.collect(Collectors.groupingBy(String::length));
wordGroups.forEach( (count, words) -> {
System.out.printf("word(s) of length %d %n", count);
words.forEach(System.out::println); });
}
}
Lists words organised by
their length in the novel
“Ulysses.txt”
Ganesh Samarthyam
ganesh@konfhub.com

Mais conteĂșdo relacionado

Semelhante a Functional Thinking for Java Developers (presented in Javafest Bengaluru)

Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 WorkshopMario Fusco
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingHenri Tremblay
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIGanesh Samarthyam
 
Python basics
Python basicsPython basics
Python basicsHoang Nguyen
 
Python basics
Python basicsPython basics
Python basicsFraboni Ec
 
Python basics
Python basicsPython basics
Python basicsJames Wong
 
Python basics
Python basicsPython basics
Python basicsTony Nguyen
 
Python basics
Python basicsPython basics
Python basicsYoung Alista
 
Python basics
Python basicsPython basics
Python basicsHarry Potter
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8James Brown
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate JavaPhilip Johnson
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsashukiller7
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8franciscoortin
 
Buffer Overflows
Buffer OverflowsBuffer Overflows
Buffer OverflowsSumit Kumar
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new featuresAniket Thakur
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdfICADCMLTPC
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshotsrichambra
 

Semelhante a Functional Thinking for Java Developers (presented in Javafest Bengaluru) (20)

Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate Java
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Buffer Overflows
Buffer OverflowsBuffer Overflows
Buffer Overflows
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Sharable_Java_Python.pdf
Sharable_Java_Python.pdfSharable_Java_Python.pdf
Sharable_Java_Python.pdf
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
 

Mais de KonfHubTechConferenc

KonfHub Features, Benefits and Pricing
KonfHub Features, Benefits and Pricing KonfHub Features, Benefits and Pricing
KonfHub Features, Benefits and Pricing KonfHubTechConferenc
 
Azuga A Safety Company - Data Science Saving Lives
Azuga A Safety Company - Data Science Saving LivesAzuga A Safety Company - Data Science Saving Lives
Azuga A Safety Company - Data Science Saving LivesKonfHubTechConferenc
 
Self Supervised Learning for Vision Tasks (1).pdf
Self Supervised Learning for Vision Tasks (1).pdfSelf Supervised Learning for Vision Tasks (1).pdf
Self Supervised Learning for Vision Tasks (1).pdfKonfHubTechConferenc
 
Application of Artificial Intelligence for Automotive Applications
Application of Artificial Intelligence for Automotive ApplicationsApplication of Artificial Intelligence for Automotive Applications
Application of Artificial Intelligence for Automotive ApplicationsKonfHubTechConferenc
 
Are you ready for AI? Is AI ready for you?
Are you ready for AI? Is AI ready for you?Are you ready for AI? Is AI ready for you?
Are you ready for AI? Is AI ready for you?KonfHubTechConferenc
 
Exploring Generating AI with Diffusion Models
Exploring Generating AI with Diffusion ModelsExploring Generating AI with Diffusion Models
Exploring Generating AI with Diffusion ModelsKonfHubTechConferenc
 
Exploring Generative AI with GAN Models
Exploring Generative AI with GAN ModelsExploring Generative AI with GAN Models
Exploring Generative AI with GAN ModelsKonfHubTechConferenc
 
Become Thanos of the LambdaLand - Wield All the Infinity Stones
Become Thanos of the LambdaLand - Wield All the Infinity StonesBecome Thanos of the LambdaLand - Wield All the Infinity Stones
Become Thanos of the LambdaLand - Wield All the Infinity StonesKonfHubTechConferenc
 

Mais de KonfHubTechConferenc (9)

KonfHub Features, Benefits and Pricing
KonfHub Features, Benefits and Pricing KonfHub Features, Benefits and Pricing
KonfHub Features, Benefits and Pricing
 
Azuga A Safety Company - Data Science Saving Lives
Azuga A Safety Company - Data Science Saving LivesAzuga A Safety Company - Data Science Saving Lives
Azuga A Safety Company - Data Science Saving Lives
 
Self Supervised Learning for Vision Tasks (1).pdf
Self Supervised Learning for Vision Tasks (1).pdfSelf Supervised Learning for Vision Tasks (1).pdf
Self Supervised Learning for Vision Tasks (1).pdf
 
Application of Artificial Intelligence for Automotive Applications
Application of Artificial Intelligence for Automotive ApplicationsApplication of Artificial Intelligence for Automotive Applications
Application of Artificial Intelligence for Automotive Applications
 
Are you ready for AI? Is AI ready for you?
Are you ready for AI? Is AI ready for you?Are you ready for AI? Is AI ready for you?
Are you ready for AI? Is AI ready for you?
 
Exploring Generating AI with Diffusion Models
Exploring Generating AI with Diffusion ModelsExploring Generating AI with Diffusion Models
Exploring Generating AI with Diffusion Models
 
Exploring Generative AI with GAN Models
Exploring Generative AI with GAN ModelsExploring Generative AI with GAN Models
Exploring Generative AI with GAN Models
 
KonfHub Recap 2021
KonfHub Recap 2021 KonfHub Recap 2021
KonfHub Recap 2021
 
Become Thanos of the LambdaLand - Wield All the Infinity Stones
Become Thanos of the LambdaLand - Wield All the Infinity StonesBecome Thanos of the LambdaLand - Wield All the Infinity Stones
Become Thanos of the LambdaLand - Wield All the Infinity Stones
 

Último

Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...Abortion Clinic
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AIAGATSoftware
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIInflectra
 
ćŸźäżĄć·èŽ­äč°
ćŸźäżĄć·èŽ­äč°ćŸźäżĄć·èŽ­äč°
ćŸźäżĄć·èŽ­äč°westwaterchamplin107
 
[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)Dimitrios Platis
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jNeo4j
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Conceptsthomashtkim
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024SimonedeGijt
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNeo4j
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio, Inc.
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024SimonedeGijt
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdftimtebeek1
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAShane Coughlan
 

Último (20)

Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ đŸ„ Women's Abortion Clinic i...
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
Abortion Pill Prices Jane Furse ](+27832195400*)[đŸ„Women's Abortion Clinic in ...
Abortion Pill Prices Jane Furse ](+27832195400*)[đŸ„Women's Abortion Clinic in ...Abortion Pill Prices Jane Furse ](+27832195400*)[đŸ„Women's Abortion Clinic in ...
Abortion Pill Prices Jane Furse ](+27832195400*)[đŸ„Women's Abortion Clinic in ...
 
ćŸźäżĄć·èŽ­äč°
ćŸźäżĄć·èŽ­äč°ćŸźäżĄć·èŽ­äč°
ćŸźäżĄć·èŽ­äč°
 
[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)[GRCPP] Introduction to concepts (C++20)
[GRCPP] Introduction to concepts (C++20)
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMs
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
Abortion Clinic In Pretoria ](+27832195400*)[ đŸ„ Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ đŸ„ Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ đŸ„ Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ đŸ„ Safe Abortion Pills in Pretor...
 
Abortion Pill Prices Aliwal North ](+27832195400*)[ đŸ„ Women's Abortion Clinic...
Abortion Pill Prices Aliwal North ](+27832195400*)[ đŸ„ Women's Abortion Clinic...Abortion Pill Prices Aliwal North ](+27832195400*)[ đŸ„ Women's Abortion Clinic...
Abortion Pill Prices Aliwal North ](+27832195400*)[ đŸ„ Women's Abortion Clinic...
 
Abortion Clinic In Pongola ](+27832195400*)[ đŸ„ Safe Abortion Pills In Pongola...
Abortion Clinic In Pongola ](+27832195400*)[ đŸ„ Safe Abortion Pills In Pongola...Abortion Clinic In Pongola ](+27832195400*)[ đŸ„ Safe Abortion Pills In Pongola...
Abortion Clinic In Pongola ](+27832195400*)[ đŸ„ Safe Abortion Pills In Pongola...
 
Abortion Pill Prices Mthatha (@](+27832195400*)[ đŸ„ Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ đŸ„ Women's Abortion Clinic In...Abortion Pill Prices Mthatha (@](+27832195400*)[ đŸ„ Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ đŸ„ Women's Abortion Clinic In...
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 

Functional Thinking for Java Developers (presented in Javafest Bengaluru)

  • 1. Lets Get Funky with Functional Progamming Ganesh Samarthyam ganesh@konfhub.com
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. Perspective - for loops! List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); for(String string : strings) { System.out.println(string); } External Iteration
  • 9. Perspective - for loops! List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); strings.forEach(string -> System.out.println(string)); Internal Iteration
  • 10. 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
  • 11. 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
  • 12. Lambdas List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); Consumer<String> printString = string -> System.out.println(string); strings.forEach(printString); Lambda functions!
  • 13. Lambdas List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); Consumer<String> printString = string -> System.out.println(string); strings.forEach(printString); Capture in a variable Execute later
  • 14. What are lambda functions? ❖ 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
  • 15. 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.”
  • 17. Productive programming with lambdas import java.io.*; class Type { public static void main(String []files) { // process each file passed as argument for(String file : files) { // try opening the file with FileReader try (FileReader inputFile = new FileReader(file)) { int ch = 0; while( (ch = inputFile.read()) != -1) { // ch is of type int - convert it back to char System.out.print( (char)ch ); } } catch (FileNotFoundException fnfe) { System.err.printf("Cannot open the given file %s ", file); } catch(IOException ioe) { System.err.printf("Error when processing file %s; skipping it", file); } // try-with-resources will automatically release FileReader object } } } args.each { println new File(it).getText() }
  • 18. Workshop overview ❖ Assumes that you already know Java ❖ You’ll know how to use Java lambdas and streams after this session ❖ Try out the programs in your machine
  • 20. Java lambdas - “Hello world!” interface LambdaFunction { void call(); } class FirstLambda { public static void main(String []args) { LambdaFunction lambdaFunction = () -> System.out.println("Hello world"); lambdaFunction.call(); } }
  • 21. Java 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
  • 22. ` () -> 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
  • 23. 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
  • 24. 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 }
  • 25. 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));
  • 26. Method references Method references - “syntactic sugar” for lambda functions They “route” function parameters arg -> System.out.println(arg) System.out::println
  • 27. 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);
  • 28. 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
  • 29. 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);
  • 30. “Effectively final” variables import java.util.Arrays; import java.util.List; class PigLatin { public static void main(String []args) { String suffix = "ay"; List<String> strings = Arrays.asList("one", "two", "three", "four"); strings.forEach(string -> System.out.println(string + suffix)); } } Accessing “local variable” suffix here; hence it is considered “effectively final”
  • 31. “Effectively final” variables import java.util.Arrays; import java.util.List; class PigLatin { public static void main(String []args) { String suffix = "ay"; List<String> strings = Arrays.asList("one", "two", "three", “four"); suffix = "e"; // assign to suffix variable strings.forEach(string -> System.out.println(string + suffix)); } } PigLatinAssign.java:9: error: local variables referenced from a lambda expression must be final or effectively final strings.forEach(string -> System.out.println(string + suffix)); ^ 1 error
  • 33. First stream example "hello".chars().sorted().forEach(ch -> System.out.printf("%c ", ch)); The chars() method in String results in a Stream; sorted() sorts the entries in the stream // prints e h l l o
  • 35. Stream pipeline example Arrays.stream(Object.class.getMethods()) // source .map(method -> method.getName()) // intermediate op .distinct() // intermediate op .forEach(System.out::println); // terminal operation
  • 36. Stream pipeline example 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);
  • 37. One-liner #1 Files.lines(Paths.get("FileRead.java")).forEach(System.out::println); This code prints the contents of the file “FileRead.java” in the current directory
  • 38. One-liner #2 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
  • 40. One-liner #4 "hello".chars().sorted().forEach(ch -> System.out.printf("%c ", ch)); Extracts characters in the string “hello”, sorts the chars and prints the chars
  • 41. Stream pipeline illustration DoubleStream.of(1.0, 4.0, 9.0) .map(Math::sqrt) .peek(System.out::println) .sum();
  • 42. Stream sources IntStream.range(1, 6) You can use range or iterate factory methods in the IntStream interface IntStream.iterate(1, i -> i + 1).limit(5)
  • 43. Stream sources Arrays.stream(new int[] {1, 2, 3, 4, 5}) Arrays.stream(new Integer[] {1, 2, 3, 4, 5}) You can use the stream() method in java.util.Arrays class to create a stream from a given array
  • 44. Stream sources Stream.of(1, 2, 3, 4, 5) Stream.of(new Integer[]{1, 2, 3, 4, 5}) We can also create streams using factories and builders (e..g, of() and build() methods in the Stream interface) Stream.builder().add(1).add(2).add(3).add(4).add(5).build()
  • 45. Stream sources ‱ The lines() method in java.nio.file.Files class ‱ The splitAsStream() method in java.util.regex.Pattern class ‱ The ints() method in java.util.Random class ‱ The chars() method in java.lang.String class There are numerous classes/interfaces in the Java library that return a stream
  • 46. Intermediate operations Stream<T> filter(Predicate<? super T> check) Removes the elements for which the check predicate returns false. <R> Stream<R> map(Function<? super T,? extends R> transform) Applies the transform() function for each of the elements in the stream. Stream<T> distinct() Removes duplicate elements in the stream; it uses the equals() method to determine if an element is repeated in the stream. Stream<T> sorted() Stream<T> sorted(Comparator<? super T> compare) Sorts the elements in its natural order. The overloaded version takes a Comparator – you can pass a lambda function for that. Stream<T> peek(Consumer<? super T> consume) Returns the same elements in the stream, but also executes the passed consume lambda expression on the elements. Stream<T> limit(long size) Removes the elements if there are more elements than the given size in the stream.
  • 47. Terminal operations void forEach(Consumer<? super T> action) Calls the action for every element in the stream. Object[] toArray() Returns an Object array that has the elements in the stream. Optional<T> min(Comparator<? super T> compare) Returns the minimum value in the stream (compares the objects using the given compare function). Optional<T> max(Comparator<? super T> compare) Returns the maximum value in the stream (compares the objects using the given compare function). long count() Returns the number of elements in the stream.
  • 48. Using “range” instead of “for” loop IntStream.range(1, 10).map(i -> i * i).forEach(System.out::println); Using streams instead of imperative for i = 1 to 1, print i * i Prints: 1 4 9 16 25 36 49 64 81
  • 50. Streams can be “infinite”
  • 51. “Generating” even numbers IntStream.iterate(0, i -> i + 2).forEach(System.out::println); The problem is it creates infinite number of even numbers!
  • 52. “Generating” limited 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
  • 53. “Generating” limited even numbers IntStream .iterate(0, i -> i + 1) .filter(i -> (i % 2) == 0) .limit(5) .forEach(System.out::println); You can also use the “filter” method
  • 54. “Reduction” using “sum” function System.out.println(IntStream.rangeClosed(0, 10).sum()); Sum of integers from 1 to 10 using “implicit reduction”
  • 55. “Reduction” using “sum” function System.out.println( IntStream .rangeClosed(1, 5) .reduce((x, y) -> (x * y)) .getAsInt()); Factorial of 5 using “explicit reduce”
  • 56. Using “map” function List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo"); strings.stream().map(value -> value.toUpperCase()).forEach(System.out::println); public static void printUpperCaseString(String string) { System.out.println(string.toUpperCase()); } strings.forEach(MethodReference::printUpperCaseString);
  • 57. Using “collect” function String boxedString = Arrays .asList("eeny", "meeny", "miny", "mo") .stream() .collect(Collectors.joining(“ ,", "[", "]")); System.out.println(boxedString); Prints: [eeny, meeny, miny, mo]
  • 58. Using Files.list() Files.list(Paths.get(".")) .map(path -> path.toAbsolutePath()) .forEach(System.out::println);
  • 59. Built-in interfaces Predicate<T> Checks a condition and returns a boolean value as result In filter() method in java.util.stream.Stream which is used to remove elements in the stream that don’t match the given condition (i.e., predicate) as argument. Consumer<T> Operation that takes an argument but returns nothing In forEach() method in collections and in java.util.stream.Stream; this method is used for traversing all the elements in the collection or stream. Function<T, R> Functions that take an argument and return a result In map() method in java.util.stream.Stream to transform or operate on the passed value and return a result. Supplier<T> Operation that returns a value to the caller (the returned value could be In generate() method in java.util.stream.Stream to
  • 64. map vs. flatMap methods
  • 65. String limerick = "There was a young lady named Bright " + "who traveled much faster than light " + "She set out one day " + "in a relative way " + "and came back the previous night "; IntSummaryStatistics wordStatistics = Pattern.compile(" ") .splitAsStream(limerick) .mapToInt(word -> word.length()) .summaryStatistics(); System.out.printf(" Number of words = %d n Sum of the length of the words = %d n" + " Minimum word size = %d n Maximum word size %d n " + " Average word size = %f n", wordStatistics.getCount(), wordStatistics.getSum(), wordStatistics.getMin(), wordStatistics.getMax(), wordStatistics.getAverage()); Data calculation methods in stream
  • 66. Final (longer) example import java.util.stream.*; import java.nio.file.*; import java.util.*; import java.nio.charset.Charset; import java.io.IOException; class ReadFile { public static void main(String []args) throws IOException { List<String> lines = Files.readAllLines(Paths.get("./Ulysses.txt"), Charset.def Map<Integer, List<String>> wordGroups = lines.parallelStream() .map(line -> line.replaceAll("W", " ").split(" ")) .flatMap(Arrays::stream) .filter(str -> str.length() > 7) .distinct() .sorted() .collect(Collectors.groupingBy(String::length)); wordGroups.forEach( (count, words) -> { System.out.printf("word(s) of length %d %n", count); words.forEach(System.out::println); }); } } Lists words organised by their length in the novel “Ulysses.txt”