Calculus, Not Rocket Science

Or, What FP Can Give You in an OO World

Will Hayworth / @_wsh

Lab49

May 17, 2013

What's FP?

What are functions?

  • Computation is function application
  • Functions are free variables with operations
  • Application (or partial application) entails resolving those variables

What are objects?

  • Smalltalk: pure state (fields + messages)
  • Java/C++: state + functions
  • Ruby: kind of both?

What does null mean?

HashMap<Integer, Integer> someHashMap = new HashMap<>();
someHashMap.put(1, 2);
Optional<Integer> foo = Optional.fromNullable(someHashMap.get(1));
Optional<Integer> bar = Optional.fromNullable(someHashMap.get(3));

if (foo.isPresent()) foo.get(); // 2
bar.isPresent(); // false
bar.get(); // IllegalStateException
bar.or(5); // 5

Why is this handy?

  • No NullPointerExceptions. Ever.
  • You're required to handle every case.

Exhibit B:

Immutable________

Functional languages

love Immutability

Thanks!