Lazy evaluation and declarative approach in Python

by Alexey Kachayev, 2012

About me


  • CTO at Kitapps Inc.
  • CPython contributor
  • Production experience: Python, Erlang, Scala, Go, Clojure, Java, JS, C
  • Looked at: Haskell, Lisp, Scheme

Goals


  • Short functional paradigm presentation
  • Other point of view on algorithmic problems
  • Python iterators/generators: advanced usage
  • Lazy sequences to work with infinite data structures

More How, less Why


(read this presentation only with code samples!)

About declarative programming


  • Imperative programming (С/C++, Java)
  • Declarative programming
    • Functional programming (Haskell, Scheme, OCaml)
    • Logic programming (Prolog, Clojure core.logic)



  • IP = computation in terms of statements that change a program state
  • FP = computation as the evaluation of mathematical functions and avoids state and mutable data

Advanced: Declarative fibonacci


Our goal:

f = Stream()
fib = f << [0, 1] << map(add, f, drop(1, f))

assert list(take(10, fib)) == [0,1,1,2,3,5,8,13,21,34]
assert fib[20] == 6765
assert fib[30:35] == [832040,1346269,2178309,3524578,5702887]

Lazy, declarative


Stream implementation:

stream.py

Conclusions


Pro

  • conciseness
  • possible parallel execution


Con

  • immutability doesn't supported

The end


Thank you for attention!

  • Alexey Kachayev
  • Email: kachayev@gmail.com
  • Twitter: @kachayev
  • Github: kachayev


This presentation:

https://github.com/kachayev/talks/kharkivpy#6