From 8fd66ee401d7bf0088b58ceb7a767458f4c209f6 Mon Sep 17 00:00:00 2001 From: Jan Klass Date: Wed, 16 Jul 2025 19:22:25 +0200 Subject: [PATCH 1/2] 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. --- lang-guide/chapters/types/basic_types/closure.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lang-guide/chapters/types/basic_types/closure.md b/lang-guide/chapters/types/basic_types/closure.md index 6638e46e0f9..d8afc0fea06 100644 --- a/lang-guide/chapters/types/basic_types/closure.md +++ b/lang-guide/chapters/types/basic_types/closure.md @@ -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 `{||}` @@ -66,6 +67,7 @@ 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. 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: From 8050de441718acf2edf4d8923950f3d491bb26d1 Mon Sep 17 00:00:00 2001 From: Jan Klass Date: Wed, 16 Jul 2025 19:25:55 +0200 Subject: [PATCH 2/2] Extend Closure reference with additional examples --- .../chapters/types/basic_types/closure.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lang-guide/chapters/types/basic_types/closure.md b/lang-guide/chapters/types/basic_types/closure.md index d8afc0fea06..c60573e2be6 100644 --- a/lang-guide/chapters/types/basic_types/closure.md +++ b/lang-guide/chapters/types/basic_types/closure.md @@ -70,6 +70,18 @@ Closures are used in Nu extensively as parameters to iteration style commands li 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 @@ -85,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)