Timeline for What is the 'pythonic' equivalent to the 'fold' function from functional programming?
Current License: CC BY-SA 3.0
19 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| S Jul 7, 2024 at 7:26 | vote | accept | mistertim | ||
| Dec 14, 2023 at 19:57 | comment | added | John Haberstroh | @dividebyzero python doesn't distinguish or have this constraint (fortunately? unfortunately?) | |
| Apr 28, 2019 at 13:00 | answer | added | Xavier Guihot | timeline score: 104 | |
| Apr 4, 2019 at 16:35 | answer | added | rq_ | timeline score: 2 | |
| Apr 27, 2017 at 15:20 | answer | added | Kyr | timeline score: 17 | |
| Apr 16, 2017 at 14:55 | answer | added | Mehdi Nellen | timeline score: 9 | |
| Jan 27, 2017 at 17:13 | comment | added | dividebyzero | It should be noted foldLeft is generally something different from reduce... You can only reduce on monoids. foldLeft is a much more general thing. | |
| Aug 19, 2015 at 17:36 | answer | added | clay | timeline score: 28 | |
| May 21, 2013 at 9:48 | comment | added | lvc |
Although the case of doing it with lists is a good demonstration of things to watch for with this technique - + on lists is a linear time operation in both time and memory, making the whole call quadratic. Using list(itertools.chain.from_iterable([a], [b,c,d],[e,f],[]]) is linear overall - and if you only need to iterate over it once, you can drop the call to list to make it constant in terms of memory.
|
|
| May 21, 2013 at 9:39 | answer | added | Frédéric | timeline score: 8 | |
| Apr 28, 2012 at 19:36 | vote | accept | mistertim | ||
| S Jul 7, 2024 at 7:26 | |||||
| Apr 28, 2012 at 19:34 | comment | added | Joel Cornett |
@mistertim: sum() actually provides limited functionality with this. sum([[a], [b, c, d], [e, f]], []) returns [a, b, c, d, e, f] for example.
|
|
| Apr 28, 2012 at 18:45 | answer | added | JBernardo | timeline score: 1 | |
| Apr 28, 2012 at 18:44 | history | edited | jamylak | CC BY-SA 3.0 |
added 6 characters in body
|
| Apr 28, 2012 at 18:43 | comment | added | mistertim | Hey JBernardo - Summing over a list of numbers was meant as a rather degenerate example, I'm more interested in the general idea of accumulating the elements of a list using some binary operation, and a starting value, not summing integers specifically. | |
| Apr 28, 2012 at 18:35 | answer | added | Fred Foo | timeline score: 166 | |
| Apr 28, 2012 at 18:33 | comment | added | jamylak |
not sure if this is a good example for your question. It can easily be achieved with sum, you may want to provide some different types of examples.
|
|
| Apr 28, 2012 at 18:32 | comment | added | JBernardo |
sum isn't good enough?
|
|
| Apr 28, 2012 at 18:30 | history | asked | mistertim | CC BY-SA 3.0 |