Skip to content

Instantly share code, notes, and snippets.

@jnape
jnape / FutureEither.scala
Created April 3, 2013 09:03
Making dealing with Future[Either[L, R]] palatable in Scala
package com.thoughtworks.futureeither
import concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import concurrent.duration.FiniteDuration
import java.util.concurrent.TimeUnit
class FutureEither[L, R](private val future: Future[Either[L, R]]) {
def flatMap[R2](block: R => FutureEither[L, R2]): FutureEither[L, R2] = {
public class WreckedCast {
@SuppressWarnings("unchecked")
static <P, PP extends Parametric<P>> PP wreckedCast(Parametric<? extends Parametric<P>> ppp) {
return (PP) ppp.extract();
}
public static void main(String[] args) {
// java.lang.ClassCastException: WreckedCast$Parametric$$Lambda$1/1607521710 cannot be cast to java.lang.String
System.out.println(wreckedCast(Parametric.parametric(Parametric.parametric(1))));
@jnape
jnape / gist:2466342
Created April 22, 2012 19:33
Aggregate List of all ancestors for a particular type in Java, using foldLeft and recursion
package example;
import com.jnape.dynamiccollection.lambda.Accumulator;
import com.jnape.dynamiccollection.list.DynamicList;
import java.util.List;
import static com.jnape.dynamiccollection.DynamicCollectionFactory.list;
public class Example {
import java.io.IOException;
public class ExceptionalExceptionHandling {
public static void main(String[] args) {
ThrowingSupplier<IllegalStateException, String> wellTyped = () -> {
throw new IllegalStateException();
};
ThrowingSupplier<RuntimeException, String> wellTypedButUnnecessary = () -> {
@jnape
jnape / Hylomorphism.java
Created September 3, 2016 20:59
General recursion with Elgot algebras
import com.jnape.palatable.lambda.adt.Either;
import com.jnape.palatable.lambda.functions.Fn3;
import com.jnape.palatable.lambda.functor.Functor;
import com.jnape.palatable.lambda.functor.builtin.Identity;
import java.util.function.Function;
import static com.jnape.palatable.lambda.adt.Either.left;
import static com.jnape.palatable.lambda.adt.Either.right;
@jnape
jnape / TypeUnifailcation.java
Last active March 12, 2016 06:16
Java8 invokeinterface type-unification bug
package example;
import java.util.Optional;
class Foo {}
interface Bar {
String barMethod();
}
public class TypeUnifailcation extends Foo implements Bar {
@jnape
jnape / gist:6329963
Created August 24, 2013 19:27
Suggested TravisCI CSS changes for better scrolling experience
#top:
Change: position: absolute; to position: fixed;
Change: top: -40px; to top: 0;
#left, #right:
Change: position: relative; to position: fixed;
#main:
Remove: position: relative;
Add: margin-left: 381px;
@jnape
jnape / who_does_the_deployment.pro
Created August 6, 2013 05:29
ThoughtWorks P2, Issue 03 Puzzle Solution
% The person who has to deploy to Mobile is not Max or Sam
% Neither Max or Charlie are fans of Copying Files
% The one who deploys quarterly does so to a Mobile target
% Andy deploys weekly
% The person making use of a third party PAAS is not Andy or Charlie
% Sam is either the Copying Files or the System Package enthusiast
% The person who makes VM Images is not Charlie and doesn’t own deploy to a PAAS
% The person who deploys to the PAAS is does so more frequently than the person who deploys to the Desktop
% Of the System Packaging fan and Charlie, one deploys monthly and the other deploys to Mobile
% The person who deploys to the IAAS is not Max
@jnape
jnape / use_mocking.py
Created June 12, 2013 16:42
Python Decorator that automatically unstubs all Mockito stubs after each test case
from unittest2 import TestCase
from mockito import when, unstub
from uuid import UUID
import uuid
def use_mocking(test_suite):
wrapped_tearDown = test_suite.tearDown if 'tearDown' in dir(test_suite) else lambda self: True
def tearDown(self):
@jnape
jnape / Brainfuck.scala
Last active December 15, 2015 20:19
A Brainfuck interpreter written in Scala
package com.thoughtworks.futureeither
import scala.collection.mutable
abstract sealed class LoopOutcome {
def endOrElse(fn: => Any) = this match {
case Repeat() => fn
case _ =>
}