Purely Functional State

Functional Programming in Java

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

Introduction

  • Learn to manipulate state using RNGs
  • Explore issues
  • Pattern of any stateful API

Standard RNG

  • java.util.Random has imperative API
  • Uses side effects
  • 
    Random rng = new java.util.Random();
    rng.nextInt(); // 500348701
    rng.nextInt(); // -350347772
    rng.nextInt(5); // 4, range = [0, 4]
    rng.nextInt(5); // 1
    					
  • Not referentially transparent
  • Harder to test, compose, modularise and parallelise

Testability


int rollDie() {
    Random rng = new java.util.Random();
    return rng.nextInt(6); // [0, 5], but want [1, 6]
}
					
  • Off by one error
  • Obvious - complicated defects are subtle
  • Need to reproduce reliably

Summary

  • FP programs with state
  • RNG example to motivate
  • Use function that takes state and returns new state and a result
  • State functions make this simple

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
  • Afterword

    • Functional Programming in Scala, Chiusano and Bjarnason
    • Chapter 6, Purely Functional State

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