Pipelines
Write data transformations in the order they happen.
"hello, world!" |> print ( range(10) |> filter$(x => x % 2 == 0) |> map$(.**2) |> list |> print )
Simple, elegant, Pythonic functional programming.
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.
Coconut aims to enhance the repertoire of Python programmers to include the tools of modern functional programming in as Pythonic a way as possible. Additionally, Coconut code runs the same on any Python version, meaning that even if you just use Coconut to write pure Python, you no longer have to worry about any core Python version differences: Coconut can backport almost all modern Python features as far back as Python 2.6.
Installing Coconut is as easy as
pip install coconut
Then run coconut for an interactive interpreter, or coconut-run my_script.coco to compile and run a file.
Write data transformations in the order they happen.
"hello, world!" |> print ( range(10) |> filter$(x => x % 2 == 0) |> map$(.**2) |> list |> print )
Fill in some arguments now with $ (and ? for the gaps), and write short functions with =>.
powers_of_two = map$(pow$(2)) range(8) |> powers_of_two |> list |> print squares = map$(pow$(?, 2)) range(8) |> squares |> list |> print
Python's match on every Python version, plus match expressions, destructuring assignment, and pattern-matching functions.
def describe(point) = match point: case (0, 0): "the origin" case (x, 0): f"on the x-axis at {x}" case (0, y): f"on the y-axis at {y}" case (x, y): f"at ({x}, {y})" (3, 0) |> describe |> print [first] + rest = [1, 2, 3] print(first, rest)
case data defines a type that's exactly one of its variants. Forget to handle one, and type checking (coconut --pyright) tells you which.
case data Shape: case Circle(radius: float) case Rect(width: float, height: float) def area(shape: Shape) -> float = match shape: case Circle(r): 3.14159 * r ** 2 case Rect(w, h): w * h [Circle(1), Rect(2, 3)] |> map$(area) |> list |> print
data types are immutable, hashable, and pattern-matchable, and update sections make updated copies.
data Account(owner: str, balance: int) def deposit(acct: Account, amount: int) -> Account = acct |> (.balance += amount) Account("ada", 10) |> deposit$(amount=5) |> print
Define a function case by case with case def, with patterns and guards right in the arguments.
case def factorial: case(0) = 1 case(int(n) if n > 0) = n * factorial(n - 1) def head_tail([head] + tail) = (head, tail) factorial(10) |> print [1, 2, 3] |> head_tail |> print
Build infinite sequences with :: and iterate, and slice any iterator lazily with $[].
@recursive_generator def fibs() = (0, 1) :: map((+), fibs(), fibs()$[1:]) fibs()$[:10] |> list |> print powers_of_two = iterate((.*2), 1) powers_of_two$[:10] |> list |> print
Build functions out of other functions with ..>, operator functions like (*), and implicit partials like .split().
first_three_words = .split() ..> .$[:3] ..> " ".join sentence = "Coconut is a functional language" sentence |> first_three_words |> print product = reduce$((*)) range(1, 6) |> product |> print
Recursive functions don't run out of stack.
def sum_to(n, total=0): if n == 0: return total return sum_to(n - 1, total + n) sum_to(100_000) |> print
Coconut's type syntax (int -> str, int[], int?, and more) compiles to standard Python types that type checkers understand.
def twice(f: int -> int) -> int -> int = f ..> f def first(xs: int[]) -> int? = xs[0] if xs else None twice(x => x * 3)(2) |> print [] |> first |> print
Swap map for process_map or thread_map to run it in parallel.
def square(x) = x ** 2 if __name__ == "__main__": range(10) |> process_map$(square) |> list |> print
And much more: see the documentation for everything Coconut can do.
Coconut runs right here in your browser, compiled and run with Pyodide. Edit the code and press Run (or Ctrl+Enter).
Coconut's built-ins are fully typed, and coconut --pyright (or --mypy) type checks your code with errors pointing at your Coconut source, including exhaustiveness checking for match:
shapes.coco:5: error: Argument of type "UnhandledCaseDataVariants[Rect]" cannot be assigned to parameter "leftover" of type "ExhaustiveMatch"
Coconut's language server (coconut --lsp) gives you errors as you type, completions, hover documentation, go to definition, and renaming. Get it with the VS Code extension, the Sublime Text package, or any editor with LSP support, like Neovim.
Run coconut for an interactive interpreter with syntax highlighting, or coconut --jupyter notebook for Coconut in Jupyter. Coconut also works with IPython, xonsh, and pytest.
Coconut ships a guide to idiomatic Coconut for AI assistants (coconut --ai-docs), an MCP server (coconut --mcp), and a Claude Code plugin that gives Claude the language server.
Like what you see? Don't forget to star Coconut on GitHub!
Ready to get started? Here are some links to help you out: