2014 java functional
-
Upload
demian-neidetcher -
Category
Documents
-
view
128 -
download
0
description
Transcript of 2014 java functional
Demian Neidetcher
FP in Java8
Who am I?
1995 20102000 2005
demian0311
linkedin.com/in/demian0311
neidetcher.com
Questions
• Who is using Java?
• Who’s using a language with FP features?
• Anyone using Java8?
Java and FP Agenda
•Intro & Context
•Non-FP features
•Lambdas
•Streams
Big Points
• Pragmatically speaking, FP helps you do efficient things with collections, taking advantage of multi-core or distributed systems
• Lambdas are small functions that you implement that extend SAM Interfaces, likely the ones found in java.util.function.*
• Streams are where collections and lambdas come together, you want to get familiar with java.util.stream.*
Intro
Background
• Java8 delayed for security improvements and lambdas
• Brian Goetz is lead on lambdas
• Java8 released March 2014
• next release will feature Jigsaw (modularity)
Lots of Cool Languages with Closures/ FP
But Still Lots of Java Work Out There
What makes a language functional?
• first class functions
• higher order functions
• recursion
• pure functions
FP and performance
• FP offers a way of working with collections of data in a way that is easy to make parallel
FP makes code more simple
• less ceremony
• fewer lines of code
FP makes code less error prone
• discourages mutable variables
• easier to understand
JVM languages with FP features
• Clojure
• Scala
• Groovy
• JRuby
• Jython
FP Languages
• 1959 Lisp
• 1975 ML, FP, Scheme
• 1978 Smalltalk
• 1986 Standard ML
• 1990 Haskell, Erlang
• 1999 XSLT
• 2000 OCaml
• 2003 Scala, XQuery
• 2005 F#
• 2007 Clojure
• 2011 C++11
Objections to FP
• learning curve
• not intuitive
• overly academic
Java8 Features
MetaSpace
No more permgen
• metaspace will grab native memory as needed at runtime
• -XX:MaxMetaspaceSize=256m can specify the upper bound
No more permgen
No more permgen
Optional
Optional
List
String String String String
Optional
List
String String String String Optional
String
Using a Populated Optional
Using an Empty Optional
Giving Optional a Null
Creating an Optional with ofNullable()
Getting an Optional in Collections
Don’t call get() without checking
Equals and Hashcode for Optional
Using Option in your own beans
String Join
StringBuilder.append
StringJoiner
StringJoiner with forEach
String.join
Interface Defender Methods
Why Defender Methods
Iterator.forEach
Why Defender Methods
<<interface>>Iterable
default forEach(Consumer<T>): void
<<interface>>Collection
<<interface>>List
<<class>>ArrayList
Why Defender Methods
Our own Interface with Defender Method
Our own Interface with Defender Method
draw(): String
<<interface>>Cowboy
<<class>>Caballero
uses draw from Interface
default method
Disambiguating Default Methods
Disambiguating Default Methods
draw(): String
<<interface>>Cowboy
draw(): String
<<interface>>Circle
<<class>>Circle
Cowboymust disambiguate
Classes Win
Classes Win
draw(): String
<<interface>>Circle
<<abstract class>>
DeckOfCards
<<class>>CircleDeckOf
Cards
class wins
Interfaces vs Abstract Classes
feature interface abstract class
method definitions yes yes
method implementations
yes with default yes
state no yes
have a constructor no yes
use synchronized on methods
no yes
Lambdas
Lambdas on Their Own
Lambda Syntax
arguments
body
Lambdas from Functional Interfaces #0
Lambdas from Functional Interfaces #1
Lambdas from Functional Interfaces #2
Lambdas Remove Cruft
Single Abstract Method Interfaces
This annotation is optional, just to keep you honest.
Single Abstract Method Interfaces
Runnable is a SAMI
Go into Runnable and show how it is an interace with only 1 abstract method
Comparator is a SAMI
New Functional Interfaces
• Function
• Predicate
• Consumer
• Supplier
All of these are found in java.util.function
Function
Function
input
result
Predicate
Predicate
input
Boolean
Consumer
Consumer
input
Supplier
Supplier
result
UnaryOperator
SAMIs That Take Primitives
SAMIs That Return Primitives
Bi*
Lambdas with Streams #0
Lambdas with Streams #1
Lambdas with Streams #2
Lambdas with Streams #3
was this
Method References
Method References
Method References
Streams
Range from IntStream
From here on you have a stream
Random Streams
This function creates an IntStream
Stream Pipeline
collection source
intermediate
intermediate
intermediate terminal
Stream Pipeline
ArrayList .stream()
.filter(…)
.map(…)
.sorted(…) .collect()
lamdas go here
Stream Pipeline
source
intermediate
terminal
Streams Should be Familiar
demian@raven ~>ls /etc/init.d/ | grep mount | sort -r > ~/stuff.txt
source
intermediate
terminal
Stream Method Taxonomy
Stream Methods
Source
Intermediate
Terminal
filter
transform
sorted reductions
collect
count
min
max
reduce
stream
map
Source
source
Intermediate: filter
Intermediate: map
Intermediate: sorted
Terminal: forEach
Terminal: reduce
Terminal: collect
Terminal: collect with multi args
Terminal: collect with groupingBy
Contrived Slow Function
Serial Stream
Parallel Stream
Parallel Streams
So is Java8 a Functional Language?
• recursion?
• pure functions?
• first class functions?
• higher order functions?
Recursion
Pure Functions
Functions that Take Another Function
Functions That Create Functions
So is Java8 a Functional Language?
Not at all a purely functional language.
What Next?
• Goetz talked about adding case classes
• I’d like matchers
• for comprehensions
!
def findChannelsForUser(id: Int): Option[List[String]] = { for { user <- userManager.findUserById(id) account <- accountManager.findAccountForUser(user) channels <- channelManager.findChannelsForAccount(account) } yield (channels) }
Big Points
• Pragmatically speaking, FP helps you do efficient things with collections, taking advantage of multi-core or distributed systems
• Lambdas are small functions that you implement that extend SAM Interfaces, likely the ones found in java.util.function.*
• Streams are where collections and lambdas come together, you want to get familiar with java.util.stream.*