Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lang-guide/chapters/types/basic_types/closure.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Closures are used in Nu extensively as parameters to iteration style commands li
```

1. The `|args|` list can also contain 0 arguments (`||`) or more than one argument `|arg1,arg2|`

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

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

1. You cannot pass a closure to an external command; they are reserved only for Nu usage.

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.

```nu
let c = {|x| $x + 1 }
do $c 1
# => 2
```

```nu
let c = [ {|x| $x + 1 } {|x| $x + 2 } ]
do $c.1 1
# => 3
```

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:

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

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

```nu
do {|| {|| 3 }} | do $in
# => 3
```

1. As closures are closely related to functions or commands, their parameters can be typed.

```nu
do {|a:int,b:int| $a + $b } 34 8
# => 42
```

## Common commands that can be used with a `closure`

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