Skip to content

Instantly share code, notes, and snippets.

View marccarre's full-sized avatar

Marc Carré marccarre

View GitHub Profile
@marccarre
marccarre / AsynchronousExceptionsHandlingWithWhenComplete.java
Created May 15, 2016 12:53
Asynchronously handle and propagate the exception
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class AsynchronousExceptionsHandlingWithWhenComplete {
public static void main(final String[] args) throws InterruptedException, ExecutionException {
for (final boolean failure : new boolean[]{false, true}) {
CompletableFuture<Integer> x = CompletableFuture.supplyAsync(() -> {
if (failure) {
throw new RuntimeException("Oops, something went wrong");
@marccarre
marccarre / AsynchronousExceptionsHandlingWithHandle.java
Created May 8, 2016 17:59
Asynchronously handle exception and return a transformed future
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static java.lang.String.format;
public class AsynchronousExceptionsHandlingWithHandle {
public static void main(final String[] args) throws InterruptedException, ExecutionException {
for (final boolean failure : new boolean[]{false, true}) {
CompletableFuture<Integer> x = CompletableFuture.supplyAsync(() -> {
if (failure) {
@marccarre
marccarre / AsynchronousExceptionsHandlingWithExceptionally.java
Created May 8, 2016 17:39
Asynchronously handle exception and return a default or error value
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class AsynchronousExceptionsHandlingWithExceptionally {
public static void main(final String[] args) throws InterruptedException, ExecutionException {
for (final boolean failure : new boolean[]{false, true}) {
CompletableFuture<Integer> x = CompletableFuture.supplyAsync(() -> {
if (failure) {
throw new RuntimeException("Oops, something went wrong");
@marccarre
marccarre / Lottery.scala
Created December 27, 2014 10:59
Scala - Winning combinations at the lottery
import scala.util.Random
def winningCombinationsFor(max: Int = 49, numDrawn: Int = 6, minNumToWin: Int = 3): Seq[Set[Int]] = {
val (draw, remainder) = Random.shuffle(1 to max).splitAt(numDrawn)
for {
numPickedFromDraw <- minNumToWin to numDrawn
subsetFromDraw <- draw.toSet.subsets(numPickedFromDraw)
subsetFromRemainder <- remainder.toSet.subsets(numDrawn - numPickedFromDraw)
} yield subsetFromDraw ++ subsetFromRemainder
}