Skip to content

Commit 49806fc

Browse files
authored
Extend Closure reference with additional examples (#1981)
* Add missing list item spacing This is a list with for a considerable number of items significant item content. For consistency, add separating whitespace lines between the items. * Extend Closure reference with additional examples
1 parent dea39ef commit 49806fc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Closures are used in Nu extensively as parameters to iteration style commands li
2020
```
2121

2222
1. The `|args|` list can also contain 0 arguments (`||`) or more than one argument `|arg1,arg2|`
23+
2324
1. When there are 0 arguments, the `||` is optional as long as the closure cannot be mistaken for a record (which also uses the curly-brace style).
2425

2526
- An empty (no-op) closure can be represented as `{||}`
@@ -66,8 +67,21 @@ Closures are used in Nu extensively as parameters to iteration style commands li
6667
```
6768

6869
1. You cannot pass a closure to an external command; they are reserved only for Nu usage.
70+
6971
1. As with other types, you can also assign a closure to a variable, and closures can be included as values in a list or record.
7072

73+
```nu
74+
let c = {|x| $x + 1 }
75+
do $c 1
76+
# => 2
77+
```
78+
79+
```nu
80+
let c = [ {|x| $x + 1 } {|x| $x + 2 } ]
81+
do $c.1 1
82+
# => 3
83+
```
84+
7185
1. You can also use [pipeline input as `$in`](pipelines.html#pipeline-input-and-the-special-in-variable) in most closures instead of providing an explicit parameter. For example:
7286

7387
```nu
@@ -83,8 +97,18 @@ Closures are used in Nu extensively as parameters to iteration style commands li
8397

8498
1. As seen above, closures can be returned from a custom command. They can also be returned from another closure.
8599

100+
```nu
101+
do {|| {|| 3 }} | do $in
102+
# => 3
103+
```
104+
86105
1. As closures are closely related to functions or commands, their parameters can be typed.
87106

107+
```nu
108+
do {|a:int,b:int| $a + $b } 34 8
109+
# => 42
110+
```
111+
88112
## Common commands that can be used with a `closure`
89113

90114
- [`all`](/commands/docs/all.md)

0 commit comments

Comments
 (0)