12 ms·
Coconut – Pythonic functional programming language
- eddd 10y ago> There should be one-- and preferably only one --obvious way to do it. https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/
- baldfat 10y agoOver the years I have come to dislike that phrase more and more. I think it hindered Python from growing. Also Python 2 and Python 3 gives me at least two was to print :)
- ferrari8608 10y agoI've never understood why that phrase applies to Python when the language still has the map, filter, reduce, etc. functional functions. Generators and comprehensions completely removed the need for those, yet the functions remain even in Python 3. On the subject of printing, there are actual reasons for having this duplicate functionality. The main way to print is of course the print function in three, or print statement in two. The print function was lacking the ability to flush the buffer before that was introduced in three. You would have to call sys.stdout.flush(), whereas now the print function includes the boolean "flush" keyword argument. The second way to print is to call sys.stdout.write(), which does not automatically append a newline. That functionality was implemented in the print function with the "end" keyword argument. You can pass it an empty string in place of the newline. So for most use cases, print() is just fine. Sometimes you want finer grained control, and for that you would use the sys module.
- drostie 10y agoPython 3 at least changes the semantics of map and filter so that they are generators. Lazy sequences are the sort of thing that you don't care about until you need to process a ten-million-line file (or whatever) and suddenly find that your program is slowing down for pointless memory allocations up-front -- then they become unbelievably important.
- ferrari8608 10y agoYes, you are absolutely correct. In our production code, which is currently running 2.7, we make extensive use of the itertools versions of those functions (imap, ifilter, etc.) for this reason. The itertools functions behave exactly like the Python 3 builtins, as iterators. The memory footprint is minimal, and they are just overall faster.
- fao_ 10y ago> Generators and comprehensions completely removed the need for those, yet the functions remain even in Python 3. Hmm, I had never thought about it like that. Are there any articles, etc. you recommend about the differences or the advantages of one method over the other? It'd be nice to look into this more.
- nomel 10y agoI don't think you'll find one. Guido wanted to remove them [1], but people wanted them to stay for some reason. There is no difference between `map(func, values)` and `[func(x) for x in values]`, except for character count. [1] http://www.artima.com/weblogs/viewpost.jsp?thread=98196 http://www.artima.com/weblogs/viewpost.jsp?thread=98196
- chishaku 10y agoI've mentioned this before and I'm still curious to hear more. > Barry Warsaw, one of the core Python developers, once said that it frustrated him that "The Zen of Python" (PEP 20) is used as a style guide for Python code, since it was originally written as a poem about Python's internal design. [0] I did a quick search for more background but didn't find anything. [0]: https://github.com/amontalenti/elements-of-python-style#a-little-zen-for-your-code-style https://github.com/amontalenti/elements-of-python-style#a-li...
- skykooler 10y agoIs there a Pythonic declarative programming language?
- jasonm23 10y agoName issues. E.g. Google: Coconut-Lang cookbook ...
- miguelrochefort 10y agoWhat's "pythonic" exactly?
- baldfat 10y agoI googled it for you: http://stackoverflow.com/questions/25011078/what-does-pythonic-mean http://stackoverflow.com/questions/25011078/what-does-python...
- miguelrochefort 10y agoDoesn't really help in this context. What's the difference between "pythonic" and "idiomatic"? Does Coconut being pythonic mean it's written in idiomatic Python (then why is it a different language?) or does it mean it's written in idiomatic Coconut? I don't think they thought this through.
- drostie 10y agoThey just mean "this language looks in general like Python." You're overthinking it. It's like saying that JavaScript looks C-ish or Java-ish. It definitely doesn't look exactly the same; if you see e.g. var result = [].slice.apply(vals, 0); then you can be highly certain that that's JS and not Java or C. But the idea of having blocks of code enclosed in curly braces, which are formed out of statements usually delimited by semicolons (except for special forms like if-statements and for-loops which don't need to be followed with a terminal semicolon), etc. is very much a C-style thing.
- Niksko 10y agoPythonic is just short for idiomatic Python. It's a different language presumably because it introduces some new language constructs that are not compatible with Python. Perhaps they're saying it's 'Pythonic' because of some loosely defined notion that the new constructs feel and behave in the same way that Python does, but that's just speculation.
- quietwhisper 10y agoPython has a very strong set of conventions, beliefs about what 'good python' looks like/what is in the spirit of the language. It's a common term in the python community.
- rcarmo 10y agoWell, so far nobody's complained about Python 3 support yet :) Seriously now, this looks pretty neat, and even includes Jupyter support. I suspect it manipulates the AST like Hy, and I can't see any reason why it shouldn't work on Pypy.
- keenerd 10y agoIt works with python3 just fine. The 'coconut-lang' and 'coconut-lang-git' packages on the AUR both use py3.
- jpulec 10y agoWhile I resonate with the sentiment, I just wish Python would add better syntax for functional programming. Having written a lot of JavaScript lately, I wish Python's built-in functional tools supported something cleaner, kinda like Underscore/Lodash.
- Lord_DeathMatch 10y agoSpecifically?
- hexane360 10y agoI think a big thing for many is the lambda: syntax, as well as the lack of full anonymous functions.
- NegativeLatency 10y agoI agree that typing out the word lambda is annoying, but you can use them as fully anonymous functions.
- masklinn 10y ago> you can use them as fully anonymous functions. A lambda can only contain a single expression, by "full anonymous function" I'm guessing hexane360 means multiple statements. You can't put a for loop or a context manager in a lambda for instance.
- grondilu 10y agoYou can nest lambdas to get the equivalent of several expressions. I once wrote a Runge-Kutta example on Rosetta Code showing this: http://rosettacode.org/wiki/Runge-Kutta_method#using_lambda http://rosettacode.org/wiki/Runge-Kutta_method#using_lambda It does not look as bad as one might expect, though the nesting of parenthesis makes things messy.
- aktiur 10y agoYour function in the tail recursion optimization exemple is actually not tail recursive. Factorial needs an accumulator parameter to get that tail call.
- agumonkey 10y agoMaybe the decorator rewrites the AST. I've seen this before.
- santialbo 10y agoIt does, when the function is actually tco optimizable. The example is not. On the docs however it's correct http://coconut.readthedocs.io/en/master/DOCS.html#recursive http://coconut.readthedocs.io/en/master/DOCS.html#recursive
- coconutlang 10y agoGood catch! I'll fix it right away.
- codezero 10y agoHow does this compare with Hy?
- akkartik 10y agoWhat are all the $ for? They seem ugly..
- doomrobo 10y agoLooks like the $ is necessary for partial function application
- dkersten 10y agoQuoting from the tutorial: Second, the partial application. Think of partial application as lazy function calling, and $ as the lazy-ify operator, where lazy just means “don’t evaluate this until you need to”. In Coconut, if a function call is prefixed by a $, like in this example, instead of actually performing the function call, a new function is returned with the given arguments already provided to it, so that when it is then called, it will be called with both the partially-applied arguments and the new arguments, in that order. In this case, reduce$((*)) is equivalent to (*args, **kwargs) -> reduce((*), *args, **kwargs). https://coconut.readthedocs.io/en/master/HELP.html https://coconut.readthedocs.io/en/master/HELP.html (about a third of the way down, just search for "$" and look near the first occurrences) I agree though, it makes the code look very... noisy.
- guitarbill 10y agoI'm all for syntactic sugar, but my experience with teaching programming is that Python is easy to pick up because of the lack of weird operators. Decorators are an obvious example where many struggle; and this looks like another. It will take someone cleverer than me to come up with an alternative solutions; I suspect they chose "$" because it isn't an operator in Python and you have to do a lot of partial application, so the logic was probably "short is better". But having something more intuitive would be great (more pythonic?).
- dkersten 10y agoPython has lots of weird operators. List/dict comprehension, list slicing, * and in arguments. Comma for tuples (which make sense to me, but add a lot of magic, especially since, in my experience, beginners often assume that the parentheses are what make tuples). There's many more examples, but those are the ones that immediately spring to mind. Reading actual production Python code is not any easier than many other languages and certainly far from "executable pseudocode"
- doomrobo 10y agoThe example algebraic data type isn't an algebraic data type at all. It's a single type with a single type constructor. How is this different from a regular class?
- andolanra 10y agoIt looks like data declarations get translated to immutable subclasses of collections.namedtuple in such a way that you can match on the resulting values. So it's really less like an algebraic data type and more like one of Scala's case classes.
- abecedarius 10y agoWhen I used namedtuple that way it tended to blow up on me, because namedtuples implement 'dunder' methods to act as sequences, which is not what I expect for some random algebraic datatype. This makes bugs manifest as very head-scratching behavior. Nowadays I use my own struct type builder with only the equivalent of 'derives Show' and such.
- willtim 10y agoThis is an impressive effort, but one major problem is that the Python runtime lacks efficient support for tail calls. I see that there is an annotation for self-recursion, but this is only a special case. Scala has a similar problem with the JVM. Still, it does permit a nice functional syntax above perhaps lower-level imperative code.
- noobermin 10y agoThat is a problem but it's not like Python is "efficient" or performant in other areas that don't call an underlying C library.
- grondilu 10y agoHave you ever looked at Perl 6, by any chance? We have most of the features you list, except for instance Tail Call Optimization, but we'd love to have someone implementing it!
- wodenokoto 10y agoI think the purpose of coconut is not to give the world new functional programming features, but give you "real" functional programming with access to pythons vast library.
- hcarvalhoalves 10y agoThis is cool, but you can go pretty far w/ FP idioms in Python without requiring a DSL/transpiler: - Toolz (http://toolz.readthedocs.io/en/latest/ http://toolz.readthedocs.io/en/latest/) provides the same primitives from Clojure (including partial functions, compose, juxt, etc) - Pysistence (https://pypi.python.org/pypi/pysistence/ https://pypi.python.org/pypi/pysistence/) provides immutable data structs - For parallel map you can use https://pypi.python.org/pypi/python-pmap/1.0.2 https://pypi.python.org/pypi/python-pmap/1.0.2 - TCO is achievable w/ a decorator regardless of AST manipulation (like Clojure's `loop/recur` construct) The only thing left is pattern matching and more powerful destructuring (Python already has limited support for it), I guess.
- _navaneethan 10y agoPattern matching Is it mimics the Erlang behaviour?
- spraak 10y agoReminds me a lot of Elixir actually. I'd really prefer a purely functional language with LISP syntax. Does anyone know of any? I find it easier to understand than Haskell..
- benley 10y agoIs Clojure close enough? Or perhaps LFE (Lisp-flavored Erlang)?
- fiddlerwoaroof 10y agoThere's also Shen: http://shenlanguage.org/ http://shenlanguage.org/
- fiatjaf 10y agoNo, you can't, because lambdas.
- Lambdanaut 10y ago
- nailer 10y ago> Supports pipeline-style programming > > "hello, world!" |> print Please follow the Elixir convention and call this by its proper name: illuminati pyramid operator.
- arnarbi 10y agoI'd suggest fixing the front page, which claims ADT support which seems untrue, and the tail recursive example isn't tail recursive at all. Errors like that make this too easy to dismiss.
- mrcactu5 10y agoIs coconut strongly typed? Is it closer to Haskell or to Lisp / ML?
- bjz_ 10y agoYou probably mean statically typed.
- coconutlang 10y agoCoconut is a superset of Python, and Python is dynamically typed, so Coconut also has to be.
- fuzzythinker 10y agoInteresting. Looks more polished than mochi[1] and hask[2]. [1] https://github.com/i2y/mochi https://github.com/i2y/mochi [2] https://github.com/billpmurphy/hask https://github.com/billpmurphy/hask
- spriggan3 10y agoReminds me Ocaml, a nice language that deserves more exposure on HN.
- infodroid 10y ago> ...joining the over 30,000 people already using Coconut... This triggered my BS detector. It turns out 30,000 is the sum of all downloads on PyPI since the very first version was released. But there are only 534 downloads on average for each release since version 0.2.2, excluding the blip for 0.3.2. So there is no way there are on the order of 30k users when downloads are roughly 1.8% of that for each release. This matters because exaggerated claims reduce trustworthiness in a project and are off-putting to potential users.
- Bromskloss 10y agoSince they just before that wrote that all valid Python is valid Coconut, I thought that it would be a joke about every Python user being a Coconut user.
- infodroid 10y agoAre you saying there are only around 30k Python users? :)
- Bromskloss 10y agoNah, when I got to the number, I realised that it wasn't going to be a joke like I thought.
- spraak 10y agoThat's actually how I read it until I saw the comment a above and reread it
- Bromskloss 10y agoSince they just before that wrote that all valid Python is valid Coconut, I thought that it would be a joke about every Python user being a Coconut user.
- 88e282102ae2e5b 10y agoDid they find a way to remove bots from the download statistics? My very obscure image library had over 2 million downloads despite an estimated 10 to 20 users, which would make 30,000 effectively equal zero.
- wentoodeep 10y agoRise of the LISP.
- a3n 10y ago$ coconut Coconut Interpreter: (type "exit()" or press Ctrl-D to end) >>> match [head] + tail in [0, 1, 2, 3]: print(head, tail) CoconutParseError: parsing failed (line 2) print(head, tail)