Skip to content

Instantly share code, notes, and snippets.

View Bill's full-sized avatar

Bill Burcham Bill

View GitHub Profile
@Bill
Bill / VirtualTimeSchedulerInaccurate.java
Last active July 5, 2019 17:55
Project Reactor VirtualTimeScheduler subclass that introduces (controlled) error into schedule times in order to cause changes to task interleaving.
package com.thoughtpropulsion.reactrode;
import static com.thoughtpropulsion.reactrode.Functional.returning;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import reactor.core.Disposable;
@Bill
Bill / ConnectableFluxTest.java
Last active July 5, 2019 17:50
Test Project Reactor ConnectableFlux. State space exploration brought to you by `VirtualTimeSchedulerInaccurate` and changing `new Random(1)` to `new Random()`.
package com.thoughtpropulsion.reactrode.rxperiment;
import static com.thoughtpropulsion.reactrode.Functional.returning;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.nanoTime;
import static org.assertj.core.api.Assertions.assertThat;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.Duration;
@Bill
Bill / MonoidMenagerie.java
Last active June 19, 2019 13:31
Monoid Menagerie, or, the uncanny usefulness of four lines of Java
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/*
@Bill
Bill / scratch_38.java
Created June 17, 2019 22:34
I don't think the changes to genericize Execution does what we want https://github.com/apache/geode/pull/3715
import java.math.BigInteger;
import java.util.Collections;
import org.apache.geode.cache.execute.Execution;
import org.apache.geode.cache.execute.FunctionService;
class Scratch {
public static void main(String[] args) {
final Execution<String, String, String> execution = FunctionService.onRegion(null);
import static net.jodah.typetools.TypeResolver.resolveRawArgument;
public class Core {
interface Handler<T> {
void process(T x);
}
void p1(final String s) {}
void p1(final int i) {}
@Bill
Bill / Memoize.java
Last active May 24, 2019 21:17
A couple memoization functions for Java Suppliers.
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
class Memoize {
private Memoize() {}
/*
Props to AOL Cyclops Memoize for this little trick: using the UNSET reference
value as the sentinel for an uninitialized cache value. This approach allows memoization
@Bill
Bill / EnhancedForLoopOverMapValues.java
Created May 24, 2019 16:50
Using JSR 201 enhanced for loop to iterate over Map values
import java.util.HashMap;
import java.util.Map;
class Scratch {
interface Pool {
void basicDestroy(boolean keepAlive);
}
static class PoolImpl implements Pool {
@Override
public void basicDestroy(final boolean keepAlive) {
@Bill
Bill / DoesOptionalObeyFunctorLaws.java
Last active May 1, 2019 18:39
Optional, introduced along with Stream and Function, in Java 8, has a map() method. Does it obey the functor laws, particularly the one pertaining to function composition?
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Scratch {
/*
A method that converts a String to an Integer.
@Bill
Bill / flatMapNulls.java
Created April 24, 2019 22:32
Use flatMap() to elide nulls in a Stream
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Scratch {
public static void main(String[] args) {
final List<String> original =
Stream.of("a", "b", null, "c")
.collect(Collectors.toList());
@Bill
Bill / EnumSetFormat.java
Created April 4, 2019 22:28
Creates an EnumSet with a couple elements and prints it in String and OutputStream format.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.EnumSet;
class Scratch {
enum Fruit {APPLE, ORANGE};
public static void main(String[] args) throws IOException {
final EnumSet<Fruit> fruits = EnumSet.of(Fruit.APPLE, Fruit.ORANGE);