Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Functional Programming Inception

Functional Programming Inception

Designing functionality that exhibits the properties of functional programming is hard because it requires a mentality change, coping with immutability and consideration for recursion, performance and polymorphism. This talk is a lesson in FP design that makes use of Scala’s hybrid OOP+FP nature.

We are going to start from Scala’s (and Java’s) ubiquitous Iterator/Iterable types which expose the famous iterator pattern, analyzing its strengths and weaknesses. And then we are going to work our way up to a fully featured FP replacement that has referential transparency and that fixes everything that’s wrong with Iterator, while being more generic.

This lesson in design involves talking about immutability, imperative programming, asynchrony and problems encountered when going FP, like performance considerations, recursion and memory leaks. We are also going to talk about ADTs, higher kinded polymorphism and type-classes versus OOP subtyping. Interestingly the example presented will use both OOP subtyping and type-classes and thus we can make a clear comparison about what to use and when - a problem that the Scala developer has in his daily work.

A working knowledge of the Scala programming language is required. Knowledge about Scala’s Future or the Task data type in Monix or from Scalaz is recommended, but not required.

BIO: Alexandru Nedelcu, @alexandru on GitHub/Gitter and @alexelcu on Twitter, is the lead developer of the Monix library.

Alexandru Nedelcu

March 14, 2017
Tweet

More Decks by Alexandru Nedelcu

Other Decks in Programming

Transcript

  1. FUNCTIONAL PROGRAMMING INCEPTION PROPERTIES OF FP ▸FP <=> Programming with

    Values ▸Referential Transparency ▸Composability, Reason
  2. ITERATOR PROBLEMS ? ▸ Synchronous Only ▸ No Backed-in Resource

    Managed ▸Minefield for Stack Overflows
  3. FP DESIGN KEY INSIGHTS 1. Freeze Algorithms into Data-Structures 2.

    Think State Machines
 (most of the time)
  4. FP DESIGN KEY INSIGHTS 1. Freeze Algorithms into Data-Structures 2.

    Think State Machines 3. Be Lazy 
 (Strict Values => Functions ;-))
  5. FP DESIGN KEY INSIGHTS 1. Freeze Algorithms into Data-Structures 2.

    Think State Machines 3. Be Lazy 4. Evaluate Effects w/ Stack-safe Monads 
 (e.g. IO, Task, Free)
  6. ASYNCHRONY EVALUATION IN SCALA Eager Lazy Synchronous A () =>

    A Asynchronous (A => Unit) => Unit () => (A => Unit) => Unit
  7. ASYNCHRONY EVALUATION IN SCALA Eager Lazy Synchronous A () =>

    A Function0[A] Asynchronous (A => Unit) => Unit () => (A => Unit) => Unit Future[A] Task[A]
  8. ASYNCHRONY MONIX TASK ▸ High-performance ▸ Lazy, possibly asynchronous behaviour

    ▸ Allows for cancelling of a running computation ▸ https://monix.io/docs/2x/eval/task.html
  9. HIGHER-KINDED POLYMORPHISM OOP VS PARAMETRIC POLYMORPHISM ▸ OOP is about

    Information Hiding
 (in types too) ▸ OOP handles Heterogeneity ▸Parametric Polymorphism is compile-time ▸Fundamentally changes behaviour based on plugged-in types
  10. HIGHER-KINDED POLYMORPHISM OOP VS PARAMETRIC POLYMORPHISM ▸ ArrayIterator vs ListIterator

    ▸ Iterant[Task, _] vs Iterant[Eval, _] ▸One is hiding implementation details ▸The other is about composition
  11. HIGHER-KINDED POLYMORPHISM PROBLEMS ▸ Pushes compiler to its limits ▸

    Unfamiliarity for users ▸Not all needed type-classes are available, design can be frustrating
 https://github.com/typelevel/cats/pull/1552
 (39 comments and counting)

  12. FUNCTIONAL PROGRAMMING PERFORMANCE PROBLEMS ▸Linked Lists are everywhere in FP

    ▸Linked Lists are terrible ▸Async or Lazy Boundaries are terrible
  13. FUNCTIONAL PROGRAMMING PERFORMANCE SOLUTIONS ▸Linked Lists are everywhere in FP

    ▸Linked Lists are terrible ▸Async or Lazy Boundaries are terrible ▸Find Ways to work with Arrays and ▸… to avoid lazy/async boundaries
  14. FUNCTIONAL PROGRAMMING INCEPTION TAKEAWAYS ▸ Freeze Algorithms into Immutable Data-Structures

    ▸ Describe State Machines ▸ Be lazy, suspend side-effects with Task/Free/IO ▸ Be lawful, use ScalaCheck/QuickCheck ▸ Performance matters (for libraries)
  15. FUNCTIONAL PROGRAMMING INCEPTION TAKEAWAYS ▸ Libraries: 
 Monix, Cats, ScalaCheck

    ▸ Generic Iterant implementation:
 https://github.com/monix/monix/pull/280 ▸ Simplified Task-based implementation:
 https://github.com/monix/monix/pull/331