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
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
Copy file name to clipboardExpand all lines: lang-guide/chapters/types/basic_types/closure.md
+24Lines changed: 24 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,7 @@ Closures are used in Nu extensively as parameters to iteration style commands li
20
20
```
21
21
22
22
1. The `|args|` list can also contain 0 arguments (`||`) or more than one argument `|arg1,arg2|`
23
+
23
24
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).
24
25
25
26
- 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
66
67
```
67
68
68
69
1. You cannot pass a closure to an external command; they are reserved only for Nu usage.
70
+
69
71
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.
70
72
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
+
71
85
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:
72
86
73
87
```nu
@@ -83,8 +97,18 @@ Closures are used in Nu extensively as parameters to iteration style commands li
83
97
84
98
1. As seen above, closures can be returned from a custom command. They can also be returned from another closure.
85
99
100
+
```nu
101
+
do {|| {|| 3 }} | do $in
102
+
# => 3
103
+
```
104
+
86
105
1. As closures are closely related to functions or commands, their parameters can be typed.
87
106
107
+
```nu
108
+
do {|a:int,b:int| $a + $b } 34 8
109
+
# => 42
110
+
```
111
+
88
112
## Common commands that can be used with a `closure`
0 commit comments