You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/loops/about.md
+45-2
Original file line number
Diff line number
Diff line change
@@ -65,11 +65,54 @@ The examples so far loop over the range `1:10`, where the value is also the loop
65
65
More generally, the index may be needed and not just the value.
66
66
For this, the [`eachindex()`][eachindex] function is used, for example `for i in eachindex(my_array) ... end`.
67
67
68
+
## Comprehensions
69
+
70
+
Writing explicit loops tends to be less common in Julia than in many traditional languages, because there are various more concise options.
71
+
72
+
A particularly common situation is when we need to build a new vector from the elements of some other collection (vector, string, set ... there are many possibilities).
73
+
74
+
Anyone who likes list comprehensions in Python will be pleased to know that Julia can use [similar syntax][comprehensions].
75
+
76
+
The essence of this is to set up a very compact loop inside a vector.
77
+
78
+
The simplest syntax is of the form `result = [f(x) for x in some_collection]`.
79
+
80
+
With a traditional loop, that might be written:
81
+
82
+
```julia
83
+
result = []
84
+
for x in some_collection
85
+
push!(result, f(x))
86
+
end
87
+
```
88
+
89
+
Optionally, a conditional can be added at the end, to select only matching elements in the collection:
The syntax is _similar_ to Python, but not _identical_.
108
+
There is some divergence for multiple variables and/or multi-dimensional arrays, so this topic will be covered in more detail in a later Concept.
109
+
68
110
## Other options
69
111
70
-
As a modern, mostly-functional language, Julia (of course) has a variety of ways to operate on collections, not just an explicit loop.
112
+
As a modern, mostly-functional language, Julia (of course) has a variety of ways to operate on collections, not just an explicit loop, or a compacted loop in a comprehension.
71
113
72
-
Later concepts will look at alternatives such as comprehensions, broadcasting, and higher-order functions.
114
+
Later concepts will look at alternatives such as broadcasting and higher-order functions.
Copy file name to clipboardExpand all lines: concepts/loops/introduction.md
+32
Original file line number
Diff line number
Diff line change
@@ -61,3 +61,35 @@ The examples so far loop over the range `1:10`, where the value is also the loop
61
61
62
62
More generally, the index may be needed and not just the value.
63
63
For this, the `eachindex()` function is used, for example `for i in eachindex(my_array) ... end`.
64
+
65
+
## Comprehensions
66
+
67
+
Writing explicit loops tends to be less common in Julia than in many traditional languages, because there are various more concise options.
68
+
69
+
A particularly common situation is when we need to build a new vector from the elements of some other collection (vector, string, set ... there are many possibilities).
70
+
71
+
Anyone who likes list comprehensions in Python will be pleased to know that Julia can use similar syntax.
72
+
73
+
The essence of this is to set up a very compact loop inside a vector.
74
+
75
+
The simplest syntax is of the form `result = [f(x) for x in some_collection]`.
76
+
77
+
With a traditional loop, that might be written:
78
+
79
+
```julia
80
+
result = []
81
+
for x in some_collection
82
+
push!(result, f(x))
83
+
end
84
+
```
85
+
86
+
Optionally, a conditional can be added at the end, to select only matching elements in the collection:
0 commit comments