Skip to content

Commit 50c3513

Browse files
author
Colin Leach
committed
Merge remote-tracking branch 'origin/main'
2 parents 40cf0fd + 7b6ad79 commit 50c3513

File tree

3 files changed

+77
-24
lines changed

3 files changed

+77
-24
lines changed

concepts/loops/about.md

+45-2
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,54 @@ The examples so far loop over the range `1:10`, where the value is also the loop
6565
More generally, the index may be needed and not just the value.
6666
For this, the [`eachindex()`][eachindex] function is used, for example `for i in eachindex(my_array) ... end`.
6767

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:
90+
91+
```julia-repl
92+
# multiples of 3
93+
julia> [n^2 for n in 1:10 if n%3 == 0]
94+
3-element Vector{Int64}:
95+
9
96+
36
97+
81
98+
99+
# letters beyond 'h' in the alphabet
100+
julia> [uppercase(c) for c in "Julia" if c > 'h']
101+
3-element Vector{Char}:
102+
'U': ASCII/Unicode U+0055 (category Lu: Letter, uppercase)
103+
'L': ASCII/Unicode U+004C (category Lu: Letter, uppercase)
104+
'I': ASCII/Unicode U+0049 (category Lu: Letter, uppercase)
105+
```
106+
107+
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+
68110
## Other options
69111

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.
71113

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.
73115

74116
[loops]: https://docs.julialang.org/en/v1/manual/control-flow/#man-loops
75117
[eachindex]: https://docs.julialang.org/en/v1/base/arrays/#Base.eachindex
118+
[comprehensions]: https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions

concepts/loops/introduction.md

+32
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,35 @@ The examples so far loop over the range `1:10`, where the value is also the loop
6161

6262
More generally, the index may be needed and not just the value.
6363
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:
87+
88+
```julia-repl
89+
# multiples of 3
90+
julia> [n^2 for n in 1:10 if n%3 == 0]
91+
3-element Vector{Int64}:
92+
9
93+
36
94+
81
95+
```

concepts/vector-operations/about.md

-22
Original file line numberDiff line numberDiff line change
@@ -247,27 +247,6 @@ julia> a[condition]
247247
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
248248
```
249249

250-
## Comprehensions
251-
252-
Anyone who likes list comprehensions in Python will be pleased to know that Julia uses [similar syntax][comprehensions].
253-
254-
```julia-repl
255-
julia> [n for n in 1:10 if n%3 == 0]
256-
3-element Vector{Int64}:
257-
3
258-
6
259-
9
260-
261-
julia> [uppercase(c) for c in "Julia" if c > 'h']
262-
3-element Vector{Char}:
263-
'U': ASCII/Unicode U+0055 (category Lu: Letter, uppercase)
264-
'L': ASCII/Unicode U+004C (category Lu: Letter, uppercase)
265-
'I': ASCII/Unicode U+0049 (category Lu: Letter, uppercase)
266-
```
267-
268-
_Similar_ syntax, but not _identical_.
269-
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.
270-
271250
[vectors]: https://exercism.org/tracks/julia/concepts/arrays
272251
[ranges]: https://exercism.org/tracks/julia/concepts/ranges
273252
[sets]: https://exercism.org/tracks/julia/concepts/sets
@@ -276,4 +255,3 @@ There is some divergence for multiple variables and/or multi-dimensional arrays,
276255
[zip]: https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.zip
277256
[bitarray]: https://docs.julialang.org/en/v1/base/arrays/#Base.BitArray
278257
[broadcasting]: https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting
279-
[comprehensions]: https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions

0 commit comments

Comments
 (0)