Navigation Menu

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
import static java.lang.System.*;
import java.util.function.BiFunction;
import java.util.function.Function;
// Implementation of a pseudo-GADT in Java, translating the examples from
// http://www.cs.ox.ac.uk/ralf.hinze/publications/With.pdf
// The technique presented below is, in fact, just an encoding of a normal Algebraic Data Type
// using a variation of the visitor pattern + the application of the Yoneda lemma to make it
// isomorphic to the targeted 'GADT'.
sealed trait TreeLike[M[_]] {
def node[A](f: M[A], g: M[A]): M[A]
}
sealed trait Tree[+A] { self =>
def flatMap[B](f: A => Tree[B]): Tree[B] = self match {
case Leaf(a) => f(a)
case Node(l, r) => Node(l.flatMap(f), r.flatMap(f))
}
}
@danieldietrich
danieldietrich / Monad.java
Last active August 29, 2015 14:21
Higher-Kinded Types
interface Monad<T> {
<U> Monad<U> flatMap(Function<? super T, ? extends Monad<U>> mapper);
}
interface Tree<T> extends Monad<T> {
@Override
<U> Tree<U> flatMap(Function<? super T, ? extends Tree<U>> mapper);
}
interface BinaryTree<T> extends Tree<T> {
@danieldietrich
danieldietrich / Match2.java
Created May 14, 2015 22:22
Match.of(value).when(function).get()
/* / \____ _ ______ _____ / \____ ____ _____
* / \__ \/ \ / \__ \ / __// \__ \ / \/ __ \ Javaslang
* _/ // _\ \ \/ / _\ \\_ \/ // _\ \ /\ \__/ / Copyright 2014-2015 Daniel Dietrich
* /___/ \_____/\____/\_____/____/\___\_____/_/ \_/____/ Licensed under the Apache License, Version 2.0
*/
package javaslang.control;
import javaslang.Function1;
import java.lang.invoke.MethodType;
@danieldietrich
danieldietrich / Fix.java
Last active August 29, 2015 14:25
What the lambda? #WTL
static class JavaslangGoodies {
static <T, R> MethodType getType(SerializableFunction<T, R> function) throws Exception {
// FIXED, DON'T CHECK FOR CAPTURED LAMBDA PARAMETERS
return getLambdaSignature(function);
}
static SerializedLambda getSerializedLambda(Serializable lambda) throws Exception {
final Method method = lambda.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(true);
@danieldietrich
danieldietrich / BinaryTree.java
Last active August 29, 2015 14:25
Checks if a binary tree is a binary search tree
class BinaryTree<T> extends Tree<T> {
// ...
default boolean isBinarySearchTree() {
class MinMax<U extends Comparable<Object>> {
final U min;
final U max;
/**
* This is a Java class using Javaslang's Lazy
*/
class LazyDemo {{
// -- Lazy instance
Lazy<String> value = Lazy.of(() -> "Yay!");
value.toString(); // = Lazy(?)
@danieldietrich
danieldietrich / SerializableLambda.md
Last active August 29, 2015 14:26
What the Serializable!? #WTS

As stated by Oracle,

You can serialize a lambda expression if its target type and its captured arguments are serializable. However, like inner classes, the serialization of lambda expressions is strongly discouraged.

Interesting that the following works:

import java.util.Optional;

public class Test {
@danieldietrich
danieldietrich / ScalaVsJavaslang.md
Last active August 29, 2015 14:26
Functional Extension for Java

If you are willing to use Javaslang, Java 8 will look more like Scala.

Scala

val filteredList =
  stringsToFilterList.filter(s => s.nonEmpty && s.startsWith("somePredicate"))

Javaslang

@danieldietrich
danieldietrich / ExampleWithJavaslang.java
Last active August 29, 2015 14:26
Pattern Matching for Java
public static void main(final String... args) {
// this is javaslang.collection.Stream
Stream.of(0, 1, 2, 3, 13, 14, null, -1)
.peek(n -> out.print(format("%d -> ", n)))
.map(Match.as(Object.class) // match as function
.when(Objects::isNull).then("!")
.whenIs(0).then(nil())
.when(is(1)).then("one")
.whenIs(13).then(() -> "unlucky")
.whenIs(14).then(printIt())