Replies: 3 comments 4 replies
-
|
You could try making your own function with the definition of truthy that you need, for example: # if a Matrix has a size that is not empty, then
isTruthy = typed({
Matrix: f(x) = sum(x.size()) > 0,
any: f(x) = x ? true : false
})
isTruthy([]) # false
isTruthy([1]) # trueIf it fits your needs better, you could make it outside the parser and import math.import({
isTruthy: math.typed({
Matrix: x => math.sum(x.size()) > 0,
any: x => math.equalScalar(x, true)
})})isTruthy([]) # false
isTruthy([1]) # trueCurrently the compare node operator doesn't support matrices, I guess there is no consensus of what a truthy matrix is. mathjs/src/expression/node/ConditionalNode.js Lines 16 to 42 in 50bd26c |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for bringing this up. It's indeed odd that To do what you are looking for though is different: see if a matrix is defined or not. I like David's example of creating an |
Beta Was this translation helpful? Give feedback.
-
|
Adding a function, ex. Yes, then it would need to be decided if lots of things like 0, [[0,0],[0,0]], and the empty string are truthy, but the experiences of other language designers can be guides to this. I think there's a lot of disagreement about this across languages. One of the consequences of that is needing to add an idiomatic way to check for membership in objects and perhaps elsewhere. Maybe an That way the user could cleanly express that they want to know if |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This question regards the expression language.
How does one check if any arbitrary thing is falsy without possibly throwing an error?
Executing these:
... give you what you'd expect:
But this:
... throws a
TypeError: Unsupported type of condition "DenseMatrix". This one:... throws
TypeError: Unexpected type of argument in function equalScalar (expected: number or bigint or string or boolean or BigNumber or Complex or Fraction, actual: identifier | undefined, index: 1). That is also surprising to me. This https://mathjs.org/docs/reference/functions/equal.html says, "Values null and undefined are compared strictly, thus null is only equal to null and nothing else, and undefined is only equal to undefined". That appears to imply that those calls to equal() return a false value, not throw an error.It looks like this does good equality checks without throwing errors no matter the type of the arguments:
You get:
... but those are pretty cumbersome. Is this the right way to do it?
The specific problem where this came up for me was in allowing expression writers to check an object to see if a key has any value, and also ensure that the value attached to the key isn't null or undefined.
related:
#1051
#3129
Beta Was this translation helpful? Give feedback.
All reactions