Scala | How to get the object reference for comparison? [closed] - scala

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to get an object reference in Scala? I would like to compare those references to see if it's a new or old object which is being used.
var objArr: ArrayBuffer[someObj] = new ArrayBuffer[someObj](0)
// fill array
objArr(0) // how to get the internal reference of that array object and not the content of it?
Any help is highly appreciated!

Actually objArr(0) seems to be what you want
class MyClass
val x = new MyClass
var objArr: ArrayBuffer[MyClass] = ArrayBuffer(x)
objArr(0) eq x // true
objArr(0) eq new MyClass // false

Related

In the following code, the output is coming out to be null, not 1. Can someone explain it to me? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I declared one variable in dart as follows-
var Latitude;
void GetCLocation() async {
Latitude=1;
}
main(){
print(Latitude);
}
The problem is that the output is not coming out to be 1, but it is coming out to be null. What am I doing wrong?
Do this instead, you should call the function for it to be assign:
void main() {
var Latitude;
void GetCLocation() async {
Latitude=1;
}
GetCLocation();
print(Latitude);
}

SCALA Option & Map Construct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Came across this in a discussion on MONADs although a bit hard to follow. But the question is, I can get map and flatmap with Big Data, but the stuff here makes me feel very shaky. firstname => lastname, compounded =>, etc. Not getting it ... Who can explain? It works though.
val maybeFirstName = Option("Joe");
val maybeLastName = Option("Black");
val maybeFullName = maybeFirstName.flatMap { firstName =>
maybeLastName.map { lastName =>
firstName + " " + lastName
}
}
As with many compound operations, it sometimes makes sense to decipher it from the inside out.
maybeLastName.map(lastName => /*some op on lastName*/)
map() opens the Option and assigns the value, if it's there, to lastName. If the Option is None then there is no lastName and no operation on it. If the Option is not None then operation is performed and the result is re-wrapped into an Option.
flatMap() does the same thing except that it doesn't re-wrap the result. Instead the op inside is required to return a value already wrapped, i.e. an Option.
So, to review:
maybeFirstName.flatMap { firstName => //op on firstName must return Option
maybeLastName.map { lastName => //op on lastName should not return Option
/* op */
}
}
If either maybe...Name is None then the final result is None.
Map applies function on maybeLastName and retur Option or Option(Joe Black). So if you use map instead of flatMap, you get Option(Option(Joe Black)), because map always return new Option, so to remove inner option you need to use flatMap or combination map and flatten.
Here you can see how works flatten

How do I write a compound if statement in Groovy? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to write an if statement that tests two conditions in Groovy, but the language does not appear to support && or &. I'd like to avoid using a nested if statement if possible for this. How can I accomplish this?
Yes groove support the logical-and operator('&&') and bitwise-and operator('&').
class Example {
static void main(String[] args) {
// Initializing a local variable
int a = 2
//Check for the boolean condition
if (a<100 && a>0) {
//If the condition is true print the following statement
println("The value is less than 100");
} else {
//If the condition is false print the following statement
println("The value is greater than 100");
}
}
}
but it is not supporting '&&' or '&'
That's incorrect. Both the logical AND (&&) and the bitwise and (&) work in groovy. See the list of operators in the official documentation.

Declare implicit var of costume class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to declare a var in which then I will set with some kind of class depending on a certain condition, so :
var tmodule
if..
tmodule=ModuleInput(frame: CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height))
if..
tmodule=ModuleOut(frame: CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height))
I get an error on the declaration , and I can't find the right way to do it.
tmodule=tmodule:ModuleInput=ModuleInput(frame: CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height))
This doesn't make any sense. Perhaps you meant:
var tmodule: ModuleInput //type annotation necessary here
//...
let frameSize = self.view.frame.size
tmodule = ModuleInput(frame: CGRectMake(0, 0, frameSize.width, frameSize.height)) //type inferred here
tmodule=tmodule:ModuleInput=ModuleInput(....
should be
tmodule = ModuleInput(....

how to write unit test case for return value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a class which declare the dictionary tags
This is declare in class FirstClass
-(NSDictionary *)getTags{
return tags;
}
At unit testing
i created FirstClassTest class of ocUnit Testing
included #import "FirstClass.h"
-(void)setUP{
[super setUp];
// Allocations the class
_firstClass = [[FirstClass alloc]init];
}
-(void)tearDown{
_firstClass =nil;
[super tearDown];
}
-(void)testDictationGetValue{
// Need to write Unit test for that class
[_firstClass getTags];
}
This is my first time writing unit testing!
Can any one guide me to write proper unit test cases
#All Thanks in advance
First, it looks like you’re writing a plain getter. That doesn’t make much sense in modern Objective-C: if you just want to return a pointer to an instance variable, use a synthesized getter (and drop the get prefix).
Second, what do you want to test here? If you just want to test that the getter really does return some dictionary, then the test is essentially useless. Maybe the tags are calculated according to some other values? Then set up those values and check that the tags dictionary is computed correctly. Never write test just for their own sake.
IT should be!!!
-(void)testDictationGetValue
{
_firstClass = nil;
_firstClass = [[FirstClass alloc] init];
NSDictionary *t_dictionary = [_firstClass getTags];
if (t_dictionary == nil) {
STAssertNil(nil, #"TestFailed return value is nil");
}
else {
STAssertNotNil(t_dictionary, #"Retrun value");
}
}