Functional Data Structures

Functional Programming in Java

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

Introduction

  • FP emphasises not updating data
  • Alternative?
  • Pure functions

Referential Transparency


    // updating is not referentially transparent
    List<Integer> list = new LinkedList<>();
    list.add(1);
    List<Integer> list2 = list; // [1]

    new LinkedList<Integer>().add(1);
    List<Integer> list2 = new LinkedList<>(); // []

Functional Data Structures

  • Values are immutable!
  • Use pure functions
  • Data copying issue?

Exercises


B foldLeft(Tree<A> tree, B acc, F2<B, A, B> f)
int size(Tree<A> tree)
int depth(Tree<A> tree)
Tree<B> map(Tree<A> t, F<A, B> f)
List<A> traverseLeft(Tree<A> t)

Summary

  • Data Sharing
  • Folds
  • Practiced Pure Functions

Afterword

Functional Programming in Scala, Chiusano and Bjarnason, Chapter 3, Functional Data Structures

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