Functional Programming in Java

An Introduction

Created by Mark Perry, @mprry, G+, Blog, LinkedIn, GitHub, maperry78@yahoo.com.au

Introduction

  • Goal: Learn FP in Java
  • Premise: Programs from pure functions
  • Can feel strange - keep an open mind

The FP Road

  • Introduction
  • Data Structures
  • Errors without exceptions
  • Strictness and Laziness
  • Purely Functional State
  • Purely Functional Parrallelism
  • Property Based Testing
  • Parser Combinators
  • Monoids
  • Monads
  • Applicative Functors
  • External Effects and I/O
  • Local Effects and Mutable State
  • Stream Processing and Incremental I/O
  • Premise

    • Premise: Programs from pure functions
    • Using book, "Functional Programming in Scala"
    • FP is less powerful than imperative
    • Increased modularity
    • Easier to:
      • write
      • test
      • reuse
      • reason
      • scale
    • Effects aren't observed

    Purity

    • every X has just one Y, f: X -> Y
    • intToString: Int -> String
    • and nothing else!

    • Referential transparency: replace expression with result

    Example

    • Replace expression with value
    • Equational reasoning
    
    String x = "Hello world";
    String r1 = x.reverse(); // "dlrow olleH"
    String r2 = x.reverse(); // "dlrow olleH"
    					
    
    // replace x with "Hello world"
    String r1 = "Hello world".reverse(); // "dlrow olleH"
    String r2 = "Hello world".reverse(); // "dlrow olleH"
     					

    Non RT Example

    
    StringBuilder sb = new StringBuilder("Hello");
    String x = sb.append(" world").toString(); // "Hello world"
    String y = sb.toString(); // "Hello world"
    					
    
    // replace sb with: new StringBuilder("Hello")
    String x = new StringBuilder("Hello").append(" world").toString(); // "Hello world"
    String y = new StringBuilder("Hello").toString(); // "Hello"
    					

    Why FP?

    • Modularity: Components understood independently
    • Compositon: f(g(x)) = (f compose g)(x)
    • Whole system depends only on composition
    • Separate:
      • Inputs
      • Computation logic
      • Result usage
    • Can reuse computation without concern for effects

    Non RT System

    Refunctoring

    
    @Value.Immutable
    interface Player {
    	String name();
    	Integer score();
    }
    
    void declareWinner(Player p1, Player p2) {
        if (p1.score() > p2.score()) {
            printWinner(p1);
        } else {
            printWinner(p2);
        }
    }
    
    void printWinner(Player p) {
        out.println(p.name() + " is the winner!");
    }
    
    
    void declareWinner(Player p1, Player p2) {
        printWinner(winner(p1, p2))
    }
    
    Player winner(Player p1, Player p2) {
        return p1.score() > p2.score() ? p1 : p2;
    }

    Better System

    Principles

    • Functions with side effects are split:
      • Pure function
      • Effect on IO
    • Push effects to the outside
    • Pure, reusable inner core

    Imperative

    • Java standard library
    • exceptions
    • variables
    • null
    • setters
    • reflection
    • instanceof
    • void

    Summary

    • What is FP
    • Why is FP important
    • Questions - how do we:
      • write loops?
      • implement data structures?
      • deal with errors and exceptions?
      • handle input and output?
      • concurrency?

    Afterword

    Functional Programming in Scala, Chiusano and Bjarnason, Chapter 1

    Created by Mark Perry, @mprry, G+, Blog, LinkedIn, GitHub, maperry78@yahoo.com.au