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.

Add control flow operators for iterated functions

See original GitHub issue

Add the following control flow operators for iterated functions:

# iterate : ∀a. ℕ → (a → a) → (a → a)
def iterate(n, f, x):
    for _ in range(n):
        x = f(x)
    return x

# orbit : ∀a. ℕ → (a → a) → (a → [a])
def orbit(n, f, x):
    xs = [x]
    for _ in range(n):
        x = f(x)
        xs.append(x)
    return xs

iterate and fori_loop are mutually definable, but the former has a simpler signature and semantics that is more common in my experience, and is arguably more “basic” or “fundamental”.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Mar 21, 2022

Sure, but adding new APIs does not come without maintenance costs. It’s true that while_loop and fori_loop can be implemented in terms of scan, but their implementations are far more involved than a single line.

As this is the first time I’m aware of receiving such a request, I would lean toward not including them in the package API, but I’m happy to change my mind if there are compelling reasons to do so.

0reactions
jakevdpcommented, Nov 11, 2022

I think that could be an improvement – I’d want to hear opinions from other folks on the team

Read more comments on GitHub >

github_iconTop Results From Across the Web

4. More Control Flow Tools — Python 3.11.1 documentation
It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn't really make...
Read more >
Control flow in BQN
However, there are many ways to write control flow, including simple operators and a mix of operators and more control-structure-like code.
Read more >
Learn Clojure - Flow Control
Flow control operators are composable, so we can use them anywhere. This leads to less duplicate code, as well as fewer intermediate variables....
Read more >
5 Control flow | Advanced R - Hadley Wickham
There are two primary tools of control flow: choices and loops. Choices, like if statements and switch() calls, allow you to run different...
Read more >
23 Control flow statements - Exploring JS
Succeed: While looping, we find a result . Then we use break plus label (line A) to skip the code that handles failure....
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