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 Qn4a #587

Open
Zhenyubbx opened this issue Apr 26, 2022 · 5 comments
Open

Mock Qn4a #587

Zhenyubbx opened this issue Apr 26, 2022 · 5 comments

Comments

@Zhenyubbx
Copy link

Hello! Would appreciate if anyone is able to provide the code for Qn 4a of the mock paper! Thank you!

image

@xmlew
Copy link

xmlew commented Apr 26, 2022

Here's some code with my own annotations. Hope this helps :)

void forEach(Consumer<? super T> consumer, int n) {
	MyStream<T> current = this;
        //n iterations 	
	for (int i = 0; i < n; i++) {		
		T value = this.get();
                //applies the transformation using the Consumer
		consumer.accept(value);
                //replaces current value with the newest value provided by the supplier
		current = this.generate(() -> get());
	}
}

@Swagston20
Copy link

void forEach(Consumer con, int n) {
for (int i = 0; i < n; i++) {
con.accept(this.get());
}
}

@bryankwe
Copy link

Here's some code with my own annotations. Hope this helps :)

void forEach(Consumer<? super T> consumer, int n) {
	MyStream<T> current = this;
        //n iterations 	
	for (int i = 0; i < n; i++) {		
		T value = this.get();
                //applies the transformation using the Consumer
		consumer.accept(value);
                //replaces current value with the newest value provided by the supplier
		current = this.generate(() -> get());
	}
}

Hi, thanks for sharing. I don't really understand the last line though..
From the given test case, doesn't it mean that forEach handles the generation of 4 more integers? If we just supply () -> get(), won't it always be the same integer being supplied (rather than a randInt)?
Or am I misunderstanding something here? Thanks!

@xmlew
Copy link

xmlew commented Apr 27, 2022

Here's some code with my own annotations. Hope this helps :)

void forEach(Consumer<? super T> consumer, int n) {
	MyStream<T> current = this;
        //n iterations 	
	for (int i = 0; i < n; i++) {		
		T value = this.get();
                //applies the transformation using the Consumer
		consumer.accept(value);
                //replaces current value with the newest value provided by the supplier
		current = this.generate(() -> get());
	}
}

Hi, thanks for sharing. I don't really understand the last line though.. From the given test case, doesn't it mean that forEach handles the generation of 4 more integers? If we just supply () -> get(), won't it always be the same integer being supplied (rather than a randInt)? Or am I misunderstanding something here? Thanks!

Hi, based on the generate() method provided,
current = this.generate(() -> get());
will obtain the next value based on the supplier, as the value will be the next one every time when seed.get() is called based on lazy evaluation. Hence current will be different every time as seed.get() will be different for every time the method is called (at least in the context given in q4a).

I think @Swagston20's answer is way more elegant haha, but hope this makes things clearer!

@calvinseptyanto
Copy link

void forEach(Consumer consumer, int n){
for (int i = 0; i < n; i++){
consumer.accept(this.get());
}
}

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

5 participants