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

Scala Functional Programming Combinators Code Kata

Scala Functional Programming Combinators Code Kata

Download for much higher quality version of the slides. Start with expression ma flatMap f and keep refactoring it by applying each of ⑧ rewrite rules in turn, until you get back to ma flatMap f.

Code: https://github.com/philipschwarz/scala-fp-combinators-code-kata

Keywords: >=>, code kata, combinators, compose, composition, fish operator, flatmap, flatten, fp, function composition, functional programming, identity, identity function, kleisli composition, map, pure, refactoring, scala

Philip Schwarz

July 26, 2020
Tweet

More Decks by Philip Schwarz

Other Decks in Programming

Transcript

  1. Scala Functional Programming Combinators Code Kata start with expression ma

    flatMap f and keep refactoring it by applying each of ⑧ rewrite rules in turn until you get back to ma flatMap f @philip_schwarz slides by https://www.slideshare.net/pjschwarz
  2. Rewrite Rules ① flatmap can be defined in terms of

    map and flatten ② map can be defined in terms of flatMap and pure ③ flatten can be defined in terms of flatMap and identity ④ chained flatMaps are equivalent to nested flatMaps (flatMap associativity law) ⑤ Kleisli composition can be defined in terms of flatMap (apply this the other way around) ⑥ the identity function can be defined in terms of flatten and pure ⑦ pure followed by flatten cancel each other out ⑧ pure is the identity function for Kleisli composition, so f >=> pure is the same as f
  3. Only half-serious, of course. But you may find it useful

    to work through the refactorings to improve your understanding of various combinators. And if you can’t be asked to type it all out, I made a gist for you @philip_schwarz https://gist.github.com/philipschwarz/a677eaa7ccc87d54673226f307b3fdb5