CrystalLang-Iterators

GeekNationGeekNation
4 min read

The word iterate means doing one thing multiple times and that is what iterators do. Sometimes iterators are termed as the custom loops.

“Iterators” is the object-oriented concept in crystal. In more simple words, iterators are the methods which are supported by collections(Arrays, Hashes etc.). Collections are the objects which store a group of data members. crystal iterators return all the elements of a collection one after another. crystal iterators are “chainable” i.e adding functionality on top of each other. There are many iterators in crystal as follows: Each Iterator: This iterator returns all the elements of an array or a hash. Each iterator returns each value one by one.

collection.each do |variable_name|
   # code to be iterate
end
# crystal program to illustrate each iterator

#!/usr/bin/crystal 

# using each iterator
# here collection is range
# variable name is i
(0..9).each do |i|

    # statement to be executed
    puts i

end

a = ['G', 'e', 'e', 'k', 's']

puts "\n"

# using each iterator
# here collection is an array
a.each do|arr|

    # statement to be executed
    puts arr

end

Output:

0 1 2 3 4 5 6 7 8 9

G e e k s

Times Iterator: In this iterator, a loop is implanted with the specific number of time. The loop is initially started from zero and runs until the one less than the specified number. This can be used with no iteration variable.. We can add an iteration variable by using the vertical bars around the identifier. Syntax:

t.times do |variable_name|

# code to be execute

end

Here t is the specified number which is used to define the number of iteration.

# crystal program to illustrate time iterator

#!/usr/bin/crystal

# using times iterator by providing 
# 7 as the iterate value
7.times do |i|
    puts i
end

Output:

0 1 2 3 4 5 6

Upto Iterator: This iterator follows top to bottom approach. It includes both the top and bottom variable in the iteration. Syntax:

top.upto(bottom) do |variable_name|

# code to execute

end

Here iteration starts from top and ends on bottom. The important point to remember that the value of bottom variable is always greater than the top variable and if it is not, then it will return nothing.

    # crystal program to illustrate the upto iterator

#!/usr/bin/crystal

# using upto iterator
# here top value is 4
# bottom value is 7
4.upto(7) do |n| 
puts n 
end

# here top > bottom
# so no output
7.upto(4) do |n| 
puts n 
end

Output:

4 5 6 7

Downto Iterator: This iterator follows bottom to top approach. It includes both the top and bottom variable in the iteration. Syntax:

top.downto(bottom) do |variable_name|

# code to execute

end

Here iteration starts from bottom and ends on top. The important point to remember that the value of bottom variable is always smaller than the top variable and if it is not, then it will return nothing.

    # crystal program to illustrate the downto iterator

#!/usr/bin/crystal

# using downto iterator
# here top value is 7
# bottom value is 4
7.downto(4) do |n| 
puts n 
end

# here top < bottom
# so no output
4.downto(7) do |n| 
puts n 
end

Output:

7 6 5 4

Step Iterator: crystal step iterator is used to iterate where the user has to skip a specified range.

Syntax:

Collection.step(rng) do |variable_name|

#code to be executed

end

Here rng is the range which will be skipped throughout the iterate operation.

`

    #crystal program to illustrate step iterator

#!/usr/bin/crystal

#using step iterator
#skipping value is 10
#(0..60 ) is the range
(0..60).step(10) do|i|
    puts i
end

Output:

0 10 20 30 40 50 60

Each_line Iterator: crystal each_line iterator is used to iterate over a new line in the string.

Syntax:

   string.each_line do |variable_name|

#code to be executed

end
#crystal program to illustrate Each_line iterator

#!/usr/bin/crystal

#using each_line iterator
"Welcome\nto\nGeeksForGeeks\nPortal".each_line do|i|
puts i
end

Output:

Welcome to GeeksForGeeks Portal

source:- geeksforgeeks.org

all code is tested on : https://play.crystal-lang.org/

0
Subscribe to my newsletter

Read articles from GeekNation directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

GeekNation
GeekNation