From 68d0fcc56a75d50afc418dd25a501c7294c3bcee Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Mon, 22 Jul 2024 11:44:56 -0700 Subject: [PATCH] move examples to examples/ directory, add for loop examnple (#64) --- Makefile | 2 +- README.md | 6 ++++-- apply.gr => examples/apply.gr | 0 apply2.gr => examples/apply2.gr | 0 examples/for.gr | 18 ++++++++++++++++++ large_fact.gr => examples/large_fact.gr | 0 pi.gr => examples/pi.gr | 0 pi2.gr => examples/pi2.gr | 0 sample.gr => examples/sample.gr | 0 9 files changed, 23 insertions(+), 3 deletions(-) rename apply.gr => examples/apply.gr (100%) rename apply2.gr => examples/apply2.gr (100%) create mode 100644 examples/for.gr rename large_fact.gr => examples/large_fact.gr (100%) rename pi.gr => examples/pi.gr (100%) rename pi2.gr => examples/pi2.gr (100%) rename sample.gr => examples/sample.gr (100%) diff --git a/Makefile b/Makefile index db601a87..559edfee 100644 --- a/Makefile +++ b/Makefile @@ -64,7 +64,7 @@ wasm/wasm_exec.html: test: grol CGO_ENABLED=0 go test -tags $(GO_BUILD_TAGS) ./... - ./grol *.gr + ./grol examples/*.gr generate: go generate ./... # if this fails go install golang.org/x/tools/cmd/stringer@latest diff --git a/README.md b/README.md index 42edd132..9b97ff60 100644 --- a/README.md +++ b/README.md @@ -54,11 +54,13 @@ print, log macros and more all the time -See also [sample.gr](sample.gr) that you can run with +See also [sample.gr](examples/sample.gr) and others in that folder, that you can run with ``` -gorepl *.gr +gorepl examples/*.gr ``` +or copypaste to the online version on [grol.io](https://grol.io) + ## Dev mode: ```shell go install golang.org/x/tools/cmd/stringer@latest diff --git a/apply.gr b/examples/apply.gr similarity index 100% rename from apply.gr rename to examples/apply.gr diff --git a/apply2.gr b/examples/apply2.gr similarity index 100% rename from apply2.gr rename to examples/apply2.gr diff --git a/examples/for.gr b/examples/for.gr new file mode 100644 index 00000000..9d304853 --- /dev/null +++ b/examples/for.gr @@ -0,0 +1,18 @@ +// for loop example, functional style +// executes with 1..n passed to the function parameter. + +for=func(n, f) { + l=func(i,f) { // internal lambda with the index param + r = f(i) + if (i>=n) { + return r + } + l(i+1, f) + } + l(1,f) +} + +for(5, func(n) { + log("n is", n) + n // return value at the end +}) diff --git a/large_fact.gr b/examples/large_fact.gr similarity index 100% rename from large_fact.gr rename to examples/large_fact.gr diff --git a/pi.gr b/examples/pi.gr similarity index 100% rename from pi.gr rename to examples/pi.gr diff --git a/pi2.gr b/examples/pi2.gr similarity index 100% rename from pi2.gr rename to examples/pi2.gr diff --git a/sample.gr b/examples/sample.gr similarity index 100% rename from sample.gr rename to examples/sample.gr