Skip to content

Instantly share code, notes, and snippets.

View johnynek's full-sized avatar

P. Oscar Boykin johnynek

View GitHub Profile
@johnynek
johnynek / stref.scala
Last active November 10, 2015 19:47
mutable variables via a state thread in scala. This is just meant as an illustration of how ST in haskell works, and more generally, how you can implement mutation with immutable APIs.
object STRef {
/**
* Here is a container where we have a single "state thread" = ST.
* When we run this thread of type ST[T] we get a result of type T.
*/
sealed abstract class ST[+T] {
def map[R](fn: T => R): ST[R] = flatMap(t => Const(fn(t)))
def flatMap[R](fn: T => ST[R]): ST[R] = FlatMapped(this, fn)
}
@johnynek
johnynek / vect_filter.idr
Created September 18, 2015 22:15
Filtering a list returns a list that is never bigger
-- If the length is < n+1, it is less than n+2
weakenPair: (p: Fin n ** Vect (finToNat p) a) -> (p1: Fin (S n) ** Vect (finToNat p1) a)
weakenPair (FZ ** v) = (FZ ** v)
weakenPair (FS k ** v) = believe_me (FS k ** v) -- How to prove this branch?
filt : Vect n a -> (fn: a -> Bool) -> (p: Fin (S n) ** Vect (finToNat p) a)
filt [] _ = (0 ** [])
filt (x :: xs) fn with (filt xs fn)
| (p' ** xs') = if (fn x) then ((shift 1 p') ** (x :: xs')) else weakenPair (p' ** xs')

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 / median.scala
Created May 7, 2015 01:50
simple streaming median
import scala.collection.immutable.Queue
object Median {
sealed trait State {
def count: Long
def add(item: Long): State
def median: Long
}
case class Start(window: Queue[Long], max: Int, countInt: Int) extends State {
def count = countInt
@johnynek
johnynek / expressions.scala
Last active August 29, 2015 14:19
Lambda Expressions for constrained languages
object exp {
case class Scope(m: Map[Var[_], Exp[_]]) {
def put[T](pair: (Var[T], Exp[T])): Scope = new Scope(m + pair)
def apply[T](v: Var[T]): Either[String, Exp[T]] = m.get(v) match {
case Some(exp) => Right(exp.asInstanceOf[Exp[T]])
case None => Left(s"$v not found. Scope: $m")
}
}
@johnynek
johnynek / error.rs
Created February 24, 2015 07:31
rust compiler error
use std::old_io as io;
use std::sync::mpsc::channel;
use std::thread::Thread;
use std::ops::Fn;
use std::marker::PhantomData;
pub struct Test<F, X>(F, PhantomData<X>);
impl <F, X> Test<F, X> where F: Fn(X) {
fn go(self, x: X) {
@johnynek
johnynek / AliceInAggregatorLand.scala
Last active January 24, 2024 19:38
A REPL Example of using Aggregators in scala
/**
* To get started:
* git clone https://github.com/twitter/algebird
* cd algebird
* ./sbt algebird-core/console
*/
/**
* Let's get some data. Here is Alice in Wonderland, line by line
*/
@johnynek
johnynek / scanlefttrav1.scala
Created December 22, 2014 20:12
Why doesn't scala give scanLeft on TraversableOnce?
/**
* This won't work because of the "situation" with scala collections. TraversableOnce actually
* needs like 12 methods implemented.
*/
class ScanLeft[A, B](it: TraversableOnce[A], init: B, fn: (B, A) => B) extends TraversableOnce[B] {
override def foreach[U](effect: B => U): Unit = {
var bstate = init
effect(bstate)
it.foreach { a: A =>
bstate = fn(bstate, a)
@johnynek
johnynek / dependent_type_question.scala
Last active August 29, 2015 14:11
type inference with cases. Why does this fail in scala 2.10.4? Is my logic wrong, or is the compiler not smart enough?
sealed trait Key {
type Inner
}
trait IntKey extends Key {
type Inner = Int
}
trait StringKey extends Key {
type Inner = String
}
@johnynek
johnynek / bug.scala
Last active August 29, 2015 14:11
Bug with dependent types in scala 2.10
trait Path {
type A
}
trait Reader { self =>
def read(p: Path): Option[p.A]
def orElse(that: Reader): Reader = ComposedReader(this, that)
}