Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile

Better Dependency Hygiene With Private Dependencies On JVM

A common pain when working with large projects is the diamond dependency. Consider a commonly used library such as ASM. One wants to build a big application reusing many powerful libraries, but unfortunately many of my desired dependencies themselves depend on different and incompatible versions of ASM. While compiling my code, since ASM does not appear in any APIs I touch, everything compiles fine, but at runtime the JVM only includes one version of classes of a given name leading to runtime binary errors.

OSGI Bundles are related to solving this problem, but it appears that is a heavy solution that has proven to be too cumbersome to actually use. Here we propose a lighter weight approach that benefits each incremental project that adopts this method.

Private dependencies are implemented by a build tool plug-in. In the build where one declares dependencies, one can label a jar dependency to be a private dependency. A private dependency means tha

@johnynek
johnynek / 0_reuse_code.js
Created April 2, 2014 22:58
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
// Run this with scala <filename>
/*
* Two-phase commit Monad
*/
trait Transaction[+T] {
def map[U](fn: T => U): Transaction[U] = flatMap { t => Constant(fn(t)) }
def flatMap[U](fn: T => Transaction[U]): Transaction[U] =
FlatMapped(this, fn)
// In a real system, this should be wrapped in a Future
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
/**
* You should be able to run this file if you have scala installed:
* either make it executable, or run it with: "scala MapReduceToy.scala < someInputFile.txt"
*/
// Toy Map Reduce framework:
class MapReduce[T,K,V,R](flatMapFn: (T) => Iterable[(K,V)], reduceFn: ((K,Iterable[V])) => R) {
@johnynek
johnynek / test.scala
Last active December 12, 2015 12:39 — forked from sritchie/test.scala
import annotation.implicitNotFound
@implicitNotFound(msg = "This message can never appear!")
trait ~>[A, B] { self =>
def apply(a: A): B
def invert(b: B): A = inverse.apply(b)
def inverse: B ~> A = new ~>[B,A] {
def apply(b: B) = self.invert(b)
override def invert(a: A) = self(a)
}
// Adds an item to the buffer and returns None, or fills the buffer and returns the sum
@tailrec
final def offer(item: V, acc: Option[V] = None): Option[V] =
if(!queue.offer(item)) {
// Queue is full
val toSum = ListBuffer[V]()
queue.drainTo(toSum.asJava)
// Add everything up and get the new acc:
val newAcc = Monoid.plus(acc, Some(Monoid.sum(toSum)))
// We never actually got the item in: