-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement cond #15
base: main
Are you sure you want to change the base?
Implement cond #15
Conversation
73e1160
to
f768415
Compare
lib/expressions.js
Outdated
let header = '('; | ||
|
||
for (const identifier of this.identifiers) { | ||
if (header.length > 1) { | ||
header += ', '; | ||
} | ||
header += identifier; | ||
} | ||
|
||
header += ')'; | ||
|
||
return `${header} => ${this.toExpression()}`; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid creating an extra array as Murych suggested
} else if (operand instanceof OperationExpression) { | ||
for (const name of operand.identifiers.values()) { | ||
if (operand.identifiers !== undefined) { | ||
for (const name of operand.identifiers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think information expert works well here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Compact empty lines and one-operator blocks
- Move oll fields to constructor
lib/expressions.js
Outdated
let header = '('; | ||
for (const identifier of this.identifiers) { | ||
if (header.length > 1) header += ', '; | ||
header += identifier; | ||
} | ||
header += ')'; | ||
return `${header} => ${this.toExpression()}`; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understood moving from Array.from
to for..of
solution, please explain.
let header = '('; | |
for (const identifier of this.identifiers) { | |
if (header.length > 1) header += ', '; | |
header += identifier; | |
} | |
header += ')'; | |
return `${header} => ${this.toExpression()}`; | |
} | |
let header = ''; | |
for (const identifier of this.identifiers) { | |
if (header) header += ', '; | |
header += identifier; | |
} | |
return `(${header}) => ${this.toExpression()}`; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Murych suggested to avoid creating an extra array. Just not sure if concatenating strings is as fast as Array.join
lib/expressions.js
Outdated
// eslint-disable-next-line consistent-return | ||
interpret(context) { | ||
for (const { condition, consequents } of this.clauses) { | ||
if (condition.interpret(context) !== false) { | ||
return consequents.reduce( | ||
(_, consequent) => consequent.interpret(context), | ||
undefined, | ||
); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I do not like disabled eslint rules, just make return consistent.
!== false
is not ok, see fix- It is not ok to start reducing from undefined in most cases, it is also strange to use reduce with ignored accumulator arg
// eslint-disable-next-line consistent-return | |
interpret(context) { | |
for (const { condition, consequents } of this.clauses) { | |
if (condition.interpret(context) !== false) { | |
return consequents.reduce( | |
(_, consequent) => consequent.interpret(context), | |
undefined, | |
); | |
} | |
} | |
} | |
interpret(context) { | |
for (const { condition, consequents } of this.clauses) { | |
if (condition.interpret(context)) { | |
return consequents.reduce( | |
(_, consequent) => consequent.interpret(context), | |
undefined, | |
); | |
} | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do if(condition.interpret(context))
because in LISP condition is false only if it evaluates to nil
while in JavaScript it can be any falsy value like 0. E.g. (cond (0 x)) would be a true condition.
This is why I did !== false
(nil
is interpreted to false
in our current solution). I've made this explicit in the latest commit.
lib/expressions.js
Outdated
const consequentExpressions = consequents.map((consequent, i) => | ||
i === consequents.length - 1 | ||
? `return ${consequent.toExpression()};` | ||
: `${consequent.toExpression()};`, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const consequentExpressions = consequents.map((consequent, i) => | |
i === consequents.length - 1 | |
? `return ${consequent.toExpression()};` | |
: `${consequent.toExpression()};`, | |
); | |
const consequentExpressions = consequents.map((consequent, i) => { | |
const ex = consequent.toExpression(); | |
return i === consequents.length - 1 ? `return ${ex};` : `${ex};`; | |
}); |
lib/parser.js
Outdated
const head = tokens[0]; | ||
const tail = tokens.splice(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have head and tail utilities
We need to see you on the call one time |
#9
npm t
)npm run fix
)