News for hackers. Stuff that matters.
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.