Does DM-script have lazy evaluation? - lazy-evaluation

I am wondering if there is no lazy evaluation in dm-script?
I have the following code fragment that checks, if a TagGroup is valid. It throwns an error telling me the TagGroup is not defined so the TagGroupCountTags() function cannot be exeucted on this TagGroup. But that is the purpose of this fragment, checking if the TagGroup and the index are valid.
I tried to debug the code. The first statement tg.TagGroupIsValid() returns false, 0 <= index of course too. Why is the tg.TagGroupCountTags() still being executed? I can't beleave there is no lazy evaluation.
number index = -1; // of course this is not set to -1 in my original code, this is just for demonstration
TagGroup tg;
clearResults();
result(tg.TagGroupIsValid() + "\n"); // resturns 0
result((0 <= index) + "\n"); // returns 0
if(tg.TagGroupIsValid() && 0 <= index && index < tg.TagGroupCountTags()){
result("Valid.");
}
else{
result("Invalid.");
}

Yes, conditions in DM-Script are always completely evaluated (no lazy evaluation), so you have to put the If-clauses in a nested way. This is also true if you're using a tertiary operator (?).

Related

'Return' outside of function

def suspect_dict():
dict = pd.read_csv("suspectdict.csv", squeeze=True)
pattern = '|'.join(dict)
result = np.where(news_df["headline"].str.contains(pattern, na=False),1, 0)
for index, value in enumerate(result):
return {value}
I am trying to return 1 and 0 if words in "suspectdict" exists in "news_df".
Code works for
for index, value in enumerate(result):
print(f"{value}")
example of output:
0
1
0
1
1
When using return I got Syntax error: 'return' outside function
How do I fix this?
As the error says, your return is outside a function. Return should be used within a scope of a function, in your case, on suspect_dict() function.
You could either just loop the result and print it, without returning anything, also, you don't need to use enumerate as you're not dealing with indexes:
def suspect_dict():
dict = pd.read_csv("suspectdict.csv", squeeze=True)
pattern = '|'.join(dict)
result = np.where(news_df["headline"].str.contains(pattern, na=False),1, 0)
for value in result:
print(value)
But if you need to return the result, you could just use return result inside the function:
def suspect_dict():
dict = pd.read_csv("suspectdict.csv", squeeze=True)
pattern = '|'.join(dict)
result = np.where(news_df["headline"].str.contains(pattern,na=False),1,0)
return result
Note that python uses indentation to understand where a block of code begins and ends, so make sure that all codes that might belong to the function are well indented.

Can map::find and iterator::second be used in one condition

I have such a piece of code:
std::map<int, int> mp;
// do something
auto itor = mp.find(some_value);
if (itor != mp.end() && itor->second == some_other_value) {
// do something
}
I'm worrying about which expression would be evaluated first, itor != mp.end() or itor->second == some_other_value?
If the second one is evaluated firstly (because of some compiler-optimization maybe?), it might get an undefined behavior because itor == mp.end() may be true.
Should I worry about this issue so that I have to code like this:
if (itor != mp.end) {
if (itor->second == some_other_value) {
// do something
}
}
No worries; your code is fine.
If the first condition of if (a && b) is false, then the second condition is not evaluated.
Look up "short-circuit evaluation"
Wikipedia has some info.
Logical And - && - expression is evaluated left to right so itor != mp.end() will be evaluated initially. Moreover, it returns true only if both expressions return true, hence if the first one is false the second one is not checked.
so the first case should work.

Using find_if with vector object

