9 ms·
Coconut: Simple, elegant, Pythonic functional programming
- deleted 5y ago[deleted]
- de_keyboard 5y agoNice idea! An emerging alternative to this is Python support in Fable. https://github.com/fable-compiler/Fable.Python https://github.com/fable-compiler/Fable.Python
- pbronez 5y ago> Fable is a compiler that translates F# source files to JavaScript and Python. interesting. So if you want to program python in a functional style, you can use Coconut to add functional features to python, or jump all the way to F# and just compile to python. Interesting to see two projects wind up in a similar place (compiling functional code to python) after starting in very different communities.
- bayesian_horse 5y agoThe major difference could be that type checking in coconut is optional. Not sure what that means for productivity and type safety.
- de_keyboard 5y agoIn F# you can type everything with `obj` and dynamic casting all over - not recommended but strictly speaking typing is optional there too.
- revskill 5y agoAnd lambda still have 1 line ? My style of programming is mostly using callback with anonymous function. This restriction prevents me to using Python in serious projects.
- scoopertrooper 5y agoDon't let your dreams be dreams. https://coconut.readthedocs.io/en/master/DOCS.html#statement-lambdas https://coconut.readthedocs.io/en/master/DOCS.html#statement...
- revskill 5y agoThis is still 1 line, just separated by semicolon ?
- pansa2 5y agoAnd it only supports assignment statements and keyword statements. What’s a “keyword statement”?
- scoopertrooper 5y agoYou want lines, so that you may have multiple statements in your lambda. They've given you multiple statements, but using the semicolon instead. I appreciate that won't work well for all use cases, but it'd eliminate a lot of the pain with the current lambda implementation. For whatever reason, the Python community declared multiline lambdas as non-pythonic and this project aims to remain pythonic, so they went with semicolons instead. I'm not a Python developer myself, but if that was the only thing holding you back, then perhaps that reason is now slightly reduced?
- macintux 5y agoIt does seem that if a function can’t fit with this new expanded capability, it probably is complex enough to warrant a name. Assuming that Python’s support for nested functions carries over to Coconut, it seems like a reasonably-complete feature set.
- xaedes 5y agoIf you really need more than one line this will probably work: L |> map$(def (x) -> ( y = 1/x; y*(1 - y) )) You can also make multiline python lambdas by using brackets. That is if you can fit your code into an expression.
- pelagicAustral 5y agoI always feel like functional programming languages start with some math major looking at traditional OOP code and thinking "ITS TOO READABLE!!"
- jimsimmons 5y agoI'm confused when people call it simple or elegant. I wonder if the same crowd would use similar words to describe regex.
- gliptic 5y agoCompared to writing the parsing in procedural code? Yes. e.g.: a(b|c)d vs something like: return str.StartsWith("a") && str.EndsWith("d") && str.Length == 3 && (str[1] == "b" || str[1] == "c"); Why wouldn't you consider regular expressions simple and elegant for the problem they are solving?
- parhamn 5y agoMaybe they mean syntax? I guess you could compare it to say syntax like "char 'a' + either char 'b' or char 'c' + char 'd'". I like regex as is but that would def make it more approachable for the folks I know who don't.
- jimsimmons 5y agoThat's a simple example. Things quickly get out of hand once you have a more complex problem like recognising a valid URL [1]: `r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s` Regex has a reputation for being "write once read never". It's primarily because it doesn't have any structure or "modularity". Functional programs also tend to pack many variables or operations in the same line without delimitation. For example Haskell's argument syntax. [1] https://www.geeksforgeeks.org/python-check-url-string/ https://www.geeksforgeeks.org/python-check-url-string/
- SOLAR_FIELDS 5y ago
- justusw 5y agoNice project. It reminds me of dogelang (dg) for Python: https://pyos.github.io/dg/ https://pyos.github.io/dg/
- pbronez 5y agolol hilarious landing page.
- jedimastert 5y agoHomepage feedback: It would be great if there was a demonstrative code-snippet that pre-populated that editor at the top of the page, even if it's just one of the snippets further down.
- pbronez 5y ago+1 Also, the editor should include an auto-print option that's on by default. The first snippets I tried (copying up from below) didn't include a print statement, and thus didn't show any output when run.
- gampleman 5y agoMy boss pointed out and I concur, this looks a lot like the the Coffeescript of Python. That might be a good thing or a bad thing, depending on where you stand.
- bayesian_horse 5y agoSimilar principle. There is also Hylang and Fable (F#). Python is a saner foundation to start from, though.
- gilch 5y agoAnd Hissp.
- asplake 5y agoNice! I was thinking I’d get round to trying some syntax-related experiments one day. Would this be an easier platform than (say) cpython for stuff like that?
- gfunk911 5y agoIs the name Coconut an intentional homage to CoffeeScript?
- pbronez 5y agoNope, Monty Python reference https://coconut.readthedocs.io/en/master/FAQ.html#id18 https://coconut.readthedocs.io/en/master/FAQ.html#id18
- bayesian_horse 5y agoLooks neat. I will check it out more deeply. In other news, there is now a port of Fable that compiles F# to Python.
- dunefox 5y agoThat sounds interesting. So I can program in F# and run it in Python? What are the limitations? Also, is the resulting code readable?
- dharmaturtle 5y ago> So I can program in F# and run it in Python? Yes. > What are the limitations? It's in alpha. > Also, is the resulting code readable? I think so, at least in this screenshot: https://user-images.githubusercontent.com/849479/104726627-38ee7680-5734-11eb-848e-4fbda163f633.png https://user-images.githubusercontent.com/849479/104726627-3... from this PR https://github.com/fable-compiler/Fable/issues/2339 https://github.com/fable-compiler/Fable/issues/2339 Its author gives a talk about it here, though you may need to make a (free) account: https://skillsmatter.com/skillscasts/17237-fable-python-f-sharp-python https://skillsmatter.com/skillscasts/17237-fable-python-f-sh... He elaborates on the above screenshot and shows F# and Python running on the same Jupyter kernel.
- bayesian_horse 5y agoI think one of the limitations is that the code is run in the Python VM, so that means you don't get the usual F# performance. That may matter for some stuff like recursions or numerical code, or for performance-sensitive things in general.
- lloydatkinson 5y agoPython is the same language that promotes throwing exceptions for parsing a number from a string instead of a simple bool. Not exactly sure Python is the best language as a basis for FP, when Python itself so openly disregards such cleaner approaches.
- wlpu 5y agoPersonally I'd rather the exception than a "simple" bool, which would make for noisy code and less simplicity. In reality and exception is just less bad, better than both would be a result type
- Jeff_Brown 5y agoYes, if you have the luxury of starting anywhere. As someone who has to work in a Python-ish language for the sake of hiring and backward compatibility, I love Coconut.
- macintux 5y agoErlang is a successful FP language that throws exceptions for parsing a number from a string. Admittedly Erlang also is built around the idea of exceptions everywhere.
- bgorman 5y agoI have wondered how something like ReasonML but with Python-like syntax instead of JavaScript-like syntax would look like, so this is pretty interesting. While “any Python is valid coconut” seems appealing, in practice I don’t think this is the correct approach, since in my experience OO code is not really a good fit for the FP mental model of reasoning, and I would be happier with most of the OO features turned off.
- dang 5y agoPast related threads: Coconut: Simple, elegant, Pythonic functional programming - https://news.ycombinator.com/item?id=23759721 https://news.ycombinator.com/item?id=23759721 - July 2020 (63 comments) Coconut Programming Language - https://news.ycombinator.com/item?id=23663010 https://news.ycombinator.com/item?id=23663010 - June 2020 (1 comment) Coconut Programming Language - https://news.ycombinator.com/item?id=23339351 https://news.ycombinator.com/item?id=23339351 - May 2020 (2 comments) Coconut: Pythonic functional programming - https://news.ycombinator.com/item?id=18815125 https://news.ycombinator.com/item?id=18815125 - Jan 2019 (169 comments) Coconut – Pythonic functional programming language - https://news.ycombinator.com/item?id=11960692 https://news.ycombinator.com/item?id=11960692 - June 2016 (130 comments) Show HN: Coconut – Simple, elegant, Pythonic functional programming - https://news.ycombinator.com/item?id=11936038 https://news.ycombinator.com/item?id=11936038 - June 2016 (2 comments)