question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

automatic folds from for loops

See original GitHub issue

a 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:open
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
johnynekcommented, Jan 15, 2020

yeah, that’s fine. translate to (noting rebinding lets is already allowed):

sum = 3
z = sum * 2
sum = xs.foldLeft(sum, \sum, x ->
  sum = sum + x
  sum)
fn(sum, z)

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?

0reactions
avibryantcommented, Jan 15, 2020

let me give a more concrete example. Is something like this legal?

sum = 3
z = sum*2
for x in xs:
  sum = sum + x
fn(sum, z)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found