Skip to content

Commit ba23bda

Browse files
authored
Reduce and focus closure command examples (#1982)
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.
1 parent 49806fc commit ba23bda

File tree

1 file changed

+25
-59
lines changed

1 file changed

+25
-59
lines changed

lang-guide/chapters/types/basic_types/closure.md

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -135,78 +135,44 @@ Closures are used in Nu extensively as parameters to iteration style commands li
135135

136136
Here are a few select, concise examples to illustrate the broad use of closures with some of the aforementioned common Nushell commands:
137137

138-
#### `each` – Applying a transformation
139-
140138
The `each` command iterates over input, applying a closure to transform each item.
141139

142140
```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+
# => ╰───┴────╯
170147
```
171148

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).
179150

180151
```nu
181-
["kiwi" "apple" "banana"] | sort-by { |fruit_name| $fruit_name | str length }
152+
1..100 | where {|num| $num > 80 and $num < 86 }
153+
# => ╭───┬────╮
154+
# => │ 0 │ 81 │
155+
# => │ 1 │ 82 │
156+
# => │ 2 │ 83 │
157+
# => │ 3 │ 84 │
158+
# => │ 4 │ 85 │
159+
# => ╰───┴────╯
182160
```
183161

184-
_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.
187163

188164
```nu
189-
["kiwi" "apple" "banana"] # sorted by string length: kiwi (4), apple (5), banana (6)
165+
["apple" "banana" "kiwi"] | sort-by {|fruit_name| $fruit_name | str length }
166+
# => ╭───┬────────╮
167+
# => │ 0 │ kiwi │
168+
# => │ 1 │ apple │
169+
# => │ 2 │ banana │
170+
# => ╰───┴────────╯
190171
```
191172

192-
_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.
199174

200175
```nu
201-
[1 2 3 4] | reduce { |accumulator, current_value| $accumulator + $current_value }
176+
[1 2 3 4] | reduce {|accumulator, current_value| $accumulator + $current_value }
177+
# => 10
202178
```
203-
204-
_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

Comments
 (0)