Learning Objectives in This Part of the Module

Learning Objectives in This Part of the Module

Understand Java’s Key Functional Programming Concepts & Features Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA Learning Objectives in this Lesson • Understand key functional programming concepts & features supported by Java These functional programming features2 were added in Java 8 & expanded later Learning Objectives in this Lesson • Understand key functional programming concepts & features supported by Java • Know how to compare & contrast functional programming & object-oriented programming 3 Key Functional Programming Concepts in Java 4 Key Functional Programming Concepts in Java • Functional programming has its roots in lambda calculus See en.wikipedia.org/wiki/5 Functional_programming Key Functional Programming Concepts in Java • Functional programming has its roots Input x in lambda calculus, e.g., • Computations are treated as Function f evaluation of math functions Output f(x) Ideally, each function is “pure,” i.e., it has no side- effects on memory or I/O See en.wikipedia.org/wiki/Functional_programming#Pure_functions6 Key Functional Programming Concepts in Java • Functional programming has its roots Input x in lambda calculus, e.g., • Computations are treated as Function f evaluation of math functions Output f(x) Function g Output g(f(x)) Note “function composition”: the output of one function serves as Function h the input to the next function, etc. Output h(g(f(x))) See martinfowler.com/articles/collection7 -pipeline Key Functional Programming Concepts in Java • Functional programming has its roots Input x in lambda calculus, e.g., • Computations are treated as Function f evaluation of math functions Output f(x) Functionally compute the ‘nth’ factorial in parallel long factorial Function g (long n) { return LongStream Output g(f(x)) .rangeClosed(1, n) .parallel() Function h .reduce(1, (a, b) -> a * b); Output h(g(f(x))) } See github.com/douglascraigschmidt8/LiveLessons/tree/master/Java8/ex16 Key Functional Programming Concepts in Java • Functional programming has its roots Range of longs from 1..8 in lambda calculus, e.g., • Computations are treated as longs 1..4 longs 5..8 evaluation of math functions longs 1..2 longs 3..4 longs 5..6 longs 7..8 long factorial (long n) { return LongStream .rangeClosed(1, n) Generate a stream of longs from .parallel() 1 to n in parallel (where n == 8) .reduce(1, (a, b) -> a * b); } See www.baeldung.com/java9 -8-streams Key Functional Programming Concepts in Java • Functional programming has its roots Range of longs from 1..8 in lambda calculus, e.g., • Computations are treated as longs 1..4 longs 5..8 evaluation of math functions longs 1..2 longs 3..4 longs 5..6 longs 7..8 long factorial (long n) { Process Process Process Process sequentially sequentially sequentially sequentially return LongStream .rangeClosed(1, n) 2 12 30 56 .parallel() .reduce(1, (a, b) -> a * b); Multiply pair-wise values in parallel } See docs.oracle.com/javase/tutorial/collections/streams/parallelism.html10 Key Functional Programming Concepts in Java • Functional programming has its roots Range of longs from 1..8 in lambda calculus, e.g., • Computations are treated as longs 1..4 longs 5..8 evaluation of math functions longs 1..2 longs 3..4 longs 5..6 longs 7..8 long factorial (long n) { Process Process Process Process sequentially sequentially sequentially sequentially return LongStream .rangeClosed(1, n) 2 12 30 56 .parallel() .reduce(1, 24 reduce() reduce() 1,680 (a, b) -> a * b); reduce() } 40,320 Successively combine two immutable11 long values & produce a new one Key Functional Programming Concepts in Java • Functional programming has its roots in lambda calculus, e.g., • Computations are treated as evaluation of math functions • Changing state & mutable shared data are discouraged to avoid various hazards See en.wikipedia.org/wiki/Side_effect12 _(computer_science) Key Functional Programming Concepts in Java • Functional programming has its roots class Total { in lambda calculus, e.g., public long mTotal = 1; • Computations are treated as public void mult(long n) evaluation of math functions { mTotal *= n; } • Changing state & mutable shared data } are discouraged to avoid various hazards long factorial(long n) { Total t = new Total(); LongStream.rangeClosed(1, n) .parallel() .forEach(t::mult); return t.mTotal; } See github.com/douglascraigschmidt13/LiveLessons/tree/master/Java8/ex16 Key Functional Programming Concepts in Java • Functional programming has its roots class Total { in lambda calculus, e.g., public long mTotal = 1; • Computations are treated as public void mult(long n) evaluation of math functions { mTotal *= n; } • Changing state & mutable shared data } are discouraged to avoid various hazards Shared mutable state long factorial(long n) { Total t = new Total(); LongStream.rangeClosed(1, n) .parallel() .forEach(t::mult); return t.mTotal; } See github.com/douglascraigschmidt14/LiveLessons/tree/master/Java8/ex16 Key Functional Programming Concepts in Java • Functional programming has its roots class Total { in lambda calculus, e.g., public long mTotal = 1; • Computations are treated as public void mult(long n) evaluation of math functions { mTotal *= n; } • Changing state & mutable shared data } are discouraged to avoid various hazards long factorial(long n) { Total t = new Total(); rangeClosed() LongStream.rangeClosed(1, n) .parallel() Run in parallel parallel() .forEach(t::mult); return t.mTotal; } forEach() See docs.oracle.com/javase/tutorial/collections/streams/parallelism.html15 Key Functional Programming Concepts in Java • Functional programming has its roots class Total { in lambda calculus, e.g., public long mTotal = 1; • Computations are treated as public void mult(long n) evaluation of math functions { mTotal *= n; } • Changing state & mutable shared data } are discouraged to avoid various hazards Beware of race conditions!!! long factorial(long n) { Total t = new Total(); LongStream.rangeClosed(1, n) .parallel() .forEach(t::mult); return t.mTotal; } See en.wikipedia.org/wiki/16Race_condition#Software Key Functional Programming Concepts in Java • Functional programming has its roots class Total { in lambda calculus, e.g., public long mTotal = 1; • Computations are treated as public void mult(long n) evaluation of math functions { mTotal *= n; } • Changing state & mutable shared data } are discouraged to avoid various hazards long factorial(long n) { Total t = new Total(); LongStream.rangeClosed(1, n) .parallel() .forEach(t::mult); return t.mTotal; } Beware of inconsistent memory visibility See jeremymanson.blogspot.com/2007/08/atomicity17 -visibility-and-ordering.html Key Functional Programming Concepts in Java • Functional programming has its roots class Total { in lambda calculus, e.g., public long mTotal = 1; • Computations are treated as public void mult(long n) evaluation of math functions { mTotal *= n; } • Changing state & mutable shared data } are discouraged to avoid various hazards long factorial(long n) { Total t = new Total(); LongStream.rangeClosed(1, n) .parallel() .forEach(t::mult); return t.mTotal; Only you can prevent } concurrency hazards! In Java you must avoid these hazards, 18i.e., the compiler & JVM won’t save you.. Key Functional Programming Concepts in Java • Functional programming has its roots in lambda calculus, e.g., • Computations are treated as evaluation of math functions • Changing state & mutable shared data are discouraged to avoid various hazards • Instead, focus is on “immutable” objects See docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html19 Key Functional Programming Concepts in Java • Functional programming has its roots final class String { in lambda calculus, e.g., private final char value[]; ... • Computations are treated as evaluation of math functions public String(String s) { • Changing state & mutable shared data value = s; are discouraged to avoid various hazards ... } • Instead, focus is on “immutable” objects • Immutable object state cannot public int length() { return value.length; change after it is constructed } ... } See www.baeldung.com/java20 -immutable-object Key Functional Programming Concepts in Java • Functional programming has its roots final class String { in lambda calculus, e.g., private final char value[]; ... • Computations are treated as evaluation of math functions public String(String s) { • Changing state & mutable shared data value = s; are discouraged to avoid various hazards ... } • Instead, focus is on “immutable” objects • Immutable object state cannot public int length() { return value.length; change after it is constructed } • Java String is a common example ... of an immutable object } See docs.oracle.com/javase/8/docs/21 api/java/lang/String.html Key Functional Programming Concepts in Java • Functional programming has its roots final class String { in lambda calculus, e.g., private final char value[]; ... • Computations are treated as evaluation of math functions public String(String s) { • Changing state & mutable shared data value = s; are discouraged to avoid various hazards ... } • Instead, focus is on “immutable” objects • Immutable object state cannot public int length() { return value.length; change after it is constructed } • Java String is a common example ... of an immutable object } • Fields are final & only accessor methods See www.programcreek.com/2013/04/why22 -string-is-immutable-in-java Functional vs. Object-Oriented

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    29 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us