From 4167db55d42bfb4306b00b7ce1cf35744538746d Mon Sep 17 00:00:00 2001 From: leonid-shutov Date: Thu, 23 Jan 2025 23:07:33 +0100 Subject: [PATCH] simplify interpret --- lib/expressions.js | 10 +++++----- test/cases.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/expressions.js b/lib/expressions.js index b869d98..4776345 100644 --- a/lib/expressions.js +++ b/lib/expressions.js @@ -129,16 +129,16 @@ class ConditionExpression { } } - // eslint-disable-next-line consistent-return interpret(context) { for (const { condition, consequents } of this.clauses) { if (condition.interpret(context) !== INTERPRETED_NIL) { - return consequents.reduce( - (_, consequent) => consequent.interpret(context), - undefined, - ); + for (let i = 0; i < consequents.length; i++) { + const interpreted = consequents[i].interpret(context); + if (i === consequents.length - 1) return interpreted; + } } } + return INTERPRETED_NIL; } toExpression() { diff --git a/test/cases.js b/test/cases.js index 3aea644..e83e62f 100644 --- a/test/cases.js +++ b/test/cases.js @@ -71,6 +71,6 @@ test('Evaluate cond (has a match)', () => { test('Evaluate cond (no match)', () => { const program = '(cond ((eq x 100)(+ 6 5)(list 1 4)) ((eq y x)(list 5 7)))'; const result = evaluate(program, { x: 99, y: 98 }); - const expected = undefined; + const expected = false; assert.strictEqual(result, expected, 'cond failed'); });