are there any loop constructs? for example: while or do.
I have not see any loop constructs in the OK? language, how do I do loop logic in OK? language?
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...
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.