Slide 1

Slide 1 text

Fn.py ideas and internals Wednesday, March 27, 13

Slide 2

Slide 2 text

Fn.py https://github.com/kachayev/fn.py Wednesday, March 27, 13

Slide 3

Slide 3 text

About me • Alexey Kachayev • CTO at KitApps Inc. • Open source activist • Functional programming advocate • Erlang, Python, Scala, Clojure, Go, Haskell • @kachayev • github.com/kachayev • kachayev at gmail.com Wednesday, March 27, 13

Slide 4

Slide 4 text

About Python • minimalistic syntax • dynamically typed • imperative (by design) • “multi paradigm” • functions are variables • lambdas • iterators, generators, decorators, ... • mutable data types (mostly) Wednesday, March 27, 13

Slide 5

Slide 5 text

About Fn.py • enjoy FP in Python • developed on my own talks materials • not only proof of concept • tremendous support from community Wednesday, March 27, 13

Slide 6

Slide 6 text

Overview • Scala-style lambdas • Stream and infinite sequences declaration • TCO / trampolines • Itertools recipes • Option monad • Python 2/3 unification for functional stuff Wednesday, March 27, 13

Slide 7

Slide 7 text

Scala-style lambdas Wednesday, March 27, 13

Slide 8

Slide 8 text

Wednesday, March 27, 13

Slide 9

Slide 9 text

Scala-style lambdas Wednesday, March 27, 13

Slide 10

Slide 10 text

Scala-style lambdas Wednesday, March 27, 13

Slide 11

Slide 11 text

Why? • Code readability • Improve you vision • Keyboard will work longer Wednesday, March 27, 13

Slide 12

Slide 12 text

How? • _Callable class • shortcut instance • overloaded operators • each instance - unary or double-side operations tree Wednesday, March 27, 13

Slide 13

Slide 13 text

Streams and infinite sequences Wednesday, March 27, 13

Slide 14

Slide 14 text

What? • Lazy evaluated list • Composable producers • Any number of consumers • All consumers share one iterable origin • Infinite (potentially) sequence Wednesday, March 27, 13

Slide 15

Slide 15 text

Wednesday, March 27, 13

Slide 16

Slide 16 text

So what? Wednesday, March 27, 13

Slide 17

Slide 17 text

Haskell Wednesday, March 27, 13

Slide 18

Slide 18 text

Clojure Wednesday, March 27, 13

Slide 19

Slide 19 text

Scala Wednesday, March 27, 13

Slide 20

Slide 20 text

Python Wednesday, March 27, 13

Slide 21

Slide 21 text

Tail call optimization “I don't believe in recursion as the basis of all programming. This is a fundamental belief of certain computer scientists, especially those who love Scheme and like to teach programming by starting with a "cons" cell and recursion.” Guido van Rossum Wednesday, March 27, 13

Slide 22

Slide 22 text

Wednesday, March 27, 13

Slide 23

Slide 23 text

Okay.... Wednesday, March 27, 13

Slide 24

Slide 24 text

Trampolines unwrap tail calls to while loop Wednesday, March 27, 13

Slide 25

Slide 25 text

Recursion = Induction recursion = induction basis + step while loop = induction basis + step Wednesday, March 27, 13

Slide 26

Slide 26 text

Trampoline Wednesday, March 27, 13

Slide 27

Slide 27 text

Control flow patterns Wednesday, March 27, 13

Slide 28

Slide 28 text

Wednesday, March 27, 13

Slide 29

Slide 29 text

Scala-style Option Wednesday, March 27, 13

Slide 30

Slide 30 text

Wednesday, March 27, 13

Slide 31

Slide 31 text

Wednesday, March 27, 13

Slide 32

Slide 32 text

What? • analog = list with 1 or 0 elements • functor + fmap • monadic flatMap operation • “retry” computations Wednesday, March 27, 13

Slide 33

Slide 33 text

When? • computations that may fail • heavy execution branching (if/else/try/etc) Wednesday, March 27, 13

Slide 34

Slide 34 text

Where to find more? https://github.com/kachayev/fn.py Wednesday, March 27, 13

Slide 35

Slide 35 text

Questions? http://kachayev.github.com/talks Wednesday, March 27, 13