Skip to content

Instantly share code, notes, and snippets.

View WreckedCast.java
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))));
View ExceptionalExceptionHandling.java
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 Sep 3, 2016
General recursion with Elgot algebras
View Hylomorphism.java
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 Mar 12, 2016
Java8 invokeinterface type-unification bug
View TypeUnifailcation.java
package example;
import java.util.Optional;
class Foo {}
interface Bar {
String barMethod();
}
public class TypeUnifailcation extends Foo implements Bar {
@jnape
jnape / BiFunctor.java
Last active Aug 29, 2015
Continuation-like iterable type
View BiFunctor.java
package com.jnape.palatable.lambda.applicative;
import com.jnape.palatable.lambda.functions.MonadicFunction;
import static com.jnape.palatable.lambda.functions.builtin.monadic.Identity.id;
@FunctionalInterface
public interface BiFunctor<A, B> {
default <C> BiFunctor<C, B> biMapL(MonadicFunction<? super A, ? extends C> fn) {
@jnape
jnape / List.hs
Created Dec 15, 2014
Haskell's missing core type classes
View List.hs
module MissingTypeClassesIntegration.List where
import MissingTypeClasses
import Prelude hiding (foldl, foldl1, foldr, foldr1, map, filter, zip)
instance RecursiveDataStructure [] where
unit = []
cons = (:)
instance LeftFoldable [] where
@jnape
jnape / gist:6329963
Created Aug 24, 2013
Suggested TravisCI CSS changes for better scrolling experience
View gist:6329963
#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 Aug 6, 2013
ThoughtWorks P2, Issue 03 Puzzle Solution
View who_does_the_deployment.pro
% 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 Jun 12, 2013
Python Decorator that automatically unstubs all Mockito stubs after each test case
View use_mocking.py
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 Dec 15, 2015
A Brainfuck interpreter written in Scala
View Brainfuck.scala
package com.thoughtworks.futureeither
import scala.collection.mutable
abstract sealed class LoopOutcome {
def endOrElse(fn: => Any) = this match {
case Repeat() => fn
case _ =>
}