16 ms·
Coconut: Pythonic functional programming
- frutiger 8y agoNice effort and I really like the premise. I hate to be negative and I appreciate this may be due to the requirement to make Coconut a superset of Python, but the pattern matching and the partial application look anything but elegant (or Pythonic) to me.
- diarmuidc 8y agoLooking at the example code, it seems that this is perl-ifying Python. And I'm not saying that as a good thing. The code is more terse and less readable & understandable.
- orblivion 8y agoI'd like to see an actually pythonic Haskell. Needs the right designer.
- entelechy 8y agothis statement makes we wonder: what does pythonic exactly mean?
- orblivion 8y agoThe important part to me is good ergonomics from the point of view of the programmer and reader. Beyond that, some particulars: * Being terse is a lower priority than usual * Cool features are a lower priority than usual * Be careful how you use and then re-use symbols (Haskell can be confusing this way). * Use English (or whatever spoken language) in your symbols
- jakear 8y agoCoconut seems anti all of those. In particular all of the weird infix operators ( ` $.> `, ` :&$ `, etc) [1]. Gives me flashbacks to dealing with Haskell. [1]: I just made those up, but I wouldn’t be surprised if they were real.
- tayo42 8y agoI've started to hate the phrase pythonic at work. its ambiguous and leads to bikesheding.
- pseudalopex 8y agoWhen talking about a language it usually means the design principles summarized in "The Zen of Python"[1]. When talking about Python code it can also mean "idiomatic". [1] https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/
- voidhorse 8y agoIt's actually quite possible to write 'pythonic' haskell, if by pythonic you mean 'kind to human readers'. Many of the obscure < * > style infixes in Haskell have prefix counterparts ( for < * >: liftA2 id). Unfortunately infix is preferred by most haskellers, and the prefix english language forms derive their terminology from advanced branches of mathematics, which make them difficult to understand and use intuitively without sufficient background. That said, all of these things are symptoms of the preferences of the community around the language, and not restrictions imposed by the language design itself. Core Haskell is actually comprised of some pretty easily understandable functions. You can craft synonyms for more advanced abstractions such as applicative fairly easily once you understand the type system. Haskell more or less gives you everything you need to write functional programs in a way that reads like plain english--this is very uncommon however, given the language's origin and close connection with mathematics.
- a-saleh 8y agoI remember reading an old post by Gabriel Gonzales on exactly this topic: http://www.haskellforall.com/2015/09/how-to-make-your-haskell-code-more.html http://www.haskellforall.com/2015/09/how-to-make-your-haskel... On the other hand, once I did get used to some of the infix operators, I started to really like some of them, i.e. how: f data data becomes f <$> fancyData <*> fancyData where what makes the data fancy might be validation, being in container like list, or even some sort of reactive-ui-component, like Flare library for Purescript does: http://try.purescript.org/?backend=flare http://try.purescript.org/?backend=flare
- pmontra 8y agoElixir-ifying Python, maybe? The only Perlism I recognize are the dollar signs, but that could be PHP.
- deleted 8y ago[deleted]
- tarellel 8y agoI've done my fair share of Elixir. And this looks like you hit it right on the nail. Elixir is a great language, but I think Python is Python and Elixir is Elixir. We shouldn't me complicating any more than it already is (not that python is complicated, just adding another languages functionality another hurdle to learn).
- dnautics 8y agoIt's got the pipe operator, but that's about it.
- marcosdumay 8y agoIt's haskellfying it. Some of those are very needed constructions (like algebraic data types with pattern matching), but it's adding an entire sub-language into a language that is built on the goal of being simple. I'm not sold into it either.
- weberc2 8y agoI'm a big fan of ADTs and pattern matching, but I hardly see the point in a dynamic language. I also like Haskell because of its type safety and _in spite of_ its syntax, so this sort of feels like the worst of all worlds to me.
- cbdumas 8y agoI could be missing something but saying this language has ADTs is stretching the definition of ADT a bit much for me. ADTs are defined by combining simpler types (usually with sum and product types) but that does not seem to be possible in Coconut.
- marcosdumay 8y agoYes, it's stretching, but there is a section for it. The sum types are create by multiple data declarations joined by directives. It's ugly, messy, error-prone and as your sibling comment says, misses the point of enabling static verification.
- haskellandchill 8y agoVery disappointing, like why bother doing ADTs if you aren't going to do them right and have exhaustive pattern matching.
- jecxjo 8y agoThis is the reason why all of these "mashup" languages are so pointless. Everyone feels like X language is so great but if only we could add a feature from Y language to it. They then attempt to trivialize what is needed for that feature not realizing that the benefits of Y are due, in part, to the non-trivial amount of functionality. They almost always turn into a façade of the feature they truly want to support and they never gain traction because their usefulness just really isn't there.
- xbryanx 8y agoI always refer back to PEP 20 when thinking about these issues (and many things in life actually): https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/ "Explicit is better than implicit."
- deleted 8y ago[deleted]
- Majestic121 8y agoI'm a huge fan of the lambda syntax : x -> x * 2 It looks much cleaner than the traditional : lambda x : x * 2 Would a PEP adding this syntax have any chance to pass ?
- orblivion 8y agoI think Guido wanted to get rid of lambdas altogether one time. If anything I would guess they want to keep it ugly to discourage its use.
- leephillips 8y agoThen you should take a look a Julia, if you haven't already.
- Waterluvian 8y agoI've heard the argument that you ought to consider writing a named function and using it where the lambda would have gone. Self documents and cuts down on lines of symbol soup. Python wants to be broadly good enough for everyone, not perfect for any one person's specific domain and expertise level. I think this might be the right attitude.
- runarberg 8y agoJust came back to python after several years in javascript. I've found that the functions you need are often already provided in the standard library (for example the `operator` module[1]). So where in javascript you would write: const double = x => x * 2 In python you can simply write: from operator import mul double = mul(2) [1]: https://docs.python.org/2/library/operator.html https://docs.python.org/2/library/operator.html
- Franciscouzo 8y agoThat won't work, you have to use something like partial instead if you want to use the operator module: from functools import partial import operator double = partial(operator.mul, 2) And at that point I think it would way simpler to just define a normal function.
- weberc2 8y agoThis seems like a neat project but I can’t say I ever wanted Python to be an even bigger language... Ideally we would have a Python-like that eschews unnecessary features like inheritance and much of the magic that makes Python hard to optimize (and often hard to understand), and with a type system (even an optional type system) that is better than Mypy. I’m not 100% sure what that would look like, but I would ditch classes (and thus inheritance) for named tuples, make most things immutable by default, include enums and pattern matching (that the type system would check; I hardly see the point in dynamic pattern matching). If possible, typed code would compile to C extensions or similar. Oh, and exceptions would go away in favor of a Rust-like Result type.
- elcomet 8y agoYou should check nim out, it's a python-like language that compiles to C.
- azhenley 8y agoI also recommend looking at Nim. It has Python-like syntax but it feels very different than Python for me when using it. Stellar language though that has been getting a lot of attention!
- weberc2 8y agoThanks for the recommendations, folks. I'll take a look!
- JoBrad 8y agoWhoa - Nim seems really cool! Thanks for mentioning it. I like that it also compiles to JS, and seems to be pretty active (latest release was just on Dec 31).
- bakery2k 8y agoIsn't Nim, like Python, a large, complex language? Looking at the documentation, it seems to include a huge number of features.
- vages 8y agoThe pipe operator alone makes this worth using.
- flyte-solutions 8y agoAt Flyte Solutions, our success is measured by your success. For us it’s not just creating something that looks great; it needs to deliver results for you. We are Flyte Solutions, We build cost effective & strategic Software, Web & Mobile solution for Startup, SMBS & Large Enterprise. https://www.flytesolutions.com/best-software-company-in-bangladesh.html https://www.flytesolutions.com/best-software-company-in-bang...
- akhong 8y agoI recently gave a talk on Coconut in PyCon Bangkok: https://youtu.be/24DWw6Ozkvo https://youtu.be/24DWw6Ozkvo
- iamapipebomb 8y agoI think I could replace bash with xonsh[0] as my default shell if coconut's pipe syntax were available! [0] https://xon.sh/ https://xon.sh/ [1] https://github.com/xonsh/xonsh/issues/1336 https://github.com/xonsh/xonsh/issues/1336
- CodeSheikh 8y agoIs it just me who finds adjective "Pythonic" usage unnecessary and somewhat elitist? Let's be honest, there are no other language counterparts (Java as Javish?! or JS as JSish?!) for such adjective and "Pythonic" seems to be used by a sub-set of Python community. Basically it is idiomatic.
- smt88 8y agoI've seen C-like, Rusty, and ___Script (meaning related to JS). Lisp is a family. I'm not sure why you think "Pythonic" is some new vein of elitism, because to me it just seems like it's telling people that Coconut is easy to learn if you know/like Python already. That said, it immediately told me that I wouldn't be interested in Coconut, so I also found the label valuable as a non-Python person.
- mikekchar 8y agoTo be fair, back in the day, both Java and C# were intended to be C++-ish. Javascript itself was famously remodelled to be Java-ish. When Coffeescript came out, it intentionally modelled itself after the brevity of Python. There are lots of Lisp derivatives and Forth-likes (Forth-like is even a pretty established term). ML derivatives are popular. I don't think it's just Python. Edit: Of course I should have read TFA before replying. Of course this is not a Python like language, but a language that compiles to Python. I agree the cute name is confusing, but if the point is to compile to a specific language, saying what that language is is pretty important ;-)
- danso 8y agoBut Coconut seems to be a superset of Python (all valid Python is valid Coconut). Seems like a very good and succinct adjective to use to attract Python users who otherwise wouldn’t be interested in a new language.
- androidgirl 8y agoIt really does just mean idiomatic python. The word itself is almost a mash up of those two. And I don't see how that's a problem? Every language has a subculture around it, language specific jargon, even inside jokes. Pythonic stems from the idea of simple ways to do "stuff", and that's pretty much it.
- portal_narlish 8y agoHow are the "algebraic data types" algebraic if they don't express any relation to each other? This seems like a cool syntactic layer for people that are already writing Python and wishing for a more functional style. However to call this a functional language would betray the definition.
- agumonkey 8y agorelated, hy-lang (clojury python transpiler)
- hmaarrfk 8y ago> And Coconut code runs the same on any Python version, making the Python 2/3 split a thing of the past. My understand was that the big issue was the fact that [third party] libraries assume that strings were made of 8bits characters. How does coconut solve that issue? Seems a little misleading.... if you import enough things from `six` or `__future__` then you code will run the same on both python 2/3. some things like fancy tuple expansion aren't available if you work with that subset, but yeah, most things are OK.
- JoBrad 8y agoI haven't used Coconut beyond some dabbling last time it made HN's frontpage, so someone with more experience may be able to provide a better answer. However, the syntax is based on Python 3, and they use their own builtins for both Python 2 and 3 to maintain compatibility. Some functionality can't be back-ported to Python 2 (tuple unpacking with *, f strings, etc.), and there is more info about that at the link below. https://coconut.readthedocs.io/en/master/DOCS.html#compatible-python-versions https://coconut.readthedocs.io/en/master/DOCS.html#compatibl...
- hmaarrfk 8y agoThanks for the link. I wonder how they are doing tail call optimization in python
- eindiran 8y agoHere is where TCO is implemented in the Coconut compiler code: https://github.com/evhub/coconut/blob/07e311fb8f69861d30e58f0de7861f2ef7351503/coconut/compiler/compiler.py#L1611 https://github.com/evhub/coconut/blob/07e311fb8f69861d30e58f...
- zbentley 8y agoThat's clever and horrifying. Clever because writing a regex-based preprocessor for a complex, dynamic language that adds TCO and actually works in most cases is impressive. Horrifying because . . . it's a regex based preprocessor that rearranges arbitrary function code in a complex, dynamic language.
- bredren 8y agoI really enjoy coconuts and food products derived from coconuts, so the name is absolutely terrific as far as I'm concerned.
- wiradikusuma 8y agoSo it's like CoffeeScript (https://coffeescript.org/ https://coffeescript.org/) to JavaScript?
- lenticular 8y agoThis is pretty cool. I'm not a huge Python fan, but I wind up using it quite a bit for ML tasks. Functional programming in Python is essentially impossible, between nearly everything being mutable and the terrible one-line lambdas. I know Python is all about "there should be only one way to do it," but that typically doesn't map (ahaha) at all to how I think.
- enedil 8y agoYou say "ML" tasks. Do you mean StandardML or Ocaml?
- alangpierce 8y agoMy reading was "machine learning".
- mark_l_watson 8y agoI totally agree. I also have to use Python for machine learning, which has been my primary work activity for years, but Python does not click with me. I have started using MyPy at home but we don’t use it at work. I am so much happier writing code in Common Lisp, Scheme, Haskell, and Ruby.
- otikik 8y agoI instinctively distrust every project that self-describes as "elegant". Doubly so if it also says it is "simple". You docs should show it, not tell it.
- arcosdev 8y agoI get the nausea shivers when something is described as "curated"
- Insanity 8y agoI really like the look of this. Most programming in my spare time is done within a more functional paradigm. But I can't help but wonder if companies ever go for these kind if extensions / frameworks / languages. I will play around with this one though!
- dang 8y agoDiscussed in 2016: https://news.ycombinator.com/item?id=11960692 https://news.ycombinator.com/item?id=11960692
- udfalkso 8y agoI'm seeing a lot of the things I like about Elixir here. Very slick. Are there performance implications?
- dnautics 8y agoAre there supervision trees, share-nothing concurrency, and out of band multithreaded garbage collection, and IO.inspect?
- eindiran 8y agoThere are two things you can do to ensure that the transpiled Python is as fast as possible. First, disable TCO (while leaving TRE enabled) with the `--no-tco` argument to the compiler. Second, make sure you set the target version of Python correctly. This will allow the compiler to make a number of optimizations that are specific to different Python versions. Overall, for the majority of use cases I have found Coconut code to be approximately as performant as Python code written by hand. The compiler speed can leave a little to be desired though.
- rthy 8y agoAlternatively, the toolz package ( https://toolz.readthedocs.io/en/latest/ https://toolz.readthedocs.io/en/latest/ ) is a nice way of getting some additional functional programming capabilities while using the standard CPython interpreter.
- foobar_ 8y agoHow is this implemented ?
- eindiran 8y agoThe code is available on Github: https://github.com/evhub/coconut https://github.com/evhub/coconut
- 40acres 8y agoI like the idea but am not fond of the execution, particularly around the aesthetics. One of the reasons I love Python is because of it's syntactic brevity and conciseness, once you get to intermediate python there are some weird forms of syntactic sugar (magic functions and unpacking for instance) but visually the language has less clutter than most popular languages. Coconut really just seems to remove that aspect.
- Walkman 8y agoCoconut, aka take a beautiful language and fuck it up with ugly syntax :D Well done!
- vram22 8y agoThere's also Hy: http://docs.hylang.org/en/stable/ http://docs.hylang.org/en/stable/ "hy - A dialect of Lisp that's embedded in Python"
- ivoras 8y agoHeh... "compiles to Python code" - so still no efficient multithreading :(
- darkpuma 8y ago"Pythonic functional" seems like a contradiction of terms, considering python's infamous hostility (e.g. http://fold.sigusr2.net/2010/03/guido-on-functional.html http://fold.sigusr2.net/2010/03/guido-on-functional.html) to functional programming seems like a key part of what it means to be pythonic. That said, it looks a lot more pleasant than Python. The addition of pattern matching alone makes this seem like a very worthwhile project, a big improvement on Python.
- sgillen 8y agoI’m not sure what you mean, what about python is notoriously unfunctional?
- darkpuma 8y agoIt'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 multi-line lambdas, and the justifications for why always boil down to it being "unpythonic". Furthermore over the years Guido has made it pretty clear that he doesn't particularly like or care about functional programming: https://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html https://python-history.blogspot.com/2009/04/origins-of-pytho...
- pseudalopex 8y agoRequiring complex functions to have names isn't hostile to functional programming.
- caymanjim 8y agoI wish people would stop using |> as an operator. It's exceedingly awkward to type, and I shouldn't have to rely on editor tricks to input code.
- jhuni 8y agoThe first question that comes to mind is, what is wrong with using the JVM or the CLR as a compilation target for such a language. The only thing that I can think of is that they want to exist on the Python platform, which makes some sense. Python is taking over the computing industry and programming education and it does have a lot of libraries. Perhaps Python should have a standard Python virtual machine, but that almost certainly is never going to happen, for multiple reasons. So here you are stuck with a transpiler. A transpiler whose purpose is to make a functional language compile to a language which was explicitly designed not to be functional at all.
- kumarvvr 8y ago> Perhaps Python should have a standard Python virtual machine, but that almost certainly is never going to happen, for multiple reasons Really really curious, what are those reasons?. Not being sarcastic, just want to know.
- zmmmmm 8y agoYes. It is a bit of a head scratcher why people would be going to enormous effort to transpile to an execution environment that is as gimped as Python's. I use Groovy pretty much as "python for the JVM", and a big reason for that is simply that Python's runtime limitations are unacceptable to me from a basic engineering standpoint (primarily, GIL). I guess Python's ecosystem is big enough now that the selling point of "you can use all your favorite libraries" is enough of a drawcard?
- technicalbard 8y agoI still don't understand why Common Lisp isn't more popular
- snthpy 8y ago> The difference between |> and |> is exactly analogous to the difference between f(args) and f(args). Should the order of the second list be reversed? i.e. |> corresponds to f(args) and |> corresponds to f(args)