why 1 and a retuned 1 are the same object [duplicate] - object

This question already has answers here:
Dart int and double being interned? Treated specially by identical()?
(3 answers)
Closed 1 year ago.
I'm confused,,who can explan it to me ,my code:
int fun1() {
return 1;
}
int fun2(int x) {
return x + x;
}
void main() {
print(identical(1, fun1()));
print(identical(2, fun2(1)));
}
result:
true
true
my question:
why const 1 and fun1() are the same object
const 2 and fun2() are the same object ?? thanks

It's because you executed fun1() which returned 1 and now you comparing 1 and 1 which is obviously true and true.

Related

Collection of closures are saved with wrong parameter [duplicate]

In a loop I create 4 closures and add them to a list:
closureList = []
for (int i=0; i<4; i++) {
def cl = {
def A=i;
}
closureList.add(cl)
}
closureList.each() {print it.call()println "";};
This results in the following output:
4
4
4
4
But I would have expected 0,1,2,3 instead. Why does the 4 closures have the same value for A?
Yeah, this catches people out, the free variable i is getting bound to the last value in the for loop, not the value at the time the closure was created.
You can either, change the loop into a closure based call:
closureList = (0..<4).collect { i ->
{ ->
def a = i
}
}
closureList.each { println it() }
Or create an extra variable that gets re-set every time round the loop, and use that:
closureList = []
for( i in (0..<4) ) {
int j = i
closureList << { ->
def a = j
}
}
closureList.each { println it() }
In both of these variants the variable closed by the closure is created afresh each time round the loop, so you get the result you'd expect

Im trying to sort a string in js. but im missing saomething [duplicate]

This question already has answers here:
Sorting in JavaScript: Shouldn't returning a boolean be enough for a comparison function?
(2 answers)
Closed 2 years ago.
function sortStrings(stringList) {
var retval = "";
var strings = stringList.split(',');
var sortedStrings = strings.sort((a,b) => {
return a < b;
});
sortedStrings.forEach((str) => {
retval = str + ',';
} );
return retval;
}
what mistake am I making
Your compare function has an error. The logical operator < only returns either true or false while the compare is expected to return an integer. Integers <0 mean that a < b, integers =0 mean a == b and integers >0 mean that a > b.
To accomplish that with strings, you should use localeCompare:
return a.localeCompare(b)

Find a palindrome in a string [duplicate]

This question already has answers here:
Check string for palindrome
(42 answers)
Closed 5 years ago.
I get a String and if that String contains a palindrome i have to return the palindrome. For example : "hsjwiUHUkajs" should return UHU, "hjakhdANNAjhad" should return ANNA.
How can I do that?
public static void main(String [] args) {
string s = "anna";
for(int i = 0; i<s.length; i++;) {
}
}
The way is explode all characters in a array.
Make a var with start at 1.
Make a for with compare 1# and #3 char.
Ex: a#a (# = 2# position).
If 1# = #3 then palindromo.
If not compare #2 = #3
If equal compare 1# = 4#
If equal palindromo.
If not them inclement start at 2.
Loop at for and compare 2# and #4
3# and #5

Determine if the integer is one digit and add a zero before it [duplicate]

This question already has answers here:
Leading zeros for Int in Swift
(12 answers)
Closed 7 years ago.
I need to check if the int is only one digit and if it is, I want to add a zero in front of it. I have this code but it doesnt work.
var minutes2 = Int(minutes)
var minutessize: Int = sizeofValue(minutes2)
if minutessize < 2 {
var needStringHere = "0\(minutes2)"
let a: Int? = needStringHere.toInt()
minutes2 = a!
}
You can just check if the minutes count is less than 10:
var str = "\(minutes2)"
if (minutes2 < 10) { str = "0\(minutes2)" }

Iterate and print content of groovy closures

In a loop I create 4 closures and add them to a list:
closureList = []
for (int i=0; i<4; i++) {
def cl = {
def A=i;
}
closureList.add(cl)
}
closureList.each() {print it.call()println "";};
This results in the following output:
4
4
4
4
But I would have expected 0,1,2,3 instead. Why does the 4 closures have the same value for A?
Yeah, this catches people out, the free variable i is getting bound to the last value in the for loop, not the value at the time the closure was created.
You can either, change the loop into a closure based call:
closureList = (0..<4).collect { i ->
{ ->
def a = i
}
}
closureList.each { println it() }
Or create an extra variable that gets re-set every time round the loop, and use that:
closureList = []
for( i in (0..<4) ) {
int j = i
closureList << { ->
def a = j
}
}
closureList.each { println it() }
In both of these variants the variable closed by the closure is created afresh each time round the loop, so you get the result you'd expect

Resources