From 55bff65635cee1493ff38f79542092c52dc1f258 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 20 Jul 2019 20:49:25 +0200 Subject: [PATCH] fix lambda shorthand, update array example --- desktop/sources/scripts/lisp.js | 4 ++-- examples/arrays.lisp | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index 71105dd..07b4165 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -69,10 +69,10 @@ function Lisp (input, lib) { __fn: function (input, context) { return async function () { const lambdaArguments = arguments - const keys = input.slice(2).filter(i => + const keys = [...new Set(input.slice(2).flat(100).filter(i => i.type === TYPES.identifier && i.value[0] === '%' - ).map(x => x.value).sort() + ).map(x => x.value).sort())] const lambdaScope = keys.reduce(function (acc, x, i) { acc[x] = lambdaArguments[i] return acc diff --git a/examples/arrays.lisp b/examples/arrays.lisp index 76c2054..e2dbf47 100644 --- a/examples/arrays.lisp +++ b/examples/arrays.lisp @@ -1,25 +1,26 @@ -(echo (map (lambda (a) (add a 1)) (1 2 3))) +(echo (map '(add %1 1) (1 2 3))) (echo (first (1 2 3))) (echo (rest (1 2 3))) (echo (filter - (lambda (a) (eq 0 (mod a 2))) + '(eq 0 (mod %1 2)) (1 2 3 4 5)) ) (clear) -(map (lambda (a) - (stroke (rect (mul a 30) 20 50 50) -1 "red")) +(map + '(stroke (rect (mul a 30) 20 50 50) +1 "red") (range 0 20 5)) -(map (lambda (a) - (stroke +(map + '(stroke (rect - (mul a 10) - (add 50 (mul (sin a) 40)) - a - (add 20 (mul (cos a) 50))) 1 "red")) - (range 0 200 5)) \ No newline at end of file + (mul %1 10) + (add 50 (mul (sin %1) 40)) + %1 + (add 20 (mul (cos %1) 50))) 1 "red") + (range 0 200 5)) +