what does "::" mean in Scala ? [duplicate] - scala

This question already has answers here:
What do all of Scala's symbolic operators mean?
(11 answers)
Closed 8 years ago.
What does :: mean here ?
listeners ::= listener
list = num :: list
Specially I don't understand the "::" operator.

Its the list cons operator. It creates a new list whose head is first argument and whose tail is contents of the second argument.

Related

Why does order of operands matter when adding powershell strings and arrays? [duplicate]

This question already has answers here:
Why do integers in PowerShell compare by digits?
(4 answers)
Closed 4 years ago.
In powershell, when I add string + array the result is a string, but when I add array + string the result is an array? Why is that?
PowerShell converts the second operand to the type of the first operand (if it can).

What are the differences between these two Scala code segments regarding postfix toString method? [duplicate]

This question already has answers here:
toList on Range with suffix notation causes type mismatch
(2 answers)
Closed 5 years ago.
I am learning postfix unary operators in Scala.
The following can not compile:
val result = 43 toString
println(result)
However if I add one empty line inbetween the two lines, the code compile and produces right output:
val result = 43 toString
println(result)
What is the difference between these two segments?
BTW, I did not add "import scala.language.postfixOps".
Perhaps the issue is clearer if we use some other operator instead of toString.
// This parses as `List(1,2,3,4) ++ List(4,5,6)`
List(1,2,3,4) ++
List(4,5,6)
Basically, in order to make the above work, while also allowing things like foo ? (a postfix operator), Scala needs to know when it is OK to stop expecting a second argument (and accept that the expression is a postfix operator).
Its solution is give up on finding a second argument if there is an interceding new line.

Difference between & and && in Scala? [duplicate]

This question already has answers here:
Are there good uses for non-short-circuiting logical (boolean) operators in Java/Scala?
(4 answers)
What is the difference between the | and || or operators?
(12 answers)
Why doesn't the bitwise & operator short-circuit?
(5 answers)
Closed 5 years ago.
I am trying to figure out the difference between & and && in Scala. I got this after searching
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
Can somebody explain the same with example as am not clear, what verifying both operands means here. Are we talking about just Booleans?
Both are logical AND operators in Scala. The first form, &, will evaluate both operands values, for example:
val first = false
val second = true
if (first & second) println("Hello!")
Will evaluate both first and second before exiting the if condition, although we know that once a false appears in a logical AND, that entire expression will already yield false.
This is what && is for, and what it does is short-circuit the evaluation, meaning you only ever evaluate firsts value and then exit the conditional.
You can use bitwise AND (&) to perform bitwise operations on integers, which is similar to most programming languages.

'If' efficiency when evaluating 2 expressions [duplicate]

This question already has answers here:
Is Perl optimized to skip remaining logic operands if the answer is already decided?
(6 answers)
Closed 7 years ago.
if(expression1 or expression 2)
{
do something
}
If 'expression1' returns true, does the compiler starts to execute 'do something' or it evaluates the second expression too?
No, expression2 will not be evaluated if expression1 is true.
This is because or is short-circutted in perl: once the result of the entire expression is known, evaluation stops. Evaluation occurs from left to right.

lisp : how to remove a string from list of strings? [duplicate]

This question already has an answer here:
Test if array is inside a list in lisp
(1 answer)
Closed 7 years ago.
I have a problem with removing strings from a list of string I use this
(remove "lol" '("lol" "lol2" "lol")
but it returns the same list. What's the problem here?
You're running into the problem of trying to determine equality. I believe remove uses eql as its default equality tester. Unfortunately, two strings are not eql unless they're actually the same object.
Try:
`(remove "lol" '("lol" "lol2" "lol") :test #'equal)
Alternatively, if you know you will be testing strings, you could pass string= as your test function.
You should close the ')'
So write : (remove "lol" '("lol" "lol2" "lol"))