Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Q4b #600

Open
sshawnlim opened this issue Apr 27, 2022 · 9 comments
Open

Mock Q4b #600

sshawnlim opened this issue Apr 27, 2022 · 9 comments

Comments

@sshawnlim
Copy link

Hi, is anyone able to help explain why my code doesn't work and if anyone has an elegant answer for this question!

MockFinals Q4b

Appreciate the help!

@sshawnlim
Copy link
Author

Oops so sorry the image turned out to be a bit small

@jlee1007
Copy link

MyStream should be in the bracket?

@pohja1999
Copy link

pohja1999 commented Apr 27, 2022

hi, hmm can you check your return value ... MyStream.mapper <<< the dot there w the mapper is kinda wrong already.. ? unless you do have a property called mapper. 1) you realised you have to return a new stream , 2) you realised you need to override the get method. 3) you don't have a seed property/var and your get method has to be overridden. The only way you can complete your get method of this new stream is through your enclosing class (MyStreamT)'s get. And to do so, you need to use MyStream.this.get() to retrieve the enclosing value because, your current this refers to the MyStreamR

@txytxytxytxytxytxy
Copy link

hii, i'm not sure if this is correct but this is mine!

<R> MyStream<R> map(Function<T, R> func) { return MyStream.generate(() -> func.apply(this.get())); }

@shotnothing
Copy link

shotnothing commented Apr 27, 2022

Like @pohja1999 said, MyStream.mapper does not exist. MyStream is an abstract class, so you should override the get() method anonymously, else your this.get() means nothing . Also I don't think you strictly need to for this question, but it might be good to use PECS as boilerplate i.e.

Function<? super T, ? extends R>

@pohja1999
Copy link

pohja1999 commented Apr 27, 2022

Hmm, i think this works too in this scenario since your get method now points to the new Stream's get() which is now an overridden version of the original abstract class's version and since the concrete class can never be created without a defined get method.

Im kinda curious too on what test cases can be used to differentiate these two methods.

What is the difference between returning a new Stream () with an overridden get method (with the applied function) and just returning a new stream of the new supplier.

image

@DikshantDulal
Copy link

Hmm, i think this works too in this scenario since your get method now points to the new Stream's get() which is now an overridden version of the original abstract class's version and since the concrete class can never be created without a defined get method.

Im kinda curious too on what test cases can be used to differentiate these two methods.

What is the difference between returning a new Stream () with an overridden get method (with the applied function) and just returning a new stream of the new supplier.

image

Hmm, i think this works too in this scenario since your get method now points to the new Stream's get() which is now an overridden version of the original abstract class's version and since the concrete class can never be created without a defined get method.

Im kinda curious too on what test cases can be used to differentiate these two methods.

What is the difference between returning a new Stream () with an overridden get method (with the applied function) and just returning a new stream of the new supplier.

image

Both of these functions have the same input parameters, so I think there isn't any particular test case to check the difference. The only takeaway in my opinion is that both of these are equivalent and can be tested by test cases to verify that .

@calvinseptyanto
Copy link

MyStream map(Function<? super T, ? extends U> mapper){
return new MyStream() {
@OverRide
U get() {
return mapper.apply(MyStream.this.get());
}
}
}

@shotnothing
Copy link

shotnothing commented Apr 27, 2022

@pohja1999 @DikshantDulal

I think they are fundamentally the same code.

Case 1:

<R> MyStream<R> map(Function<T, R> func) { 
	return MyStream.generate(() -> func.apply(this.get())); 
}

Subbing <R> and () -> func.apply(this.get()) into generate()

static <R> MyStream<R> generate(() -> func.apply(this.get())) {
	return new MyStream<R>() {
		R get() {
			return [() -> func.apply(this.get())].get();
		}
	};
}

Focusing on the return and subbing seed.get() into this.get()

return new MyStream<R>() {
	R get() {
		return [() -> func.apply(seed.get())].get();
	}
};

Resolving the supplier .get()

return new MyStream<R>() {
	R get() {
		return func.apply(seed.get());
	}
};

Case 2:

<R> MyStream<R> map(Function<T, R> func) {
	return new MyStream<R>() {
		R get() {
			return func.apply(MyStream.this.get());
		}
	};
}

Focusing on the return and subbing seed.get() into MyStream.this.get()

return new MyStream<R>() {
	R get() {
		return func.apply(seed.get());
	}
}; 
// QED Case 1 == Case 2

I think that they both resolve into MyStream objects with the exact same get(), and that I think there are fundamentally no differences or ways of distinguishing the two.

I think the latter is just a more logically straightforward, possibly(?) less time complex way of generating the solution, while the former is easier to read and understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants