Add control flow operators for iterated functions
See original GitHub issueAdd 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:
- Created 2 years ago
- Comments:15 (8 by maintainers)
Top 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 >
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 Free
Top 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
Sure, but adding new APIs does not come without maintenance costs. It’s true that
while_loop
andfori_loop
can be implemented in terms ofscan
, 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.
I think that could be an improvement – I’d want to hear opinions from other folks on the team