Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

are there any loop constructs? for example: while or do. #6

Open
haifenghuang opened this issue Oct 13, 2021 · 2 comments
Open

are there any loop constructs? for example: while or do. #6

haifenghuang opened this issue Oct 13, 2021 · 2 comments

Comments

@haifenghuang
Copy link

I have not see any loop constructs in the OK? language, how do I do loop logic in OK? language?

@ChayimFriedman2
Copy link

ChayimFriedman2 commented Oct 25, 2021

There is map(), of course, and you can build a sequential one, if you want, by using recursion:

let while = fn(cond, body) {
  if (!cond()) { return NO! }
  body()
  while(cond, body)
}

let i = 1
while(fn() { 10 >= i }, fn() {
  puts("This will be printed ten times")
  i = i + 1
})

Of course, this can blow up the stack, and also will be pretty slow, however I managed this way to run the following code:

timeseq(100000, fn() {
  mapseq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], fn(item) { item * item })
})

In "just" two and a half minutes, where timeseq() is based on whileseq() which is the above, and mapseq() runs forseq() which is again based on whileseq(). So... Not so bad. Well, terrible, but still...

@jandrese
Copy link

jandrese commented Sep 9, 2022

So what if you need to iterate over a list with side effects? What if you want to sort the list? What if there is an error halfway through and you need to abort cleanly, like a device went offline and now you need to stop processing and not pound the poor thing with a million further requests? map() is a nice tool, but it has a lot of limitations in the real world. Generally it has to be used judiciously to avoid runaway code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants