News for hackers. Stuff that matters.
Re:Coconut – Pythonic functional programming language by jpulec on Thursday June 23, 2016 @10:23AM (#11961057)
While 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.
Re:Coconut – Pythonic functional programming language by pjmlp on Thursday June 23, 2016 @10:38AM (#11961194)
Guido is against it, so I doubt it will ever happen.

https://news.ycombinator.com/item?id=9973301

Re:Coconut – Pythonic functional programming language by daveguy on Thursday June 23, 2016 @12:55PM (#11962485)
Good good. One more reason to pry 2.7 out of Guido's hands and into 2.8.
Re:Coconut – Pythonic functional programming language by dragonwriter on Sunday June 26, 2016 @02:59AM (#11979714)
Send to me something like Coconut which implements that in a way which accepts existing Python but adds cleaner functional syntax is one way of making the case for that in future Python.
Re:Coconut – Pythonic functional programming language by Lord_DeathMatch on Thursday June 23, 2016 @10:31AM (#11961123)
Specifically?
Re:Coconut – Pythonic functional programming language by saadq on Thursday June 23, 2016 @11:05AM (#11961489)
Not really a fan of the lambda syntax in Python. Comparison:

JavaScript:

  let arr = [1, 2, 3]
  let sumOfSquares = arr.map(n => n * n).reduce((a, b) => a + b) // 14
Python:

  arr = [1, 2, 3]
  sum_of_squares = reduce(lambda a, b: a + b, map(lambda n: n * n, arr)) # 14
Re:Coconut – Pythonic functional programming language by brianwawok on Thursday June 23, 2016 @11:22AM (#11961654)
Is there a more Pythonic way to do it? Lambdas are cool but usually not the first place you go in Python. I would think something like (my best guess, not a Python pro)...

    sum_of_squares = sum([x*x for x in arr])
Which I think is easier to read than either example post above.

Of course you will point out that this is less powerful than full map and reduce.. but meh... pros and cons to both styles

Re:Coconut – Pythonic functional programming language by FreeFull on Thursday June 23, 2016 @11:44AM (#11961876)
I would write it like this, to avoid constructing the immediate list:

    sum_of_squares = sum(x*x for x in arr)
This makes use of https://www.python.org/dev/peps/pep-0289/
Re:Coconut – Pythonic functional programming language by brianwawok on Thursday June 23, 2016 @12:08PM (#11962070)
Thanks for the link, this is good to know.
Re:Coconut – Pythonic functional programming language by numlocked on Thursday June 23, 2016 @04:06PM (#11963924)
Worth noting that map() can be parallelized whereas a list comprehension can't necessarily (since it is an explicit loop). The multiprocessing module allows trivial map parallelization, but can't work on list comprehensions.

It's more than just stylistic.

Re:Coconut – Pythonic functional programming language by brianwawok on Thursday June 23, 2016 @07:44PM (#11965145)
So I have coded everything from dumb web servers (tm), to high performance trading engines (tm). I have toyed with doing the list in parallel thing... and used it in a toy GUI tool or two I wrote... but never really found it that useful in the real world. If you actually want high performance, doing a parallel map is not going to be fast enough. If you are a dumb web server, it's a waste of overhead 99% of the time.

But hey, if you want to use map when you actually need to do a parallel map, cool. But seems very very uncommon. ~ 1 in 10,000 maps I write.

Re:Coconut – Pythonic functional programming language by riffraff on Thursday June 23, 2016 @05:14PM (#11964353)
I don't think this is the case, list comprehensions can be expressed as syntax sugar over list functions, it's how they work in Scala for example

http://docs.scala-lang.org/tutorials/FAQ/yield.html

Re:Coconut – Pythonic functional programming language by fovc on Thursday June 23, 2016 @09:11PM (#11965542)
map() can only be parallelized if the function has no side effects. If there are no side effects, list comprehensions can be parallelized just as well
Re:Coconut – Pythonic functional programming language by S4M on Thursday June 23, 2016 @12:00PM (#11962002)
That example works only because the function sum is already defined in Python. If you wanted to do something less common than summing up elements you would have to either use reduce or implement a for loop.
Re:Coconut – Pythonic functional programming language by Orangeair on Thursday June 23, 2016 @05:23PM (#11964410)
In Python 3, reduce was intentionally moved into the functools library because it was argued that its two biggest use cases by far were sum and product, which were both added as builtins. In my experience, this has very much been the case. Reduce is still there if you need it, and isn't any more verbose. The only thing that is a little bit more gross about this example is the lambda syntax; I would argue that even that is a moot point, however, since Python supports first-class functions, so you can always just write your complicated reduce function out and then plug it in.
Re:Coconut – Pythonic functional programming language by zardeh on Thursday June 23, 2016 @01:13PM (#11962668)
True, but I've used python a lot, and I've used reduce maybe...twice? (well, twice that I can find on my github at least)
Re:Coconut – Pythonic functional programming language by S4M on Thursday June 23, 2016 @02:05PM (#11963115)
I just counted the number of reduce I used in my current python project (6k lines). reduce comes up 32 times. And by comparison, map is used 159 times and filter 125 times - for some reason I tend to use list comprehensions less than I should.
Re:Coconut – Pythonic functional programming language by brianwawok on Thursday June 23, 2016 @03:28PM (#11963721)
I also thing you use map not a ton if you get the list comprehension syntax down... it is a map with less cruft mostly...
Re:Coconut – Pythonic functional programming language by baq on Thursday June 23, 2016 @02:18PM (#11963198)
curious how often reduce is used with something else than operator.add?
Re:Coconut – Pythonic functional programming language by S4M on Thursday June 23, 2016 @02:32PM (#11963327)
One occurence was to calculate the GCD of a list of polynomials.

In fact I had "reduce" appearing in the names of some of my variables so I used it less than 32 times, about 20 times in that project.

Re:Coconut – Pythonic functional programming language by d0mine on Thursday June 23, 2016 @05:00PM (#11964272)
I see nothing particularly inspiring in the examples posted on http://stackoverflow.com/questions/15995/useful-code-which-u...

Could you show your reduce calls?

Re:Coconut – Pythonic functional programming language by S4M on Thursday June 23, 2016 @07:01PM (#11964930)
They are very similar to this one from your link: http://stackoverflow.com/questions/15995/useful-code-which-u...
Re:Coconut – Pythonic functional programming language by brianwawok on Thursday June 23, 2016 @12:06PM (#11962057)
Well, or write a static method somewhere that you call. Sum is used a lot, so handy it is written somewhere (vs having to do a lambda x + x thing.
Re:Coconut – Pythonic functional programming language by nawitus on Thursday June 23, 2016 @02:14PM (#11963175)
That seems like an argument against lambda functions in general - why use lambdas when you can define a static function for every case? Well, the answer in my opinion is because it makes code more readable if you can define a simple lambda function instead of having to name every single function in the code base.
Re:Coconut – Pythonic functional programming language by brianwawok on Thursday June 23, 2016 @03:25PM (#11963704)
Well if you are going to reuse the function, name it. If it is a 1 time thing, use a lambda.
Re:Coconut – Pythonic functional programming language by quietwhisper on Thursday June 23, 2016 @06:14PM (#11964695)
Sounds great in theory. Problem is if you need a lambda that isn't a single expression, you then have to name it. Welcome to the conversation.
Re:Coconut – Pythonic functional programming language by akavi on Thursday June 23, 2016 @11:56AM (#11961962)
What's the advantage of list comprehension over lambdas (assuming the lambda syntax is decently lightweight)?

I feel like I come down hard on the side of lambdas, but I've never really spent enough time in a language with list comprehension, so there's a good chance I'm missing something.

Re:Coconut – Pythonic functional programming language by quietwhisper on Thursday June 23, 2016 @06:17PM (#11964705)
how can you come down hard on the side of one when you've never experienced the other?

I'm from a non-list-comprehension background too, but recently started working a lot in a large python codebase, and have found the dict/list comprehensions to be beautiful. I'm a huge fan. It's a shame lambda syntax is not the best and it's generally crippled, but comprehensions are a great 80/20 compromise for handling most cases very cleanly.

Re:Coconut – Pythonic functional programming language by cwilkes on Thursday June 23, 2016 @12:30PM (#11962269)
I find it a lot easier to read, part of which is that I'm used to the Scala way of sequence dot map function. When I see the python one I can't remember if the function comes first or the array.
Re:Coconut – Pythonic functional programming language by warbiscuit on Thursday June 23, 2016 @04:38PM (#11964133)
I'm not positive, but I think it saves the need to create a new execution frame for each lambda call, since the whole loop executes in single frame used by the comprehension.

In theory I suppose the VM could have a map() implementation which opportunistically extracts the code from a lambda and inlines them when possible; but doubt CPython does that. OTOH, I'd be surprised if PyPy doesn't do something like that.

Re:Coconut – Pythonic functional programming language by fovc on Thursday June 23, 2016 @09:18PM (#11965566)
Since Python 3, both generators and lists create a new stack frame. [1] (2nd to last paragraph)

[1] http://python-history.blogspot.com/2010/06/from-list-compreh...

Re:Coconut – Pythonic functional programming language by warbiscuit on Friday June 24, 2016 @11:12AM (#11970366)
I'm not meaning when the comprehension is invoked, but during each iteration of the loop within the comprehension.

When doing something like `map(lambda x: 2+x, range(100))`, there will be 101 frames created: the outer frame, and 100 for each invocation of the lambda.

Whereas `[2+x for x in range(100)]` will only create 2: one for the outer frame, and one for the comprehension.

Re:Coconut – Pythonic functional programming language by brianwawok on Thursday June 23, 2016 @12:05PM (#11962043)
I think it's just less to type really, and it's considered the more standard way to do it.
Re:Coconut – Pythonic functional programming language by hcrisp on Thursday June 23, 2016 @12:48PM (#11962426)
For simple mathematical operations you can import them as functions:

    from operator import mul, add
    arr = [1, 2, 3]
    sum_of_squares = reduce(add, map(mul, arr, arr))
Re:Coconut – Pythonic functional programming language by flaie on Thursday June 23, 2016 @05:08PM (#11964322)
It's even more concise in Clojure:

    (defn sum-of-squares [a] (reduce + (map #(* % %) a)))
    (sum-of-squares [1, 2, 3]) ; => 14
Re:Coconut – Pythonic functional programming language by jpulec on Thursday June 23, 2016 @01:26PM (#11962792)
I think lambda syntax can be a bit cumbersome, but that aside what I really miss is a clean syntax for chaining functional operations. So often I find myself thinking about data in terms of 'pipelines'. i.e. in JS:

  _.chain(values)
   .map(() => {})
   .flatten()
   .compact()
   .uniq()
   .value()
vs Python where doing the same thing becomes either a nested mess of function calls or comprehensions or a for loop.
Re:Coconut – Pythonic functional programming language by SoftwareMaven on Thursday June 23, 2016 @01:40PM (#11962921)
But that's a function of API, not the language itself. Django (and most ORMs, I believe) support that kind of behavior:

    MyTable.objects.
        filter(some_row__gt=5).
        exlude(other_row='q').
        order_by('other_row')
The Python iterable APIs have decided to use nesting rather than chaining, but you can still have an underscore-like API: https://github.com/serkanyersen/underscore.py

The bigger problem remains: lambda functions are hideous in Python. map() will forever be ugly if you try to use it in the same way it is used in most functional languages.

Re:Coconut – Pythonic functional programming language by chris_7 on Thursday June 23, 2016 @02:05PM (#11963120)
This sort of API is hard to implement in Python though, because there's no formal notion of interfaces, so you cannot extend all iterables generically. So you need to use free functions (which don't read well when chained) or a wrapper object (ick).
Re:Coconut – Pythonic functional programming language by moosingin3space on Thursday June 23, 2016 @08:08PM (#11965247)
Elixir and F# have my favorite syntax for that:

    values |> map(&({})) |> flatten |> compact |> uniq
Although, the closure syntax is a little clunky before you get used to it.
Re:Coconut – Pythonic functional programming language by Bromskloss on Thursday June 23, 2016 @05:51PM (#11964558)
I've been thinking that it might be nice to use chaining (though I didn't know it had a name) in ordinary mathematical notation too, writing "x f g" instead of "g(f(x))".
Re:Coconut – Pythonic functional programming language by hcrisp on Thursday June 23, 2016 @02:32PM (#11963325)
You don't like using pytoolz? Pseudocode:

    result = pytoolz.pipe(values, map, flatten, compact, uniq, value)
    # or
    func = pytoolz.compose(value, uniq, compact, flatten, map)
    results = func(values)
Re:Coconut – Pythonic functional programming language by Bromskloss on Thursday June 23, 2016 @05:55PM (#11964577)
Looks interesting. How do you tell map what function to use?
Re:Coconut – Pythonic functional programming language by hcarvalhoalves on Thursday June 23, 2016 @11:06PM (#11966003)
You probably need `compose(foo, partial(map, fn), bar)`.
Re:Coconut – Pythonic functional programming language by hcrisp on Friday June 24, 2016 @12:31AM (#11966463)
Yeah, you do this or use curried version:

    pytoolz.curried.map(fn)
Re:Coconut – Pythonic functional programming language by hexane360 on Thursday June 23, 2016 @10:37AM (#11961183)
I think a big thing for many is the lambda: syntax, as well as the lack of full anonymous functions.
Re:Coconut – Pythonic functional programming language by masklinn on Thursday June 23, 2016 @11:06AM (#11961493)
The latter can't really happen given Python is a statements- and indentations-based language. You'd need some really weird meta-magical syntax which really isn't going to happen in Python. Although you can cheat by fucking around with decorators e.g.

    def postfix(fn, *args):
        return lambda arg: fn(arg, *args)

    @postfix(map, range(5))
    def result(i):
        return i * i

    print result
    # [0, 1, 4, 9, 16]
(`postfix` is necessary because `map` takes its argument positionally so it's not possible to pass in the sequence with functools.partial)
Re:Coconut – Pythonic functional programming language by dragonwriter on Thursday June 23, 2016 @11:24AM (#11961676)
> The latter can't really happen given Python is a statements- and indentations-based language.

Yeah, though I suppose you could hack around that and get nearly-full functionality in lambdas if you built a library that either wrapped non-expression statements in functions or provided equivalent functions. There are obviously some statements that there aren't good solutions for in that direction.

OTOH, using named functions is in many cases more readable -- in the context of what is otherwise a normal Python codebase -- than the kind of lambdas that you can't easily write in Python. But I like the Coconut approach of but providing a more concise syntax for the kind of lambdas Python already supports.

Re:Coconut – Pythonic functional programming language by NegativeLatency on Thursday June 23, 2016 @11:05AM (#11961481)
I agree that typing out the word lambda is annoying, but you can use them as fully anonymous functions.
Re:Coconut – Pythonic functional programming language by masklinn on Thursday June 23, 2016 @11:07AM (#11961502)
> 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.

Re:Coconut – Pythonic functional programming language by grondilu on Thursday June 23, 2016 @11:25AM (#11961693)
You 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

It does not look as bad as one might expect, though the nesting of parenthesis makes things messy.

Re:Coconut – Pythonic functional programming language by masklinn on Thursday June 23, 2016 @11:35AM (#11961796)
You can but you still can't get statements in there.
Re:Coconut – Pythonic functional programming language by ergl on Thursday June 23, 2016 @12:07PM (#11962063)
You can hack together multiple expressions chaining them with and http://sigusr2.net/one-line-echo-server-using-let-python.htm...
Re:Coconut – Pythonic functional programming language by zardeh on Thursday June 23, 2016 @01:21PM (#11962745)
That still doesn't get you statements.

You can't get context managers or exception handling (although you can raise exceptions) into lambdas, I've tried.

Re:Coconut – Pythonic functional programming language by masklinn on Thursday June 23, 2016 @01:35PM (#11962870)
Well you might be able to if you add a bunch of named function combinators wrapping these, but definitely not with only lambdas, unless you define your combinators using `ast`, which I think would let you define statements via expressions.
Re:Coconut – Pythonic functional programming language by AnkhMorporkian on Thursday June 23, 2016 @03:18PM (#11963663)
That's an awful lot of effort to go to to avoid naming a function.
Re:Coconut – Pythonic functional programming language by zardeh on Friday June 24, 2016 @12:58PM (#11971444)
Well sure, you could do something like

    def apply_ctx(ctx, func, *func_args, **func_kwargs):
        with ctx as __ctx:
            func(*func_args, **func_kwargs, ctx=__ctx)
but you can't do that itself as a lambda. And I consider modifying the ast cheating :P
Re:Coconut – Pythonic functional programming language by masklinn on Friday June 24, 2016 @03:11PM (#11972561)
> but you can't do that itself as a lambda. And I consider modifying the ast cheating :P

No disagreement, really depends whether you're a "rules" or "spirit" kind of person though.