|
10 | 10 |
|
11 | 11 | Closures are used in Nu extensively as parameters to iteration style commands like `each`, `filter`, and `reduce`, to name but a few. A closure acts like a custom command that can be invoked either explicitly or by other commands. Closures can take parameters, return values and be passed to commands, either builtin or custom.
|
12 | 12 |
|
13 |
| -## Language Notes: |
| 13 | +## Language Notes |
14 | 14 |
|
15 | 15 | 1. A closure can be directly invoked using the [`do`](/commands/docs/do.md) command.
|
16 | 16 |
|
@@ -106,3 +106,83 @@ Closures are used in Nu extensively as parameters to iteration style commands li
|
106 | 106 | - `update`
|
107 | 107 | - `upsert`
|
108 | 108 | - `zip`
|
| 109 | + |
| 110 | +### Examples of using closures |
| 111 | + |
| 112 | +Here are a few select, concise examples to illustrate the broad use of closures with some of the aforementioned common Nushell commands: |
| 113 | + |
| 114 | +#### `each` – Applying a transformation |
| 115 | + |
| 116 | +The `each` command iterates over input, applying a closure to transform each item. |
| 117 | + |
| 118 | +```nu |
| 119 | +[1 2 3] | each { |num| $num * 10 } |
| 120 | +``` |
| 121 | + |
| 122 | +_Explanation:_ This takes a list of numbers. The closure `{|num| $num * 10}` is executed for each number (`num`), multiplying it by 10. |
| 123 | + |
| 124 | +**Output:** |
| 125 | + |
| 126 | +```nu |
| 127 | +[10 20 30] |
| 128 | +``` |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +#### `where` – Filtering data |
| 133 | + |
| 134 | +The `where` command filters data based on a condition defined in a closure. The closure must return true (keep) or false (discard). |
| 135 | + |
| 136 | +```nu |
| 137 | +ls | where { |file_info| $file_info.size > 1mb } |
| 138 | +``` |
| 139 | + |
| 140 | +_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. |
| 141 | + |
| 142 | +**Output:** |
| 143 | + |
| 144 | +```nu |
| 145 | +# A table of files larger than 1MB |
| 146 | +``` |
| 147 | + |
| 148 | +_Closure's role:_ Defines the operation to perform on every element. |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +#### `sort-by` – Custom sorting logic |
| 153 | + |
| 154 | +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. |
| 155 | + |
| 156 | +```nu |
| 157 | +["kiwi" "apple" "banana"] | sort-by { |fruit_name| $fruit_name | str length } |
| 158 | +``` |
| 159 | + |
| 160 | +_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. |
| 161 | + |
| 162 | +**Output:** |
| 163 | + |
| 164 | +```nu |
| 165 | +["kiwi" "apple" "banana"] # sorted by string length: kiwi (4), apple (5), banana (6) |
| 166 | +``` |
| 167 | + |
| 168 | +_Closure's role:_ Specifies the attribute or derived value to use for comparison during sorting. |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +#### `reduce` – Aggregating values |
| 173 | + |
| 174 | +The `reduce` command processes a list to accumulate a single result. The closure defines how to combine the current item with the accumulated value. |
| 175 | + |
| 176 | +```nu |
| 177 | +[1 2 3 4] | reduce { |accumulator, current_value| $accumulator + $current_value } |
| 178 | +``` |
| 179 | + |
| 180 | +_Explanation:_ This sums the numbers in the list. The closure `{|accumulator, current_value| $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. |
| 181 | + |
| 182 | +**Output:** |
| 183 | + |
| 184 | +```nu |
| 185 | +10 |
| 186 | +``` |
| 187 | + |
| 188 | +_Closure's role:_ Defines the operation for combining elements into a single accumulated value. |
0 commit comments