Skip to content

Instantly share code, notes, and snippets.

View danieldietrich's full-sized avatar
💭
📡 working from space

Daniel Dietrich danieldietrich

💭
📡 working from space
View GitHub Profile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Javaslang 1.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-release-plugin:2.4.2:perform (default-cli) @ javaslang ---
[INFO] Checking out the project to perform the release ...
[INFO] Executing: /bin/sh -c cd /Users/daniel/git/rocketscience-projects/javaslang/target && git clone --branch javaslang-1.0.0 git@github.com:rocketscience-projects/javaslang.git /Users/daniel/git/rocketscience-projects/javaslang/target/checkout
[INFO] Working directory: /Users/daniel/git/rocketscience-projects/javaslang/target
@danieldietrich
danieldietrich / NestedFunWithLambda.java
Last active August 29, 2015 14:04
Java Nested Functions
interface NestedFunWithLambda {
default String spaces(int count) {
final BiFunction<String, Integer, String> spaces =
// compiler complains that spaces may not have been initialized
(s, i) -> (i < 1) ? s : spaces.apply(s + " ", i - 1);
return spaces.apply("", count);
}
/**
* Exits the JVM using {@code Runtime.getRuntime().exit(status)} (which is equivalent to
* {@code System.exit(0)}). If something goes wrong while running the finalizers and shutdown
* hooks, or the timeout is reached, the JVM is forced to be terminated by calling
* {@code Runtime.getRuntime().halt(status)}.
*
* @param status the exit status, zero for OK, non-zero for error
* @param timeout The maximum delay in milliseconds before calling
* {@code Runtime.getRuntime().halt(status)}.
*
@danieldietrich
danieldietrich / Assertions.java
Last active August 29, 2015 14:04
Assert that a block of code is throwing
public final class Assertions {
/**
* This class is not intended to be instantiated.
*/
private Assertions() {
throw new AssertionError(Assertions.class.getName() + " cannot be instantiated.");
}
public static RunnableAssert assertThat(Runnable test) {
@danieldietrich
danieldietrich / Example
Last active August 29, 2015 14:06
Antlr Parse Result Printer
$ alias antlr=./antlr.sh
$ antlr Issue118 start
Please enter input to be parsed and press <Ctrl>-D when finished.
a 1 b
<Ctrl>-D
(start a 1 b)
$ echo "a 1 b" > test.txt
$ antlr Issue118 start test.txt
@danieldietrich
danieldietrich / Iterators.java
Last active August 29, 2015 14:08
Use javaslang.Option instead of java.util.Optional if null is permitted
/** / \____ _ ______ _____ / \____ ____ _____
* / \__ \/ \ / \__ \ / __// \__ \ / \/ __ \ Javaslang
* _/ // _\ \ \/ / _\ \\_ \/ // _\ \ /\ \__/ / Copyright 2014 Daniel Dietrich
* /___/ \_____/\____/\_____/____/\___\_____/_/ \_/____/ Licensed under the Apache License, Version 2.0
*/
package javaslang.collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Predicate;
@danieldietrich
danieldietrich / Monad.java
Last active August 29, 2015 14:08
The most sophisticated generic Java type I ever wrote
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* The Monad interface.
*
* @param <M> The type of the monad, e.g. {@code class Foo<A> extends Monad<Foo<?>, A, Foo<A>>}.
* @param <A> Component type of {@code M}.
*/
@danieldietrich
danieldietrich / Example.java
Last active August 29, 2015 14:10
Java Shared Types
/*
* Java Quest
* ==========
*
* Write a method in two different contexts (read: Types)
* which takes a function which returns an object of the current type.
*
* T1<A> { <B> T1<B> m(f: A -> T1<B>); }
* T2<A> { <B> T2<B> m(f: A -> T2<B>); }
*
package labs;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
@danieldietrich
danieldietrich / StableDate.java
Last active August 29, 2015 14:16
A Date which is stable regarding hashCode, equals and compareTo
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* A UTC date, consisting of a year, a month and a day component. Compared to {@linkplain java.util.Date}
* a StableDate is stable regarding {@linkplain java.lang.Object#equals} and {@linkplain java.lang.Object#hashCode}.
*/
public class StableDate implements Comparable<StableDate>, Serializable {