3 ms·
And lambda still have 1 line ? My style of programming is mostly using callback with anonymous function. This restriction prevents me to using Python in seriou
by revskill 5y ago
And 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.
- quietbritishjim 5y agoWhat is the problem using def for your multiline closures?
- valenterry 5y agoIt's boilerplate and makes you have to jump around in the code to follow what's happening. If a function is used 2 times, 3 times, ... sure, go for it. If it's just a one-time use, please don't make a separate function.
- quietbritishjim 5y agoIt doesn't have to be a separate function, it can be nested (in fact it has to be if it's a closure). That's no different from what you would have got if you were allowed multiline lambdas in the first place. Or did you just mean it has to be one line earlier than the line of code that uses it? That doesn't seem like a big imposition to me. def precise_multiples_first(max_val, divisor): print(f"Using {max_val=}") def sort_key(val): if val % divisor == 0: return 0, val else: return 1, val return sorted(range(max_val), key=sort_key) vs def precise_multiples_first(range_max, divisor): print(f"Using {max_val=}") return sorted(range(max_val), key=lambda val: ( if val % divisor == 0: return 0, val else: return 1, val )) (Yes I know I could've squeezed that function onto one line e.g. using ternary operator, but it was just a quick example.)
- valenterry 5y agoThat only works if you are not already within a lambda expression - which happens quite often if you embrace a FP style