"undefined" in Coffeescript - coffeescript

b
if b?
transforms to
b;
if (typeof b !== "undefined" && b !== null)
If I set b = [], I get if (b != null) {
Where is undefined? It's problem to catch undefined in array (after unique).
When I did
a = [1,1,2,2,3,3,4,444,4,4,4]
a.unique()
I can get something like this:
a = [1,2,3,4,444,undefined]
And I can't catch this, because in case a?[key] - I got only check for != null
What I must do?

I don't see the problem. undefined == null is true in JavaScript and undefined != null is false so if(b != null) covers the b === undefined case. Something like this:
a = [ undefined ]
if(a[0]?)
console.log('if')
else
console.log('else')
will produce else on the console as expected, so will this:
a = undefined
if(a?[0])
console.log('if')
else
console.log('else')

Related

dart null safty issue when try to add value to map element

I am using Dart but i am facing null safty issue with following code
RxMap<Product,int>cartItems=Map<Product,int>().obs;
void updateCart(Product product,String type){
if(type=="plus") {
cartItems.value[product]++;
}
else {
cartItems.value[product]--;
}
}
i got the following error message
the method '+' can't be unconditionally invoked because the receiver can be 'null'
i tried to add null check to the target as following
cartItems.value![product]++;
You can give a default value if null.
cartItems.value[product]??0 +1
Or force assert to non null value like this.It may throw exception if element not present in HashMap
cartItems.value[product]!+1
In your question you are asserting not null for HashMap not the value of the HashMap.
The problem is that cartItems.value is a Map and it's possible that cartItems.value[product] is null. In this case you can't add or remove 1 to null.
So you should do like the following:
if (type == "plus") {
cartItems.value[product] = (cartItems.value[product] ?? 0) + 1;
} else {
cartItems.value[product] = (cartItems.value[product] ?? 0) - 1;
}
Using (cartItems.value[product] ?? 0) you're saying that if cartItems.value[product] is null 0 is used instead.
Also note that in the else clause, when cartItems.value[product] == null, you're trying to remove 1 to something that doesn't exist, so in that case it may be best to throw an exception:
int? currentValue = cartItems.value[product];
if (currentValue == null) {
throw Exception('Trying to remove on a null object');
}
cartItems.value[product] = currentValue - 1;

Exceptions with if else statement in Scala

if ( option != "AB" || option != "BC"|| option != "CD") {
try {
var option = dbutils.widgets.get("option")
}
catch {
case e: Exception => println("The option shoud be following AB, BC or CD " + option)
}
}
I am trying to use exception in scala
However, the widget value is not throwing an exception when I use the value such as
"XY" or any other value than AB, BC or CD.
Please can someone let me know what I am doing wrong here. Thank you
If AB,BC & CD are the valid values, then your predicate should look like this:
if(option == "AB" || option == "BC" || option == "CD")
Also, you can use the require function to enforce the predicate and throw an exception if the input doesn't match:
def getWidget(option: String) = try {
require(option == "AB" || option == "BC"|| option == "CD", s"The option shoud be following AB, BC or CD instead got $option")
dbutils.widgets.get("option")
} catch { case e: Exception => println(e) }

Coursera scala assignment objects : week 3 descendingByRetweet

I am trying to figure out whats wrong in my solution for the coursera Scala course , 3rd assignment. I have figured out everything else , other than the method to convert a TweetSet into a descending tweetList.
Here are the main methods : mostRetweeted for Non-Empty Set.
def mostRetweeted: Tweet = {
val mostRetweetedLeft = left.mostRetweeted
val mostRetweetedRight = right.mostRetweeted
if(mostRetweetedLeft != null && mostRetweetedRight!=null)
if(elem.retweets > mostRetweetedRight.retweets && elem.retweets > mostRetweetedLeft.retweets)
return elem
else if(elem.retweets > mostRetweetedRight.retweets && elem.retweets < mostRetweetedLeft.retweets)
return mostRetweetedLeft
else
return mostRetweetedRight
else if(mostRetweetedLeft == null && mostRetweetedRight!=null)
if(elem.retweets > mostRetweetedRight.retweets)
return elem
else
return mostRetweetedRight
else if(mostRetweetedLeft != null && mostRetweetedRight==null)
if(elem.retweets > mostRetweetedLeft.retweets)
return elem
else
return mostRetweetedLeft
else
return elem
}
mostRetweeted for empty Set
def mostRetweeted: Tweet = null
And finally this method , uses an accumulator to form the list by using the above methods to calculate the most retweeted tweet.
def descendingByRetweet: TweetList = {
def descendingByRetweet(tweetSet: TweetSet, tweetList : TweetList):TweetList = {
val mostTweeted:Tweet = tweetSet.mostRetweeted
// empty set returns null for mostRetweeted method
if(mostTweeted == null)
return tweetList
else {
descendingByRetweet(tweetSet.remove(mostTweeted),tweetList.add(mostTweeted))
}
}
descendingByRetweet(this,Nil)
}
As you might have noticed I added an add method to TweetList
For Nil
def add(tweet: Tweet): TweetList = new Cons(tweet,Nil)
For Cons
def add(tweet: Tweet): TweetList = new Cons(head,tail add tweet)
I have also tried some online solutions , but those did not work either.
https://coderwall.com/p/_akojq/scala-week-3
Just like my solution , this gives an "Almost" descending list , but the list is not perfectly descending. Anyways I have spent the entire day and have passed the assignment, so am moving on to the next week.
Your code for descendingByRetweet looks correct thought it could be written shorter. I think your problem could be in the mostRetweeted method. In this part of the code:
if(elem.retweets > mostRetweetedRight.retweets && elem.retweets > mostRetweetedLeft.retweets)
return elem
else if(elem.retweets > mostRetweetedRight.retweets && elem.retweets < mostRetweetedLeft.retweets)
return mostRetweetedLeft
else
return mostRetweetedRight
Consider the case where, for example, elem.retweets = 5, mostRetweetedRight.retweets = 0 and mostRetweetedLeft.retweets = 5. The return value would be mostRetweetedRight which obviously is incorrect. You should add >= and <= to one of the if statements depending on your desired outcome.

Are "&&" and "," the same in Swift?

As the title says, I just came across a case where if && (AND) and , give the same result in Swift. I tested the code below:
let a = 5
let b = 6
if a<6, b>0 {
print("should be true")
}
if a<6, b<0 {
print("should be false")
}
if a>6, b>0 {
print("should be false")
}
if a>6, b<0 {
print("should be false")
}
It only logs:
should be true
So, the behavior of , is just like &&, am I correct?
They can be used in similar situations but that does not mean they are exactly the same.
Consider:
if (a && b) || c
you cannot write
if (a, b) || c
even a && b || c is different from a, b || c.
if a, b is something like
if a {
if b {
...
}
}
Both expressions have to be evaluated to true but they are still two separate expressions. We shouldn't imagine && there.
Why do we need the , operator?
The operator is needed to combine optional binding with boolean conditions, e.g.
if let a = a, a.isValid() {
becuase && wouldn't help us in such situations.
They're different in that a comma will take the lowest precedence possible. i.e everything else will be executed before the commas are checked.
// (true || false) && false
if true || false, false {
print("I will never be executed")
}
// true || (false && false)
if true || false && false {
print("I am always executed")
}

Checking multiple conditions for a string in C#

I want to check multiple conditions in a string in C# but it throws error saying Cannot use && for string or boolean
if ((duStart.Trim() != "" && duStart.Trim() != null) &&(duEnd.Trim() != "" && duEnd.Trim() != null))
{
//do this
}
else
//do that
The code you've given compiles fine. Here's a short but complete program - I've changed the whitespace of your line of code, but that's all:
using System;
class Test
{
static void Main()
{
string duStart = "X";
string duEnd = "X";
if ((duStart.Trim() != "" && duStart.Trim() != null) &&
(duEnd.Trim() != "" && duEnd.Trim() != null))
{
Console.WriteLine("Yes");
}
}
}
Having said that:
If you're going to use the same value (the trimmed version of duStart, for example) multiple times, there seems little point in computing it twice. I'd have used extra local variables (trimmedStart, trimmedEnd) here
Trim never returns null, so those tests are pointless.
Using string.IsNullOrWhitespace is probably a better idea here. Why bother creating strings that you're never going to use?
you can simplify the condition by writing:
if( !string.IsNullOrEmpty(duStart.Trim()) && !string.isNullOrEmpty(duEnd.Trim()) )
{
}
Check for the Null first for duStart and duEnd. Then try Trim the string. Trim cannot be applied on a null value. So, below code block should work for you.
if ((duStart != null && duStart.Trim() != "") && (duEnd != null && duEnd.Trim() != ""))
{
//do this
}
else
{
//do that
}