Coconut

Coconut is a functional programming language that compiles to Python. Since all valid Python is valid Coconut, using Coconut will only extend and enhance what you're already capable of in Python.

Why use Coconut? Coconut is built to be useful. Coconut enhances the repertoire of Python programmers to include the tools of modern functional programming, in such a way that those tools are easy to use and immensely powerful; that is, Coconut does to functional programming what Python did to imperative programming. And Coconut code runs the same on any Python version, making the Python 2/3 split a thing of the past.

Installing Coconut is as easy as

installing Python, opening a command-line prompt, and entering:

pip install coconut

which will give you access to all the features of Coconut, which adds to Python built-in, syntactical support for:

pipeline-style programming

"hello, world!" |> print

prettier lambdas

x -> x ** 2

partial application

range ( 10 ) |> map $ ( pow $ ( ? , 2 )) |> list

pattern-matching

match [ head ] + tail in [ 0 , 1 , 2 , 3 ]: print ( head , tail )

destructuring assignment

{ "list" : [ 0 ] + rest } = { "list" : [ 0 , 1 , 2 , 3 ]}

infix notation

5 ` mod ` 3 == 2

operator functions

product = reduce $ ( * )

function composition

( f .. g .. h )( x , y , z )

lazy lists

( | first_elem () | ) :: rest_elems ()

parallel programming

range ( 100 ) |> parallel_map $ ( pow $ ( 2 )) |> list

tail call optimization

def factorial ( n , acc = 1 ): case n : match 0 : return acc match _ is int if n > 0 : return factorial ( n - 1 , acc * n )

algebraic data types

data Empty () data Leaf ( n ) data Node ( l , r ) def size ( Empty ()) = 0 addpattern def size ( Leaf ( n )) = 1 addpattern def size ( Node ( l , r )) = size ( l ) + size ( r )

and much more!

Like what you see? Don't forget to star Coconut on GitHub!

Star

Ready to get started? Here are some links to help you out: