Skip to content

Instantly share code, notes, and snippets.

@mcalavera81
Created October 30, 2015 14:22
Show Gist options
  • Save mcalavera81/00688561a1e97f826a2c to your computer and use it in GitHub Desktop.
Save mcalavera81/00688561a1e97f826a2c to your computer and use it in GitHub Desktop.
Lazy evaluation
package com.example;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.function.Supplier;
public class MessingAround {
public static void main(String[] args) throws IOException, InterruptedException {
logDuration(()->eagerEvaluator(() -> evaluate(1), () -> evaluate(2)));
}
public static void logDuration(Runnable runnable){
Instant start = Instant.now();
runnable.run();
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
System.out.println(String.format("Running for %d sec",duration.getSeconds()));
}
public static void eagerEvaluator(
final Supplier<Boolean> input1, final Supplier<Boolean> input2) {
System.out.println("eagerEvaluator called...");
System.out.println("accept?: " + (input1.get() && input2.get()));
}
public static boolean evaluate(final int value) {
System.out.println("evaluating ..." + value);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
return value > 100;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment