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
30 changes: 15 additions & 15 deletions lang-guide/chapters/types/basic_types/closure.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Closures are used in Nu extensively as parameters to iteration style commands li
1. A closure can be directly invoked using the [`do`](/commands/docs/do.md) command.

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

1. The `|args|` list can also contain 0 arguments (`||`) or more than one argument `|arg1,arg2|`
Expand All @@ -34,21 +34,21 @@ Closures are used in Nu extensively as parameters to iteration style commands li
- However, the closure that is returned has "captured" the value represented by `$greeting` and can still access it later when the closure itself is called.

```nu
> def create_greeter [ greeting: string ]: nothing -> closure {
{|name| $"($greeting), ($name)" }
}
def create_greeter [ greeting: string ]: nothing -> closure {
{|name| $"($greeting), ($name)" }
}

> let greet = create_greeter "Hello"
let greet = create_greeter "Hello"
# Invoke the closure with `do`
> do $greet Dalija
Hello, Dalija
> do $greet Ryan
Hello, Ryan
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
let greet = create_greeter "Aloha"
do $greet Kai
# => Aloha, Kai
```

Note that the `create_greeter` only needs to be defined once.
Expand Down Expand Up @@ -77,8 +77,8 @@ Closures are used in Nu extensively as parameters to iteration style commands li
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
{|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.
Expand Down