Dysfunctional Programming (phpnw13)

Dysfunctional Programming (phpnw13)

A4b95be2145cc46f891707b6db9dd82d?s=128

Igor Wiedler

October 05, 2013
Tweet

Transcript

  1. Dysfunctional Programming

  2. @igorwesome

  3. None
  4. None
  5. None
  6. Immuteability: The property of functional programmers that prevents them from

    shutting up about pure functional programming. – Reg Braithwaite @raganwald
  7. dys·func·tion·al

  8. php λ

  9. Paradigms

  10. procedural Object- Oriented functional logic

  11. Imperative Declarative

  12. Structure Time & State

  13. Structure

  14. None
  15. ‘sup?

  16. None
  17. it’s not about the money

  18. • Message passing • Interfaces • Composition • (Not inheritance)

  19. Actor model: Asynchronous

  20. None
  21. None
  22. f g h

  23. tools and materials

  24. • Value semantics • Higher-order functions • Lazy evaluation

  25. The Garbage Overfloweth

  26. $numberCollection = new NumberCollection(); $numberParser = new SimpleNumberStringParser(); $firstParsedNumber =

    $numberParser->parse('1'); $firstNumber = new SimpleNumber($firstParsedNumber); $firstNumberProxy = new CollectionItemNumberProxy($firstNumber); $numberCollection->add($firstNumberProxy); $secondParsedNumber = $numberParser->parse('1'); $secondNumber = new SimpleNumber($secondParsedNumber); $secondNumberProxy = new CollectionItemNumberProxy($secondNumber); $numberCollection->add($secondNumberProxy); $addition = new AdditionOperator('SimplePHPEasyPlus\Number\SimpleNumber'); $operation = new ArithmeticOperation($addition); $engine = new Engine($operation); $calcul = new Calcul($engine, $numberCollection); $runner = new CalculRunner(); $runner->run($calcul); $result = $calcul->getResult(); $numericResult = $result->getValue();
  27. 1 + 1

  28. Composition

  29. c a b a b

  30. a b c

  31. Higher-order functions

  32. Dependency Injection

  33. Inversion of control is really just a pretentious way of

    saying “Taking an argument”. – Rúnar Óli @runarorama
  34. Taking an argument

  35. g g’

  36. 0 1 2 3 4 even? 0 2 4

  37. 0 1 2 3 4 #(+ 5 %) 5 7

    9 6 8
  38. 3 6 7 9 + 0 9 25 3 16

    acumulator
  39. Datastructures

  40. Number 42 String "Hello, cruel world." Map {:foo "bar", :baz

    "qux"} List [:a :b :c] Set #{"Arthur Dent" "Ford Prefect"}
  41. function is a means of abstraction

  42. data is a means of abstraction

  43. Structure Time & State

  44. Time & State

  45. Place-Oriented Programming

  46. None
  47. None
  48. None
  49. None
  50. identity

  51. new Foo("x") !== new Foo("x")

  52. defensive copy

  53. $users = new UserList(); $users->add(new User("Igor")); notify($users);

  54. while (count($users)) { $user = $users->shift(); notifyUser($user); }

  55. notify(clone $users)

  56. observation

  57. $user->getName()

  58. $user["name"]

  59. Value

  60. Primitives

  61. Value Object

  62. date address money response request

  63. serialization

  64. History

  65. Objects are forgetful

  66. $sophie->setName("Joanna")

  67. 2013-09-13 01:01:47 Sophie 2013-09-13 01:02:25 Joanna 2013-09-13 01:05:31 Caroline

  68. $joanna = $sophie->setName("Joanna")

  69. Git

  70. 98ca9 34ac2 f30ab master Snapshot A Snapshot B Snapshot C

  71. Epochal Time Model

  72. v1 v2 v3 v4 f f f identity states events

  73. Lazy evaluation

  74. Iterator Generator Stream

  75. a b c d a’ b’ c’ d’ consume

  76. Order of execution becomes implicit

  77. ๏ filter ๏ map ๏ reduce

  78. What about I/O?

  79. Filesystem Database Network }is a place

  80. Separate I/O from computation

  81. None
  82. What about testing?

  83. But I heard static methods are untestable and evil

  84. You cannot mock functions

  85. Why do we mock?

  86. Functions are fast

  87. Impure parts are mocked by passing functions

  88. my_beautiful_program(function () { return do_evil_shit(); })

  89. Pure functions are inherently testable

  90. But functions make code look awkward!

  91. strlen(get_name(get_user($data)))

  92. $data |> get_user |> get_name |> strlen

  93. pipeline('get_user', 'get_name', 'strlen')

  94. But PHP does not properly support functions!

  95. use function igorw\edn\parse; $data = parse($edn);

  96. PHP 5.6 will hopefully get function autoloading

  97. Applicability of the functional paradigm

  98. f g h i j k l

  99. A foo foo foo foo B foo foo C foo

    foo D foo foo E foo foo
  100. Granularity

  101. None
  102. Queue

  103. ๏ Value semantics ๏ Higher-order functions ๏ Lazy evaluation

  104. pure functions

  105. None
  106. Questions? • joind.in/{ 9471, 9472 } • github.com/igorw/edn • github.com/nikic/iter

    • @igorwesome
  107. References • The Paradigms of Programming by Robert W. Floyd

    • Execution in the Kingdom of Nouns by Steve Yegge • The Value of Values by Rich Hickey • Are we there yet? by Rich Hickey