Related Articles

Related Articles

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

1
8
27

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

1
4
9
My Personal Notes arrow_drop_up
Recommended Articles
Page :