News

Introducing Coconut for making functional programming in Python simpler

2 min read

Adding to the list of Python-like programming languages is Coconut that allows developers write simple and elegant functional code, while using the familiar Python environment and libraries. It is basically superset of Python that adds on top of Python syntax and is inspired by programming languages like Haskell, CoffeeScript, F#, and patterns.py. The language is written by Evan Hubinger, an undergraduate student studying mathematics and computer science at Harvey Mudd College.

Why Coconut was developed?

Writing functional programs in Python can be challenging at times. While Python does allow developers to write high-order functions, it lacks concise syntax for lambdas, boilerplate-less pattern matching, etc. One Hacker News user mentioned, “It’s capable of functional programming in the sense that functions are first class values that you can pass around, but it is infamously hostile to functional programming. For example, Python still doesn’t have multiline lambdas, and the justifications for why always boil down to it being “unpythonic”.”

This is what Coconut tries to solve by bringing tools of modern functional programming. It provides a clean -> operator replacing Python’s lambda statements. It supports pipeline-style programming, partial application, pattern-matching, destructuring assignment, parallel programming, and more. To provide optional static type-checking you can integrate Coconut with MyPy, which is an optional static type checker for Python. But, it is not purely functional that allows programmers to choose whatever programming style they are comfortable with.

As it is a variant of Python, developers who have experience in working on Python will not have much difficulty in learning it. Coconut code are compiled to Python code. Developers can access the Coconut compiler through its command-line utility, which also comes with an interpreter to enable real-time compilation. Additionally, it also supports the use of IPython/Jupyter notebooks.

What are the Python version it supports?

It supports these Python versions: >= 2.6 on the 2.x branch or >= 3.2 on the 3.x branch. To make Coconut built-ins universal across Python versions, it automatically overwrites Python 2 built-ins with their Python 3 counterparts. It also overwrites some Python 3 built-ins for optimization and enhancement purposes. If developers want to access the original Python versions of any overwritten built-ins, they can retrieve the old built-in by prefixing them with py_.

Read Next

pandas will drop support for Python 2 this month with pandas 0.24

Qt for Python 5.12 released with PySide2, Qt GUI and more

Google researchers introduce JAX: A TensorFlow-like framework for generating high-performance code from Python and NumPy machine learning programs

Bhagyashree R

Share
Published by
Bhagyashree R

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago