automatic folds from for loops
See original GitHub issuea common idiom in python is something like:
x = 0
for y in z:
x += y
return x
This does not look so functional, but it could be. We could convert for comprehensions like this into folds. We could do this by considering x += y
to be rebinding a new x to x + y
and we could look at all such rebound variables in the loop. Then we could convert it to something like:
z.foldLeft(0, \y, x -> x + y)
This is not so dissimilar to having the do
notation in haskell which allows you to write imperative looking code that still returns a value.
Similarly, we could do the same translation for map-like for comprehensions:
[f(x) for x in y]
to y.map(f)
There may be some subtlety with making these fully composable which we would want to support.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
FunFolDes - RosettaCommons
The FoldFromLoops (FFL) protocol is a variant of the grafting protocol. ... is capable to generate fragments on the fly to guide the...
Read more >Matlab unfolds folded code when I leave an open for/if loop ...
I have tried searching for some setting that governs this behavior, but have not found anything, and don't know what this sort of...
Read more >Folding, accumulate, for loop - c++ - Stack Overflow
We have to perform a left fold (std accumulate with C++20 improvements (move semantics)) with the following lambda. auto gatherChunks ...
Read more >Replace Loops with Folds - James Earl Douglas
Motivation. Loops generally coincide with mutable state, which is not referentially transparent. Folds not only eliminate mutation, but they're more concise.
Read more >Folding repetition into a loop - TestArchitect Docs
How to use loop control actions to handle iterative processes. ... Note that an until action is automatically inserted several lines below the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
yeah, that’s fine. translate to (noting rebinding lets is already allowed):
so, you just build a tuple of the values bound in the for loop, then put those bindings into a tuple and return it a fold. Isn’t that what you intend?
let me give a more concrete example. Is something like this legal?