Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View marccarre's full-sized avatar

Marc Carré marccarre

View GitHub Profile
@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
}
@marccarre
marccarre / SynchronousVsAsynchronous.java
Last active May 19, 2016 03:50
Compare synchronous function calls with asynchronous promises using CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.SECONDS;
public class SynchronousVsAsynchronous {
public static void main(final String[] args) {
stopwatch(() -> {
// Synchronous:
int x1 = compute(1);
@marccarre
marccarre / AsynchronousExceptions.java
Created May 8, 2016 16:58
What happens when a CompletableFuture throws an exception
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class AsynchronousExceptions {
public static void main(final String[] args) throws InterruptedException {
for (final boolean failure : new boolean[]{false, true}) {
CompletableFuture<Integer> x = CompletableFuture.supplyAsync(() -> {
if (failure) {
throw new RuntimeException("Oops, something went wrong");
@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 / 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 / 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 / AsynchronousSumAndMax.java
Last active May 19, 2016 03:53
Asynchronously synchronise and aggregate intermediate results
package com.marccarre;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static java.util.concurrent.CompletableFuture.completedFuture;
@marccarre
marccarre / AsynchronousLongComputeVsSlowStore.java
Last active July 19, 2016 20:55
Asynchronously compute or load from cache
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.SECONDS;
public class AsynchronousLongComputeVsSlowStore {
private static final ConcurrentMap<Integer, Integer> store = new ConcurrentHashMap<>();
@marccarre
marccarre / keybase.md
Created March 25, 2017 09:23
keybase.md

Keybase proof

I hereby claim:

  • I am marccarre on github.
  • I am marccarre (https://keybase.io/marccarre) on keybase.
  • I have a public key whose fingerprint is E783 6CDE 2E42 F8FE 3847 0FBF 0626 58EF F69B 8B32

To claim this, I am signing this object:

@marccarre
marccarre / heap.py
Created January 28, 2020 02:03
Min and max heaps in Python
from __future__ import annotations # To allow "MinHeap.push -> MinHeap:"
from typing import Generic, List, Optional, TypeVar
from heapq import heapify, heappop, heappush, heapreplace
T = TypeVar('T')
class MinHeap(Generic[T]):
'''