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

Pragmatic Purity

Pragmatic Purity

Purely functional code is easier to reason about ... is the functional programmer's guiding mantra. But to what extent is it really easier to reason about? This short talk will look at and challenge this claim, and propose a pragmatic attitude towards purity.

Avatar for Bill Venners

Bill Venners

March 21, 2018

Other Decks in Technology

Transcript

  1. def puzzle(size: Int): List[Int] = { val array = new

    Array[Int](size) var i = 0 while (i < size) { array(i) = i + 1 i += 1 } array.toList } puzzle(3) Pragmatic Purity
  2. def puzzle(size: Int): List[Int] = { val array = new

    Array[Int](size) var i = 0 while (i < size) { array(i) = i + 1 i += 1 } array.toList } puzzle(3) 1. Go into an infinite loop Pragmatic Purity
  3. def puzzle(size: Int): List[Int] = { val array = new

    Array[Int](size) var i = 0 while (i < size) { array(i) = i + 1 i += 1 } array.toList } puzzle(3) 1. Go into an infinite loop 2. Return List(1, 2, 3) Pragmatic Purity
  4. def puzzle(size: Int): List[Int] = { val array = new

    Array[Int](size) var i = 0 while (i < size) { array(i) = i + 1 i += 1 } array.toList } puzzle(3) 1. Go into an infinite loop 2. Return List(1, 2, 3) 3. Return List(1, 2, 3, 4) Pragmatic Purity
  5. def puzzle(size: Int): List[Int] = { val array = new

    Array[Int](size) var i = 0 while (i < size) { array(i) = i + 1 i += 1 } array.toList } puzzle(3) 1. Go into an infinite loop 2. Return List(1, 2, 3) 3. Return List(1, 2, 3, 4) 4. Throw ArrayIndexOutOfBoundsException Pragmatic Purity
  6. Pragmatic Purity Pure code and… …impure code that doesn’t force

    you to keep track of too many things at once. Pragmatic Purity
  7. • object instantiation, initialization, relocation, and reclamation • lazy initialization

    • caching • referentially transparent on the outside, “minimally mutating” on the inside • tail call optimization • logging Pragmatic Purity
  8. http://www.artima.com Can one formally define the rules imperative code must

    follow so that it can be used “on the inside” while not violating referential transparency “on the outside?”