this code always returns false
I tried to pass lambda parameter by reference and I had the same result
any tip please
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(5);
for (int x : v1)
{
auto it = find_if(v1.begin(), v1.end(), [x](int y){ return x == y; });
if (it != v1.end())
return false;
return true;
}
To check for duplicates (not remove them, just check for them) then you can do something like this:
Get the first value, and check for it in the rest of the container. You should not check the first element again because that's the element we are checking currently.
If a duplicate is not found then go on to the second element, and check from the third element forward. WE don't need to check the first element because that was done in the previous step.
Then continue like that for all elements.
If you find a duplicate then stop the searching and return true. If none are found then continue until the end, and then return false.
This can be done easily using iterators:
// Outer loop, current element to check
for (auto const i = v1.begin(); i != v1.end(); ++i)
{
// Inner loop, the element to check against
for (auto const j = i + 1; j != v1.end(); ++j)
{
if (*i == *j)
return true; // Duplicate found
}
}
// No duplicates found
return false;
The above code shows the principle, you could of course use std::find_if instead of the inner loop. The important thing is to start looking at the next element. All the previous have already been checked, and you should not compare the current value with itself.
Look at your condition in the if. What you meant it to do and what it actually does?
From using for it looks like you want it to go over all the container. Can you find a case where the body of the loop doesn't return immediately on the first iteration?

Comparison method violates its general contract in Spark

I tried to sort my List[Row] data set and here is how I made for it.
def getDiffMinute(ts1:Timestamp, ts2:Timestamp) : Long = {
if(ts1==null || ts2==null) 0
else (ts1.getTime - ts2.getTime) / 60000
}
myList.sortWith( (r1: Row, r2: Row) =>
MYUtils.getDiffMinute( r1.getAs[Timestamp]("time"), r2.getAs[Timestamp]("time")) < 0
)
Since getDiffMinute function return Long type data and wortWith need bool type, there is no way to get exception.
Some data lists work so well, but others(especially big data like more than 1gb) does not work with this error.
Comparison method violates its general contract
Any Idea of this?
I assume its because your comparator getDiffMinute is not properly written.
In your case lets say B is null, then diff(A,B) = 0, diff(B,C) = 0 so diff (A,C) should be 0 too, but it can be anything if neither A and C are nulls.
more info:
http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html#compare(T,%20T)

Two semicolons inside a for-loop parentheses

I'm customising a code I found over the internet (it's the Adafruit Tweet Receipt). I cannot understand many parts of the code but the most perplexing to me is the for-loop with two semicolons inside the parentheses
boolean jsonParse(int depth, byte endChar) {
int c, i;
boolean readName = true;
for(;;) { //<---------
while(isspace(c = timedRead())); // Scan past whitespace
if(c < 0) return false; // Timeout
if(c == endChar) return true; // EOD
if(c == '{') { // Object follows
if(!jsonParse(depth + 1, '}')) return false;
if(!depth) return true; // End of file
if(depth == resultsDepth) { // End of object in results list
What does for(;;) mean? (It's an Arduino program so I guess it's in C).
for(;;) {
}
functionally means
while (true) {
}
It will probably break the loop/ return from loop based on some condition inside the loop body.
The reason that for(;;) loops forever is because for has three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:
for(i = 0; i < 10; i++)
If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though true were there in its place. So for(;;) is the same as for(;true;)', which (as shown above) is the same as while (true).
The for loop has 3 components, separated by semi-colons. The first component runs before the looping starts and is commonly used to initialize a variable. The second is a condition. The condition is checked at the beginning of each iteration, and if it evaluates to true, then the code in the loop runs. The third components is executed at the end of the loop, before another iteration (starting with condition check) begins, and is often used to increment a variable.
In your case for(;;) means that it will loop forever since the condition is not present. The loop ends when the code returns or breaks.
Each clause of a for loop is optional. So when they are excluded, it still loops. for loops compile into while loops.
The end result becomes a check to initialize any variables, which concludes after nothing happening since it is empty, a check to the boolean condition in the second clause, which is not present so the loop starts, and once the loop hits the end bracket, a check to see if there is any code to run before checking the boolean condition again.
In code it looks like:
while(true){
}
Here's What Wikipedia Says About it
Use as infinite loops
This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for-loop can be used (with empty expressions), such as:
for (;;)
//loop body
This style is used instead of infinite while(1) loops to avoid a type conversion warning in some C/C++ compilers.Some programmers prefer the more succinct for(;;) form over the semantically equivalent but more verbose while (true) form.

Resources