Home Explore Blog CI



nushell

2nd chunk of `lang-guide/chapters/types/basic_types/closure.md`
287899e4d6ecbf00f85c91e854b489877ecc02191b0b0eef0000000100000955
   ```nu
   > def create_greeter [ greeting: string ]: nothing -> closure {
       {|name| $"($greeting), ($name)" }
     }

   > let greet = create_greeter "Hello"
   # Invoke the closure with `do`
   > do $greet Dalija
   Hello, Dalija
   > do $greet Ryan
   Hello, Ryan

   # Redefine greet with a new greeting
   > let greet = create_greeter "Aloha"
   > do $greet Kai
   Aloha, Kai
   ```

   Note that the `create_greeter` only needs to be defined once.

1. There are some restrictions on the kind of external values that can be closed over. Only immutable variables like those created with the `let` keyword or parameters to a custom command can be captured in a closure. Mutable variables created with the `mut` keyword cannot be captured in a closure. However, you can mutate an `$env` variable if used by the `--env` flag passed to the `do` keyword.

   If we try to create a closure that attempts to capture a mutable variable we get a compile error:

   ```nu
   if true {
     mut x = 9
     do {|p| $p + $x }
   }
   # => Error: Capture of mutable variable.
   ```

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.

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
   1..10 | each { print $in }
   ```

1. You can also pass closures themselves into a pipeline assuming the next command knows how to consume it. For example, the `do` example can be rewritten as:

   ```nu
   > {|a,b| $a + $b} | do $in 34 8
   43
   ```

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

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

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

- `all`
- `any`
- `collect`
- `do`
- `each`
- `explain`
- `filter`
- `group-by`
- `interleave`
- `items`
- `par-each`
- `reduce`
- `skip until`
- `skip while`
- `take until`
- `tee`
- `update`
- `upsert`
- `zip`

### Examples of using closures

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

Title: Nu Closures: Restrictions, Pipelines, and Common Commands
Summary
This section discusses limitations on capturing mutable variables in closures and the inability to pass closures to external commands. It explains how closures can utilize pipeline input as `$in` and be passed through pipelines themselves, assuming the consuming command knows how to handle them. The section also lists common commands that work with closures and provides a sample use case with the 'create_greeter' custom command.