diff --git a/gitlab-pages/docs/language-basics/functions.md b/gitlab-pages/docs/language-basics/functions.md index c43e5e33e..e68c93ed8 100644 --- a/gitlab-pages/docs/language-basics/functions.md +++ b/gitlab-pages/docs/language-basics/functions.md @@ -204,4 +204,55 @@ ligo evaluate-value gitlab-pages/docs/language-basics/src/functions/anon.religo # Outputs: 2 ``` + + +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. + + + +```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 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 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 ] +``` + + diff --git a/gitlab-pages/docs/language-basics/src/functions/incr_map.ligo b/gitlab-pages/docs/language-basics/src/functions/incr_map.ligo new file mode 100644 index 000000000..e5f915847 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/functions/incr_map.ligo @@ -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) diff --git a/gitlab-pages/docs/language-basics/src/functions/incr_map.mligo b/gitlab-pages/docs/language-basics/src/functions/incr_map.mligo new file mode 100644 index 000000000..b2dd363c8 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/functions/incr_map.mligo @@ -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 diff --git a/gitlab-pages/docs/language-basics/src/functions/incr_map.religo b/gitlab-pages/docs/language-basics/src/functions/incr_map.religo new file mode 100644 index 000000000..85b8a39f4 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/functions/incr_map.religo @@ -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);