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
@danieldietrich
danieldietrich / LazyHashCodeExample.md
Last active August 29, 2015 14:26
Lazy evaluation to the rescue!

Time consuming hashCode calculation (of immutable objects)?

These objects are used in a HashMap?

No problem!

class MyClass {

 // may also be Lazy.of(this::timeConsumingOperation)
@danieldietrich
danieldietrich / RedBlackTree.java
Created August 5, 2015 19:55
Look ma, generic method reference!
public interface RedBlackTree<T> {
static <T extends Comparable<T>> EmptyNode<T> empty() {
// HERE:
return new EmptyNode<>(T::compareTo);
}
class EmptyNode<T> implements RedBlackTree<T> {
EmptyNode(Comparator<? super T> comparator) {
@danieldietrich
danieldietrich / FunctionalQueueImpl.java
Last active August 29, 2015 14:26
Imperative vs. Functional Queue.groupBy() implementation with Javaslang collections
class FunctionalQueueImpl<T> implements Queue<T> {
// ...
@Override
public <C> Map<C, Queue<T>> groupBy(Function<? super T, ? extends C> classifier) {
return foldLeft(HashMap.empty(), (map, t) -> {
final C key = classifier.apply(t);
final Queue<T> queue = map.getOption(key).orElse(Queue.empty());
return map.put(key, queue.enqueue(t));
@danieldietrich
danieldietrich / Account.scala
Created March 10, 2012 23:35
Play w/ Scala: Url Path Binding
package models
import play.api.mvc.PathBindable
object Account extends Enumeration {
val Company, Sales = Value // create enumerated values
// url path binding (routes)
implicit def bindableAccount = new PathBindable[Account.Value] {
@danieldietrich
danieldietrich / Account.scala
Created March 11, 2012 22:13
Url Path Binding w/o Boilerplate
package models
object Account extends BindableEnum {
val Company, Sales = Value // create enumerated values
}
@danieldietrich
danieldietrich / CachedLdapPermissionResolver
Created March 20, 2012 21:48
Howto abstract an LDAP layer to resolve permissions
/**
* PLEASE AVOID THIS IMPLEMENTATION BECAUSE OF...
* - a cache should be garbage collectable (on low memory)
* (see Soft Reference, Weak Reference etc.)
* - a cache should be thread safe (this is not)
*/
class CachedLdapPermissionResolver extends LdapPermissionResolver {
private static final long MAX_TIME = 1000 * 60 * 10; // = 10 min.
@danieldietrich
danieldietrich / PeekSomeValues.java
Created October 2, 2015 21:24
Need for Kestrels / k-combinators
import javaslang.Lazy;
import javaslang.Value;
import javaslang.control.Try;
class PeekSomeValues {{
Seq<Value<String>> values = List.of(
// a lazy value
Lazy.of(() -> "test"),
@danieldietrich
danieldietrich / 1_Sample.java
Created October 5, 2012 08:39
Extension Methods in Java, Xtend and Scala
/* JAVA */
public class SampleExtensions {
public static String sort(String s) {
final char[] arr = s.toCharArray();
java.util.Arrays.sort(arr);
return String.valueOf(arr);
}
@danieldietrich
danieldietrich / Future.java
Last active October 28, 2015 23:37
Essence of Future
/* / \____ _ _ ____ ______ / \ ____ __ _ _____
* / / \/ \ / \/ \ / /\__\/ // \/ \ / / _ \ Javaslang
* _/ / /\ \ \/ / /\ \\__\\ \ // /\ \ /\\/ \__/ / Copyright 2014-now Daniel Dietrich
* /___/\_/ \_/\____/\_/ \_/\__\/__/___\_/ \_// \__/_____/ Licensed under the Apache License, Version 2.0
*/
package javaslang.concurrent;
import javaslang.collection.Queue;
import javaslang.control.None;
import javaslang.control.Option;
@danieldietrich
danieldietrich / JEP269.md
Last active November 7, 2015 18:19
JEP 269: Interesting corner cases

Status quo

Calling factory methods of same name for single and multiple elements may raise ambiguities problems. This is the case for the actual java.util.stream.Stream#of(T) and #of(T...) API.

These are the relevant cases (compiling with -Xlint:all -Werror):

1. It is hard to create a Stream<T[]>

Stream.of(arrayOfObj) calls Stream.of(T...) instead of Stream.of(T).