Loops

Just functions again

Like conditionals (if, either, switch) looping constructs in Rye are also just functions. Rye has many of them, and you can create your own.

Simple loop

Sometimes you just need to loop N times. And there is a function for that. It accepts integer - a number of loops, and a block of code.

loop 3 { print "Hey!" }
; prints:
; Hey!
; Hey!
; Hey!

loop 2 { prns "Bye" }
; prints: Bye Bye

For function

Injected values - quick crash course

We are getting ahead of ourselves. We will learn about injected values, op-words and similar things later, but we need to touch them a little. We could skip them for the loop function above, but the next one (for) doesn’t make any sense without them.

123 ::a                   ; you already learned about left leaning set-word or a mod-word
                          ; it gets its value from the left
print a                   ; prints: 123

loop 3 { ::i , prns i }   ; loop function injects loop number into the 
                          ; code block and left set-word can pick it up
; prints: 1 2 3

	                      ; because loop is repeater i is assigned multiple
						  ; times, hence the need for a mod-word

with 100 { ::x , print x } ; with takes an argument and injects it. Now why
                          ; would you do that? There is more to this as
						  ; you will see later
; prints: 100

There are other functions that inject values into blocks, not just loop, with (and for). There are also other elements of the language that let you use that.

The use of left set-word above might seem a little odd, but you will see the benefits later.

BTW: the comma shown above is called an expression guard, and is optional.

Now a “for” loop

The for function takes a block of values and a block of code. It iterates through values passing each as injected value into the block as it evaluates the block.

names: { "Jim" "Jane" "Anne" }

for names { ::name , print "Hi " + name }
; prints:
; Hi Jim
; Hi Jane
; Ji Anne

; range takes two integers and creates a block of integers between them 

for range 1 5 { ::i , prns i }
; prints: 1 2 3 4 5