
Coconut: Simple, elegant, Pythonic functional programming - shawndellysse
http://coconut-lang.org/
======
smabie
This is cool stuff, but it's kind of sad that Python doesn't have any of this
stuff.

Apparently the Pythonic way to do anything is to repeat yourself, type a lot,
implement flatten yourself, and cripple lambdas so no one will want to use
them.

It's a shame, because there's no reason why Python couldn't be an acceptable
language for FP, like Julia. It'll never be great at it of course (just like
Julia), but good enough that when I'm doing some ML/stats work in Python, I
don't have to rip my hair at because of all the imperative stuff I'm
practically forced to do.

Python still feels very much like a 90s programming language, except for maybe
the async syntax. I'm not sure what exactly went wrong with the development,
but the dev team needs to start shaping up or Python risks becoming completely
obsolete in the next 5 or 10 years.

People might say it's hard to develop a language or whatever, but just look at
Julia. Superior in every way imaginable and a much younger language. I've
mostly switched over to Julia except for some legacy Python ML projects, and
can confidently say there's no reason at all to start a new project in Python.

The Python dev team needs to do something drastic if they want Python to
survive. They've been slowly boiled alive by conservatism and a host of
missteps. I doubt they'll do anything to save Python, but I hope they at least
try. Implementing some Coconut features would be a great start.

~~~
cutler
Can anything really be done by a single-threaded, GIL-laden, dynamic language
trying to compete with a modern, natively parallel, JIT-ed language like
Julia? That's a bit apples to oranges, no? Once other languages like Go,
Swift, Kotlin, Rust and Julia catch-up with Python's range of data science
libraries I'd say Python's days are numbered. Python is the Betmax of
programming languages and FP is where the shallowness of its design really
shows.

~~~
helgie
Natively parallel? From [https://docs.julialang.org/en/v1/base/multi-
threading/](https://docs.julialang.org/en/v1/base/multi-threading/) : "Julia
is not yet fully thread-safe. In particular segfaults seem to occur during I/O
operations and task switching."

One solution for numerical code at least is to use Numba to JIT-compile (with
LLVM, as Julia does) code that can be run without using the GIL, which can be
run in parallel on top of Python's threading mechanisms (threading.Thread,
concurrent.futures), or with Numba's other options (parallel-for, CUDA
kernels, CPU or GPU vectorized Ufuncs/GUfuncs, stencil kernels).

------
ghj
OMG there are multiline lambdas!!!
[https://coconut.readthedocs.io/en/master/DOCS.html#statement...](https://coconut.readthedocs.io/en/master/DOCS.html#statement-
lambdas)

But trying it out, it doesn't even look like python anymore. Especially if you
want to nest them a lot like in other callback heavy languages. It requires a
lot of semicolons and parens to get past the parsing ambiguity problem:
[https://stackoverflow.com/questions/1233448/no-multiline-
lam...](https://stackoverflow.com/questions/1233448/no-multiline-lambda-in-
python-why-not)

    
    
        # Incorrect, the lambda returns (y, [1,2,3]) here!
        map(def (x) ->
              y=x+1;
              return y
        , [1,2,3])
        
        # Right
        map((def (x) ->
              y=x+1;
              return y
        ), [1,2,3])

~~~
eindiran
A more Coconutty way to write this might be:

    
    
        [1, 2, 3] |> map$(def (x) -> y = x + 1; return y)
    

Which looks even less Pythonic.

------
dig1
Those who like clojure/lisp, can try with Hy [1]. The latest version removed
Python 2 support, but previous versions would work on Python 2 and 3. Can
directly run hy code, compile to pyc or readable python source files.

For functional programming aficionados that still prefer python, don't forget
PyToolz [2] or single-source-file underscore.py [3].

[1] [https://docs.hylang.org/en/stable/](https://docs.hylang.org/en/stable/)

[2]
[https://toolz.readthedocs.io/en/latest/](https://toolz.readthedocs.io/en/latest/)

[3]
[https://github.com/serkanyersen/underscore.py](https://github.com/serkanyersen/underscore.py)

------
satvikpendem
Somewhat related is the language Nim: Pythonic, typed, and compiles to native
code so it's super fast.

~~~
FridgeSeal
Nim deserves more recognition: it has that same feeling of being fast and easy
to write, but has an actual type system and it's shockingly quick.

I'd argue as well, that Nim is actually what a lot of people are looking for
when they look at using Go.

------
mark_l_watson
I liked the code examples/snippets.

I use the Hy language (hylang) when I want a Lisp syntax for Python and I also
wrote a book on Hy [1] that can be read for free online.

The Python ecosystem, especially for deep learning, is awesome but some of us
troublemakers don’t much like Python’s syntax.

[1] [https://leanpub.com/hy-lisp-python/](https://leanpub.com/hy-lisp-python/)

------
knocte
In case you dont want to go hybrid but go all the way:
[https://github.com/knocte/2fsharp/blob/master/python2fsharp....](https://github.com/knocte/2fsharp/blob/master/python2fsharp.md)

------
dang
If curious see also

2016
[https://news.ycombinator.com/item?id=11960692](https://news.ycombinator.com/item?id=11960692)

2019
[https://news.ycombinator.com/item?id=18815125](https://news.ycombinator.com/item?id=18815125)

and these bits from recent weeks:

[https://news.ycombinator.com/item?id=23663010](https://news.ycombinator.com/item?id=23663010)

[https://news.ycombinator.com/item?id=23339351](https://news.ycombinator.com/item?id=23339351)

------
codezero
Folks here probably already know, but any time I see functional and python in
the same sentence, I like to remind folks about Hy:
[https://github.com/hylang/hy](https://github.com/hylang/hy)

I think it operates on the AST in python, which is also nifty.

------
vmchale
Cool stuff! Python's ecosystem for data science is amazing, it's definitely
good to be able to use a functional language with them.

------
wendyshu
I've used it in the past but there were subtle interoperability issues with
existing python libraries and I gave up.

------
cutler
"... compiles to Python"? Am I missing something or shouldn't this just be a
library?

~~~
ihumanable
They are adding new syntax and keywords, things that you can't really do with
a library.

The analog in the JS ecosystem would be typescript. Why isn't it just a
library, because they wanted to be able to introduce new syntax and new
constructs.

The little editor on the homepage shows the "compile to Python" step if you
click the little checkbox. It will list the python that the coconut code gets
transformed into. The code is relatively readable (for short snippets) and
this is the same approach that typescript took for adding new functionality on
top of an existing language.

