Coconut Sandor aguilar
What is Coconut? Open source derivative of Python Introduces friendlier functional programming to Python by adding built-in syntactical support Compiles to Python Unique in that it is Python version agnostic Works ALMOST the same on any version of Python “
Why Coconut? “… if you look at the top 20 most popular programming languages, not a single one is functional. But there are functional alternatives to a lot of them. Java has Scala. C# has F#. Python has ... nothing.” - Evan Hubinger, creator of Coconut
Pipeline and Lambdas Pipeline style programming Prettier Lambdas “Hello, World!” |> print instead of print(“Hello, World!) In Coconut for multiple argument: def sq(x) = x**2 (1, 2) |*> (+) |> sq |> print In Python: import operator def sq(x): return x**2 print(sq(operator.add(1, 2))) Prettier Lambdas (x) -> x**2 instead of lamda x: x**2 In Python: map(lambda x: x * x, [0, 1, 2, 3, 4]) In Coconut: map(x -> x*x, [0, 1, 2, 3, 4]) |> list
Partial Application and Infix Partial Application (i.e. currying) In Coconut: range(5) |> map$((x) -> x*2) |> list produces [0, 2, 4, 6, 8] In Python: import functools print(map(functools.partial(lambda x: x*2), range(5))) Infix notation def a `mod` b = a % b 5 ‘mod’ 3 == 2
Lazy Lists and Pattern Matching In Coconut: (| range(100) |> list |) In Python: Need to import python package Destructing Assignment {”list”: [0] + rest} = {“list”: [0, 1, 2, 3]} rest = [1, 2, 3] Pattern matching match [head] + tail in [0, 1, 2, 3]: print(head, tail) produces [1, 2, 3] 0
And More Parallel programming Tail recursion optimization range(10) |> parallel_map$( (**)$(2)) |> list produces [1, 2, 4, 8, 16, 32, 64, 128, 256, 512] Tail recursion optimization
Further Information Coconut Documentation: http://coconut.readthedocs.io/en/master Coconut Tutorial: http://coconut.readthedocs.io/en/master/HELP.html Coconut Github repository: https://github.com/evhub/coconut