From 4d437eaac1ad746ebb70282968b653a3ec7e670b Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 20 Jul 2019 20:34:23 +0200 Subject: [PATCH] add lambda shorthand with params --- desktop/sources/scripts/lisp.js | 8 ++++++-- examples/lambda.lisp | 7 +------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index 19bab38..71105dd 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -69,8 +69,12 @@ function Lisp (input, lib) { __fn: function (input, context) { return async function () { const lambdaArguments = arguments - const lambdaScope = [].reduce(function (acc, x, i) { - acc[x.value] = lambdaArguments[i] + const keys = input.slice(2).filter(i => + i.type === TYPES.identifier && + i.value[0] === '%' + ).map(x => x.value).sort() + const lambdaScope = keys.reduce(function (acc, x, i) { + acc[x] = lambdaArguments[i] return acc }, {}) return interpret(input.slice(1), new Context(lambdaScope, context)) diff --git a/examples/lambda.lisp b/examples/lambda.lisp index ed62eb4..d22fb5c 100644 --- a/examples/lambda.lisp +++ b/examples/lambda.lisp @@ -1,6 +1 @@ -( - -(echo (map '(add 1 2) (4 5 6)) - -) - +(echo (map '(add %1 2) (4 5 6))