Added an example about lambdas.
This commit is contained in:
parent
3583f72cb4
commit
9f9f5c82ae
@ -204,4 +204,55 @@ ligo evaluate-value gitlab-pages/docs/language-basics/src/functions/anon.religo
|
||||
# Outputs: 2
|
||||
```
|
||||
|
||||
<!--END_DOCUSAURUS_CODE_TABS-->
|
||||
|
||||
If the example above seems contrived, here is a more common design
|
||||
pattern for lambdas: to be used as parameters to functions. Consider
|
||||
the use case of having a list of integers and mapping the increment
|
||||
function to all its elements.
|
||||
|
||||
<!--DOCUSAURUS_CODE_TABS-->
|
||||
<!--Pascaligo-->
|
||||
```pascaligo group=c
|
||||
function incr_map (const l : list (int)) : list (int) is
|
||||
list_map (function (const i : int) : int is i + 1, l)
|
||||
```
|
||||
You can call the function `incr_map` defined above using the LIGO compiler
|
||||
like so:
|
||||
```shell
|
||||
ligo run-function
|
||||
gitlab-pages/docs/language-basics/src/functions/incr_map.ligo incr_map
|
||||
"list [1;2;3]"
|
||||
# Outputs: [ 2 ; 3 ; 4 ]
|
||||
```
|
||||
|
||||
<!--Cameligo-->
|
||||
```cameligo group=c
|
||||
let incr_map (l : int list) : int list =
|
||||
List.map (fun (i : int) -> i + 1) l
|
||||
```
|
||||
You can call the function `incr_map` defined above using the LIGO compiler
|
||||
like so:
|
||||
```shell
|
||||
ligo run-function
|
||||
gitlab-pages/docs/language-basics/src/functions/incr_map.mligo incr_map
|
||||
"list [1;2;3]"
|
||||
# Outputs: [ 2 ; 3 ; 4 ]
|
||||
```
|
||||
|
||||
<!--Reasonligo-->
|
||||
```reasonligo group=c
|
||||
let incr_map = (l : list (int)) : list (int) =>
|
||||
List.map ((i : int) => i + 1, l);
|
||||
```
|
||||
You can call the function `incr_map` defined above using the LIGO compiler
|
||||
like so:
|
||||
```shell
|
||||
ligo run-function
|
||||
gitlab-pages/docs/language-basics/src/functions/incr_map.religo incr_map
|
||||
"list [1;2;3]"
|
||||
# Outputs: [ 2 ; 3 ; 4 ]
|
||||
```
|
||||
|
||||
|
||||
<!--END_DOCUSAURUS_CODE_TABS-->
|
||||
|
@ -0,0 +1,5 @@
|
||||
function increment (const b : int) : int is
|
||||
(function (const a : int) : int is a + 1) (b)
|
||||
|
||||
function incr_map (const l : list (int)) : list (int) is
|
||||
list_map (function (const i : int) : int is i + 1, l)
|
@ -0,0 +1,4 @@
|
||||
let increment (b : int) : int = (fun (a : int) -> a + 1) b
|
||||
|
||||
let incr_map (l : int list) : int list =
|
||||
List.map (fun (i : int) -> i + 1) l
|
@ -0,0 +1,4 @@
|
||||
let increment = (b : int) : int => ((a : int) : int => a + 1)(b);
|
||||
|
||||
let incr_map = (l : list (int)) : list (int) =>
|
||||
List.map ((i : int) => i + 1, l);
|
Loading…
Reference in New Issue
Block a user