From a6043ffef425e5aed9aac536605ed4344c1e5272 Mon Sep 17 00:00:00 2001 From: flaviut Date: Wed, 16 Jul 2014 19:47:06 -0400 Subject: [PATCH] Initial draft of first class functions --- content/lambdas.md | 5 ----- content/procvars.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) delete mode 100644 content/lambdas.md create mode 100644 content/procvars.md diff --git a/content/lambdas.md b/content/lambdas.md deleted file mode 100644 index 09cc300..0000000 --- a/content/lambdas.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -title: First Class Functions ---- - -# First Class Functions \ No newline at end of file diff --git a/content/procvars.md b/content/procvars.md new file mode 100644 index 0000000..f2f244c --- /dev/null +++ b/content/procvars.md @@ -0,0 +1,29 @@ +--- +title: First Class Functions +--- + +# First Class Functions + +Nimrod supports closures as well as passing functions. Two different syntaxes available for closures, proc syntax, which is identical to regular procedure syntax, and "do notation", which + +``` nimrod +import sequtils + +let powersOfTwo = @[1, 2, 4, 8, 16, 32, 64, 128, 256] + +echo powersOfTwo.filter do (x: int) -> bool : x > 32 +echo powersOfTwo.filter(proc (x: int): bool = x > 32) +``` +``` console +$ nimrod c -r filterclosure.nim +@[64, 128, 256] +@[64, 128, 256] +``` + +Existing procedures can also be passed as functions, but only if they are annotated with the `{.procvar.}` pragma. + +``` nimrod +proc greaterThan32(x: int): bool {.procvar.} = x > 32 + +echo powersOfTwo.filter(greaterThan32) +``` \ No newline at end of file