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
In this section of the language reference we want to explore and demonstrate examples of variance of use.
The unnecessary layout separation (headlines, horizontal line separators) and command elaboration took away from that.
This change focuses the examples into demonstrating different closure use cases and behaviors in the context of commands.
It also adjusts the examples to have one code block with input and output.
It also improves some code examples, like the sort-by actually doing some sorting, demonstrating.
With the sometimes adjusted examples and documented output, the closure behavior does not need the redundant explanation.
Copy file name to clipboardExpand all lines: lang-guide/chapters/types/basic_types/closure.md
+25-59Lines changed: 25 additions & 59 deletions
Original file line number
Diff line number
Diff line change
@@ -135,78 +135,44 @@ Closures are used in Nu extensively as parameters to iteration style commands li
135
135
136
136
Here are a few select, concise examples to illustrate the broad use of closures with some of the aforementioned common Nushell commands:
137
137
138
-
#### `each` – Applying a transformation
139
-
140
138
The `each` command iterates over input, applying a closure to transform each item.
141
139
142
140
```nu
143
-
[1 2 3] | each { |num| $num * 10 }
144
-
```
145
-
146
-
_Explanation:_ This takes a list of numbers. The closure `{|num| $num * 10}` is executed for each number (`num`), multiplying it by 10.
147
-
148
-
**Output:**
149
-
150
-
```nu
151
-
[10 20 30]
152
-
```
153
-
154
-
---
155
-
156
-
#### `where` – Filtering data
157
-
158
-
The `where` command filters data based on a condition defined in a closure. The closure must return true (keep) or false (discard).
159
-
160
-
```nu
161
-
ls | where { |file_info| $file_info.size > 1mb }
162
-
```
163
-
164
-
_Explanation:_ This lists files and then filters them. The closure `{|file_info| $file_info.size > 1mb}` checks if each file's size is greater than 1 megabyte.
165
-
166
-
**Output:**
167
-
168
-
```nu
169
-
# A table of files larger than 1MB
141
+
[1 2 3] | each {|num| $num * 10 }
142
+
# => ╭───┬────╮
143
+
# => │ 0 │ 10 │
144
+
# => │ 1 │ 20 │
145
+
# => │ 2 │ 30 │
146
+
# => ╰───┴────╯
170
147
```
171
148
172
-
_Closure's role:_ Defines the operation to perform on every element.
173
-
174
-
---
175
-
176
-
#### `sort-by` – Custom sorting logic
177
-
178
-
The `sort-by` command sorts a list or table. The closure is used to extract or calculate the value to sort on for each item.
149
+
The `where` command filters data based on the result of a closure (true: keep, false: reject).
_Explanation:_ This sorts a list of fruit names. The closure `{|fruit_name| $fruit_name | str length}` calculates the length of each fruit name. `sort-by` then uses these lengths for sorting.
185
-
186
-
**Output:**
162
+
The `sort-by` command sorts a list or table. The closure determines and returns the sort key.
187
163
188
164
```nu
189
-
["kiwi" "apple" "banana"] # sorted by string length: kiwi (4), apple (5), banana (6)
_Closure's role:_ Specifies the attribute or derived value to use for comparison during sorting.
193
-
194
-
---
195
-
196
-
#### `reduce` – Aggregating values
197
-
198
-
The `reduce` command processes a list to accumulate a single result. The closure defines how to combine the current item with the accumulated value.
173
+
The `reduce` command processes a list to accumulate a single result. The closure defines how to combine the current item with the (intermediate) accumulated value.
_Explanation:_ This sums the numbers in the list. The closure `{|current_value, accumulator| $accumulator + $current_value}` adds the `current_value` to the `accumulator`. By default, the first item is the initial accumulator, and iteration starts from the second.
205
-
206
-
**Output:**
207
-
208
-
```nu
209
-
10
210
-
```
211
-
212
-
_Closure's role:_ Defines the operation for combining elements into a single accumulated value.
0 commit comments