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/map/about.md
+10-3Lines changed: 10 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,14 @@
1
1
# Introduction
2
2
3
3
In its most basic form, the higher-order function `map` accepts two arguments: a function `f` and a collection `coll`.
4
-
It applies `f` to each element of `coll`, returning a list of the results in the same order.
4
+
5
+
Given `f` and `coll`, `map` applies `f` to each element of `coll`, returning a list of the results in the same order.
5
6
6
7
```clojure
7
8
(map inc [123]) ; => (2 3 4)
8
9
```
9
10
10
-
The previous example applies the function `inc`, which increases a number by one, to each element of the vector `[1 2 3]`, and returns the result as the list `(2 3 4)`, where each element is the result of applying `inc` to the corresponding element of [1 2 3].
11
+
The previous example applies the function `inc`to each element of the vector and returns the result as the list `(2 3 4)`.
11
12
12
13
Let's see another example where we greet someone with the message "Welcome":
13
14
@@ -22,15 +23,21 @@ Let's see another example where we greet someone with the message "Welcome":
22
23
## Returning a lazy sequence
23
24
24
25
Previously we provided a simplified explanation of how `map` operates.
26
+
25
27
In reality, `map` does not return a list but a *lazy* sequence.
26
-
Although an explanation of lazy sequences is beyond the scope of this introduction, it essentially means that the elements of the resulting sequence are not generated until they are actually needed.
28
+
29
+
This essentially means that the elements of the resulting sequence are not generated until they are actually needed.
30
+
27
31
In other words, the function `f` passed to `map` is not applied until the elements of the resulting sequence are requested, for example, by printing them or retrieving their values.
32
+
28
33
This is advantageous when `f` is computationally expensive and only some elements are actually needed.
29
34
30
35
## Multiple collections
31
36
32
37
`map` allows multiple collections to be passed as input.
38
+
33
39
If the number of collections passed is `n`, the function `f` will receive `n` arguments, one from each collection.
40
+
34
41
The result is a sequence where the i-th element is obtained by applying `f` to the `n` elements at position i from each collection.
0 commit comments