Skip to content
Related Articles

Related Articles

Improve Article

Accessing each element of a collection in Julia – foreach() Method

  • Last Updated : 26 Mar, 2020

The foreach() is an inbuilt function in julia which is used to call function f on each element of the specified iterable c.

Syntax: foreach(f, c…)

Parameters:

  • f: Specified set of instructions.
  • c: Specified iterable.

Returns: It returns the result of the operation of function f on each element of the specified iterable c.

Example 1:






# Julia program to illustrate 
# the use of foreach() method
  
# Getting the result of the operation
# of function f on each element of 
# the specified array a.
a = [1, 2, 3];
foreach(x -> println(x ^ 3), a)

Output:

1
8
27

Example 2:




# Julia program to illustrate 
# the use of foreach() method
  
# Getting the result of the operation
# of function f on each element of 
# the specified iterable c.
a = 1:3;
foreach(x -> println(x ^ 2), a)

Output:

1
4
9
My Personal Notes arrow_drop_up
Recommended Articles
Page :