2014 java functional

105
Demian Neidetcher FP in Java8

description

 

Transcript of 2014 java functional

Page 1: 2014 java functional

Demian Neidetcher

FP in Java8

Page 2: 2014 java functional

Who am I?

1995 20102000 2005

demian0311

linkedin.com/in/demian0311

neidetcher.com

[email protected]

Page 3: 2014 java functional

Questions

• Who is using Java?

• Who’s using a language with FP features?

• Anyone using Java8?

Page 4: 2014 java functional

Java and FP Agenda

•Intro & Context

•Non-FP features

•Lambdas

•Streams

Page 5: 2014 java functional

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.*

Page 6: 2014 java functional

Intro

Page 7: 2014 java functional

Background

• Java8 delayed for security improvements and lambdas

• Brian Goetz is lead on lambdas

• Java8 released March 2014

• next release will feature Jigsaw (modularity)

Page 8: 2014 java functional

Lots of Cool Languages with Closures/ FP

Page 9: 2014 java functional

But Still Lots of Java Work Out There

Page 10: 2014 java functional

What makes a language functional?

• first class functions

• higher order functions

• recursion

• pure functions

Page 11: 2014 java functional

FP and performance

• FP offers a way of working with collections of data in a way that is easy to make parallel

Page 12: 2014 java functional

FP makes code more simple

• less ceremony

• fewer lines of code

Page 13: 2014 java functional

FP makes code less error prone

• discourages mutable variables

• easier to understand

Page 14: 2014 java functional

JVM languages with FP features

• Clojure

• Scala

• Groovy

• JRuby

• Jython

Page 15: 2014 java functional

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

Page 16: 2014 java functional

Objections to FP

• learning curve

• not intuitive

• overly academic

Page 17: 2014 java functional

Java8 Features

Page 18: 2014 java functional

MetaSpace

Page 19: 2014 java functional

No more permgen

• metaspace will grab native memory as needed at runtime

• -XX:MaxMetaspaceSize=256m can specify the upper bound

Page 20: 2014 java functional

No more permgen

Page 21: 2014 java functional

No more permgen

Page 22: 2014 java functional

Optional

Page 23: 2014 java functional

Optional

List

String String String String

Page 24: 2014 java functional

Optional

List

String String String String Optional

String

Page 25: 2014 java functional

Using a Populated Optional

Page 26: 2014 java functional

Using an Empty Optional

Page 27: 2014 java functional

Giving Optional a Null

Page 28: 2014 java functional

Creating an Optional with ofNullable()

Page 29: 2014 java functional

Getting an Optional in Collections

Page 30: 2014 java functional

Don’t call get() without checking

Page 31: 2014 java functional

Equals and Hashcode for Optional

Page 32: 2014 java functional

Using Option in your own beans

Page 33: 2014 java functional

String Join

Page 34: 2014 java functional

StringBuilder.append

Page 35: 2014 java functional

StringJoiner

Page 36: 2014 java functional

StringJoiner with forEach

Page 37: 2014 java functional

String.join

Page 38: 2014 java functional

Interface Defender Methods

Page 39: 2014 java functional

Why Defender Methods

Page 40: 2014 java functional

Iterator.forEach

Page 41: 2014 java functional

Why Defender Methods

<<interface>>Iterable

default forEach(Consumer<T>): void

<<interface>>Collection

<<interface>>List

<<class>>ArrayList

Page 42: 2014 java functional

Why Defender Methods

Page 43: 2014 java functional

Our own Interface with Defender Method

Page 44: 2014 java functional

Our own Interface with Defender Method

draw(): String

<<interface>>Cowboy

<<class>>Caballero

uses draw from Interface

default method

Page 45: 2014 java functional

Disambiguating Default Methods

Page 46: 2014 java functional

Disambiguating Default Methods

draw(): String

<<interface>>Cowboy

draw(): String

<<interface>>Circle

<<class>>Circle

Cowboymust disambiguate

Page 47: 2014 java functional

Classes Win

Page 48: 2014 java functional

Classes Win

draw(): String

<<interface>>Circle

<<abstract class>>

DeckOfCards

