Skip to content

Instantly share code, notes, and snippets.

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))));
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 / BiFunctor.java
Last active August 29, 2015 14:20
Continuation-like iterable type
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 December 15, 2014 19:21
Haskell's missing core type classes
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 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 _ =>
}