8 ms·
Coconut: Simple, elegant, Pythonic functional programming
- vmchale 6y agoCool stuff! Python's ecosystem for data science is amazing, it's definitely good to be able to use a functional language with them.
- ghj 6y agoOMG there are multiline lambdas!!! https://coconut.readthedocs.io/en/master/DOCS.html#statement-lambdas https://coconut.readthedocs.io/en/master/DOCS.html#statement... 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-lambda-in-python-why-not https://stackoverflow.com/questions/1233448/no-multiline-lam... # 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])
- knocte 6y agoIn case you dont want to go hybrid but go all the way: https://github.com/knocte/2fsharp/blob/master/python2fsharp.md https://github.com/knocte/2fsharp/blob/master/python2fsharp....
- smabie 6y agoThis 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.
- zelphirkalt 6y agoI think "whitespace matters" and Python's quite uninspired way of writing procedure calls make it difficult to really innovate in the syntax area, without breaking backwards compatibility and without causing ambiguity.
- laretluval 6y agoBoring, unchanging syntax is a plus.
- mumblemumble 6y agoFunctional is my preferred paradigm. I do a lot of work in Python, too, and often find myself rubbing up against Python's less-than-complete support for FP. That being said, I am quite happy that Python does not have most of this stuff. It's good for a language to pick a paradigm, and stick to it. Trying to be all things to all people is the best way to grow a good language into a great big mushball. Python, for its part, is, first and foremost, a procedural imperative language. With some facilities for object-oriented and functional programming, yes, but its heart and soul are procedural. And, unless we're all ready for a backward-compatibility crisis that's even worse than 2->3, that's not changing any time soon. And that's fine. Despite it not being a paradigm that anyone has considered particularly sexy for a good 30 years now, for the problem domains where Python is most successful, procedural programming is a perfectly good way to work. Perhaps even the ideal one. If that means that Python doesn't remain one of the world's most popular languages for the foreseeable future, that's fine. I'd honestly rather switch to a new clean language every so often than get stuck permanently on a Frankenstein's monster cobbled together from all of the most popular programming language design trends from every decade of my life. That said, I don't think that Python's existential peril is quite as great as you're making it out to be. It's got a truly impressive amount of staying power. I've spent a good two decades now watching this language repeatedly survive its own death.
- dang 6y agoIf 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
- dig1 6y agoThose 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
- cutler 6y ago"... compiles to Python"? Am I missing something or shouldn't this just be a library?
- ihumanable 6y agoThey 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.
- satvikpendem 6y agoSomewhat related is the language Nim: Pythonic, typed, and compiles to native code so it's super fast.
- FridgeSeal 6y agoNim 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.
- wendyshu 6y agoI've used it in the past but there were subtle interoperability issues with existing python libraries and I gave up.
- codezero 6y agoFolks 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.
- mark_l_watson 6y agoI 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/