This repository was archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Add docs for lambdas & closures #356
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --- | ||
| title: Lambdas | ||
| description: Learn how to use anonymous functions in Noir programming language. | ||
| keywords: [Noir programming language, lambda, closure, function, anonymous function] | ||
| --- | ||
|
|
||
| ## Introduction | ||
|
|
||
| Lambdas are anonymous functions. The syntax is `|arg1, arg2, ..., argN| return_expression`. | ||
|
|
||
| ```rust | ||
| let add_50 = |val| val + 50; | ||
| assert(add_50(100) == 150); | ||
| ``` | ||
|
|
||
| A block can be used as the body of a lambda, allowing you to declare local variables inside it: | ||
|
|
||
| ```rust | ||
| let cool = || { | ||
| let x = 100; | ||
| let y = 100; | ||
| x + y | ||
| } | ||
|
|
||
| assert(cool() == 200); | ||
| ``` | ||
|
|
||
| ## Closures | ||
|
|
||
| Inside the body of a lambda, you can use variables defined in the enclosing function. Such lambdas are called **closures**. In this example `x` is defined inside `main` and is accessed from within the lambda: | ||
|
|
||
| ```rust | ||
| fn main() { | ||
| let x = 100; | ||
| let closure = || x + 150; | ||
| assert(closure() == 150); | ||
| } | ||
| ``` | ||
|
|
||
| ## Passing closures to higher-order functions | ||
|
|
||
| It may catch you by surprise that the following code fails to compile: | ||
|
|
||
| ```rust | ||
| fn foo(f: fn () -> Field) -> Field { | ||
| f() | ||
| } | ||
|
|
||
| fn main() { | ||
| let (x, y) = (50, 50); | ||
| assert(foo(|| x + y) == 100); // error :( | ||
| } | ||
| ``` | ||
|
|
||
| The reason is that the closure's capture environment affects its type - we have a closure that captures two Fields and `foo` | ||
| expects a regular function as an argument - those are incompatible. | ||
| :::note | ||
|
|
||
alexvitkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Variables contained within the `||` are the closure's parameters, and the expression that follows it is the closure's body. The capture environment is comprised of any variables used in the closure's body that are not parameters. | ||
|
|
||
| E.g. in |x| x + y, y would be a captured variable, but x would not be, since it is a parameter of the closure. | ||
|
|
||
| ::: | ||
| The syntax for the type of a closure is `fn[env](args) -> ret_type`, where `env` is the capture environment of the closure - | ||
| in this example that's `(Field, Field)`. | ||
|
|
||
| The best solution in our case is to make `foo` generic over the environment type of its parameter, so that it can be called | ||
| with closures with any environment, as well as with regular functions: | ||
|
|
||
| ```rust | ||
| fn foo<Env>(f: fn[Env]() -> Field) -> Field { | ||
| f() | ||
| } | ||
|
|
||
| fn main() { | ||
| let (x, y) = (50, 50); | ||
| assert(foo(|| x + y) == 100); // compiles fine | ||
| assert(foo(|| 60) == 60); // compiles fine | ||
| } | ||
| ``` | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.