<<class>>CircleDeckOf

Cards

class wins

Page 49: 2014 java functional

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

Page 50: 2014 java functional

Lambdas

Page 51: 2014 java functional

Lambdas on Their Own

Page 52: 2014 java functional

Lambda Syntax

arguments

body

Page 53: 2014 java functional

Lambdas from Functional Interfaces #0

Page 54: 2014 java functional

Lambdas from Functional Interfaces #1

Page 55: 2014 java functional

Lambdas from Functional Interfaces #2

Page 56: 2014 java functional

Lambdas Remove Cruft

Page 57: 2014 java functional

Single Abstract Method Interfaces

This annotation is optional, just to keep you honest.

Page 58: 2014 java functional

Single Abstract Method Interfaces

Page 59: 2014 java functional

Runnable is a SAMI

Go into Runnable and show how it is an interace with only 1 abstract method

Page 60: 2014 java functional

Comparator is a SAMI

Page 61: 2014 java functional

New Functional Interfaces

• Function

• Predicate

• Consumer

• Supplier

All of these are found in java.util.function

Page 62: 2014 java functional

Function

Function

input

result

Page 63: 2014 java functional

Predicate

Predicate

input

Boolean

Page 64: 2014 java functional

Consumer

Consumer

input

Page 65: 2014 java functional

Supplier

Supplier

result

Page 66: 2014 java functional

UnaryOperator

Page 67: 2014 java functional

SAMIs That Take Primitives

Page 68: 2014 java functional

SAMIs That Return Primitives

Page 69: 2014 java functional

Bi*

Page 70: 2014 java functional

Lambdas with Streams #0

Page 71: 2014 java functional

Lambdas with Streams #1

Page 72: 2014 java functional

Lambdas with Streams #2

Page 73: 2014 java functional

Lambdas with Streams #3

was this

Page 74: 2014 java functional

Method References

Page 75: 2014 java functional

Method References

Page 76: 2014 java functional

Method References

Page 77: 2014 java functional

Streams

Page 78: 2014 java functional

Range from IntStream

From here on you have a stream

Page 79: 2014 java functional

Random Streams

This function creates an IntStream

Page 80: 2014 java functional

Stream Pipeline

collection source

intermediate

intermediate

intermediate terminal

Page 81: 2014 java functional

Stream Pipeline

ArrayList .stream()

.filter(…)

.map(…)

.sorted(…) .collect()

lamdas go here

Page 82: 2014 java functional

Stream Pipeline

source

intermediate

terminal

Page 83: 2014 java functional

Streams Should be Familiar

demian@raven ~>ls /etc/init.d/ | grep mount | sort -r > ~/stuff.txt

source

intermediate

terminal

Page 84: 2014 java functional

Stream Method Taxonomy

Stream Methods

Source

Intermediate

Terminal

filter

transform

sorted reductions

collect

count

min

max

reduce

stream

map

Page 85: 2014 java functional

Source

source

Page 86: 2014 java functional

Intermediate: filter

Page 87: 2014 java functional

Intermediate: map

Page 88: 2014 java functional

Intermediate: sorted

Page 89: 2014 java functional

Terminal: forEach

Page 90: 2014 java functional

Terminal: reduce

Page 91: 2014 java functional

Terminal: collect

Page 92: 2014 java functional

Terminal: collect with multi args

Page 93: 2014 java functional

Terminal: collect with groupingBy

Page 94: 2014 java functional

Contrived Slow Function

Page 95: 2014 java functional

Serial Stream

Page 96: 2014 java functional

Parallel Stream

Page 97: 2014 java functional

Parallel Streams

Page 98: 2014 java functional

So is Java8 a Functional Language?

• recursion?

• pure functions?

• first class functions?

• higher order functions?

Page 99: 2014 java functional

Recursion

Page 100: 2014 java functional

Pure Functions

Page 101: 2014 java functional

Functions that Take Another Function

Page 102: 2014 java functional

Functions That Create Functions

Page 103: 2014 java functional

So is Java8 a Functional Language?

Not at all a purely functional language.

Page 104: 2014 java functional

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) }

Page 105: 2014 java functional

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.*