Fn.py: enjoy FP in Python

by Alexey Kachayev, 2013




  • Twitter: @kachayev
  • Github: @kachayev
  • What we are going to talk about?



      fn.py  


    Prehistory

    Lambdas


    Scala

    List(1,2,3).map(_*2)
    

    Clojure

    (map #(* % 2) '(1 2 3))
    

    Haskell

    map (2*) [1,2,3]
    

    Python ???

    Lambdas


    Python !!!

    from fn import _
    from fn.iters import zipwith
    from itertools import repeat
    
    assert list(map(_ * 2, range(5))) == [0,2,4,6,8]
    assert list(filter(_ < 10, [9,10,11])) == [9]
    assert list(zipwith(_ + _)([0,1,2], repeat(10))) == [10,11,12]
    

    • code readability
    • improve your vision
    • keyboard will work longer

    Fibonacci in Python


    from fn import Stream
    from fn.iters import take, drop, map
    from operator import add
    
    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]

    Need more?


    • Simple syntax for partial application and composition
    • 22 additional itertools recipes
    • [TBD] Trampoline decorator
    • [TBD] Functional errors-handling
    • [TBD] Benchmarks, optimization, C-accelerators

    Want to help?


    • Use it and enjoy FP
    • Post bugs, ideas, advices, patches
    • Tell your friends



    Thanks for your attention