Alloy has plenty of logical connectives like and and or and implies. But I can't find true and false. Are they missing? At the moment I've been making do with 1=1 and 1=0, but this is rather hacky (and gives a compiler warning).
My reason, by the way, for wanting true and false is that I'm writing something that produces an .als file. My top-level .als file expects that my auto-generated .als file defines a wellformed predicate and a faulty predicate. Sometimes these predicates are complicated, but sometimes I just want wellformed[...] to return true, and faulty[...] to return false. This is why I want true and false in the Alloy language.
They're not built in for a good reason: see the FAQ on p137 of Software Abstractions (Daniel Jackson, MIT Press, 2012). The issue in short is that if they were built in, you'd have to be able to declare a relation over the booleans, and then because boolean expressions could evaluate to {} and {T,F}, the connectives would need to be defined over these values, and that seemed like a really bad idea.
Since an empty predicate is true, my favorite implementation of true and false is:
pred true {}
pred false { not true }
pred true {no none}
pred false {some none}
seems to work; but it would be nice to have these inbuilt.
Related
How do we write conditions in arango, that includes for loops. I can elaborate the requirement below.
My requirement is if a particular attribute(array type) exists in the arango collection, i would read data from the collection(that requires a loop) or else, might do the following :
return null
return empty string ""
do nothing.
Is this possible to achieve in arango?
The helping methods could be -->
-- has(collectionname, attributename)
-- The ternary operator ?:
let attribute1 = has(doc,"attribute1") ?(
for name in doc.attribute1.names
filter name.language == "xyz"
return name.name
) : ""
But this dosent work. Seems like arango compiler first attempts to compile the for loop, finds nulls and reports error as below. Instead, it should have compiled "has" function first for the ternary operator being used.
collection or array expected as operand to FOR loop; you provided a value of type 'null' (while executing)
If there is a better way of doing it, would appreciate the advice!!
Thanks in advance!
Nilotpal
Fakhrany here from ArangoDB.
Regarding your question, this is a known limitation.
From https://www.arangodb.com/docs/3.8/aql/fundamentals-limitations.html:
The following other limitations are known for AQL queries:
Subqueries that are used inside expressions are pulled out of these
expressions and executed beforehand. That means that subqueries do not
participate in lazy evaluation of operands, for example in the ternary
operator. Also see evaluation of subqueries.
Also noted here for the ternary operator:
https://www.arangodb.com/docs/3.8/aql/operators.html#ternary-operator.
An answer to the question what to do may be to use a FILTER before enumerating over the attributes:
FOR doc IN collection
/* the following filter will only let those documents passed in which "attribute1.names" is an array */
FILTER IS_ARRAY(doc.attribute1.names)
FOR name IN doc.attribute1.names
FILTER name.language == "xyz"
RETURN name.name
Other solutions are also possible. Depends a bit on the use case.
In this question I refer to the assert module that is included within the node.js core.
As far as I can tell the following two assertions are pretty much identical:
assert.equal(typeof path, "string", "argument 'path' must be a string");
assert(typeof path === "string", "argument 'path' must be a string");
Upon failure both variations report the same message:
AssertionError: argument 'path' must be a string
Are there any notable advantages of the former over the latter in this situation?
Well, depending on the test runner framework, assert.equal will probably give you a more descriptive error message. For instance, in this case:
assert.equal(typeof path, "string");
assert(typeof path === "string");
the first statement would give you a message along the lines of:
actual: number
expected: string
which already tells you that the test case fails because typeof path is a number.
The latter will only print something like this:
AssertionError: false == true
Also, note that if you want to check for strict equality (===), you should use assert.strictEqual instead of assert.equal.
assert.equal doesn't check for identity, only equality. It's equivalent to:
assert(typeof path == 'string', "argument 'path' must be a string");
The real equivalent would be assert.strictEqual, which uses the identity operator ===:
assert.strictEqual(typeof path, "string", "argument 'path' must be a string");
For typeof, no, there's no difference. You'll run into problems with other data types though:
> assert.equal('test', ['test']);
undefined
> 'test' == ['test']
true
> 'test' === ['test']
false
Both will work.
First, assert uses the coercive == operator, not strict ===
Also, when you read a lot of your unit tests or other people's unit tests, you'll strain your eyes on repetitive syntax. You'll love it when people will write this
assert.equal(aValue, anotherValue) // sexy
/** But you will hate people writing this. **/
assert.ok(aValue == anotherValue) // ugly
In the first case, you can see the condition being checked within the first 9 letters. You don't even need to look further. In the other case, you have to read 20 letters to know what the test is checking. It's more cryptic.
Also, assert.equal is more declarative of your intention than assert.ok.
Imagine you are writing a test for testing a set intersection. You would read better
assert.setIntersect(set1, set2) // wow
assert.ok(setIntersect(set1, set2)); // hm.
To sum it up, the advantage is in readability (thus maintainability) of you unit tests. It's not much, but it helps writing better understandable code.
As alexander said, too, you will have more precise error messages when test fail if you don't specify a message.
The user sets a boolean to true or false.
That does (exemple)
ElementNameone = true
ElementNametwo = false
ElementNamethree = true
Etc.
Now I have a string that is loaded from a file. The string called name can have values that are Nameone, Nametwo, Namethree, etc. Anyone of them at a time.
Now I would like to be able to do this
if Element .. name == true then
do something
Except I don't know how to do this properly.
I've tried to do
if not not ("Element" .. name) then
But it does not work.
Can anyone help ?
Thanks
Try this:
if _G["Element" .. name] == true then
-- do something
end
Note that this will work only if the variables set by the user (ElementNameone, .. etc.) are globals.
It's very likely you're solving the wrong problem.
You say "the user" sets these variables. How? An end user normally isn't going to be interacting directly with variables inside your program.
Can you use a table instead, with ElementNameone as the key and true or false as the associated value? If so, that would be a lot cleaner.
I have a list which contains two types of text. One type is used for authorization while other type is used for all other purposes.
The type used for authorization always uses the same text + some code after it.
I would like to compare content of these two types of text and separate them based on content.
My idea is to look for pattern in authorization type and if it matches the pattern then this would be marked as authorization, otherwise it would be marked as "other".
I researched about comparison of patterns in Groovy, but all variations I tried did not work for me. Here is the part which should do the comparison, I am obviously doing something wrong but I don't know what.
jdbcOperations.queryForList(sql).collect { row ->
if(assert (row['MSG'] ==~ /token/)){
mark as authorization
}
else{
mark as other
}
}
Sorry for the vague code, I can not share more than this.
I think you just missing the match for the rest of the text, since you are looking only for the first part to match.
assert ("abc" ==~ /abc/) == true
assert ("abcdefg" ==~ /abc/) == false
assert ("abcdefg" ==~ /abc(.*)/) == true // <--- This can also be made more complicated
If I have a Set that I know contains a single element, what's the best way to extract it? The best I can come up with is this, but it doesn't feel very groovy:
set = [1] as Set
e = set.toList()[0]
assert e == 1
If I'm dealing with a list, I've got lots of nice ways to get the element, none of which seem to work with Sets:
def list = [1]
e = list[0]
(e) = list
e = list.head()
One other possibility (which will work in Java or Groovy):
set.iterator().next()
A few alternatives, none of them very pretty:
set.iterator()[0]
set.find { true }
set.collect { it }[0]
Finally, if it's guaranteed that that set has only one item:
def e
set.each { e = it }
The underlying issue, of course, is that Java Sets provide no defined order (as mentioned in the Javadoc), and hence no ability to get the nth element (discussed in this question and this one). Hence, any solution is always to somehow convert the set to a list.
My guess is that either of the first two options involve the least data-copying, as they needn't construct a complete list of the set, but for a one-element set, that should hardly be a concern.
Since Java 8, here is another solution that will work for both Java and Groovy:
set.stream().findFirst().get()
Even when this question is quite old, I am sharing my just a bit prettier solution.
(set as List).first()
(set as List)[0]
If you need to take null into account (not the case in this question):
(set as List)?.first()
(set as List)?.get(index)
Hope it helps! :)