Skip to content

Instantly share code, notes, and snippets.

@wheaties
wheaties / TypePredicate2
Created May 29, 2012 00:29
Type Predicates that just ain't working...
sealed trait TypeBoolean
sealed trait True extends TypeBoolean
sealed trait False extends TypeBoolean
trait Conditional{
type Result <: TypeBoolean
}
abstract class TypePredicate2[A,B,Cond[_,_] <: Conditional,Implicit[_,_]] extends Conditional{
implicit def pred[Child,Parent](implicit ev:Implicit[Child,Parent]):Cond[Child,Parent]
@wheaties
wheaties / Effects
Last active December 10, 2015 19:48 — forked from anonymous/gist:4484300
An effect which can be "undone."
trait Change[+A]{
def map[B](f: A => B): Change[B]
def flatMap[B](f: A => Change[B]): Change[B]
def foreach(f: A => Unit): Unit
def filter(pred: A => Boolean): Change[A]
}
case class Reversible[+A](value: A, undo: List[Undo]) extends Change[A]{
def map[B](f: A => B) = Reversible(f(value), undo)
def flatMap[B](f: A => Change[B]) = f(value) match{
@wheaties
wheaties / Mapper.scala
Last active December 12, 2015 04:48
Working out some ideas.
trait Mapper[Elem,C[_]]{
def onTrue(t: Elem => Elem): FilterTraversable[Elem,C]
def onFalse(f: Elem => Elem): FilterTraversable[Elem,C]
def on[A](f: Elem => Elem, t: Elem => Elem): FilterTraversable[Elem,C]
def apply(collection: C[Elem], condition: Elem => Boolean): C[Elem]
}
case class MapperIterable[Elem,I[_] <: Iterabable[_]](t: Elem => Elem, f: Elem => Elem) extends Mapper[Elem,I]{
def onTrue(func: Elem => Elem) = copy(t = func)
@wheaties
wheaties / query.scala
Last active December 16, 2015 21:28
Arbitrary Query object
trait Query[-A,+B,Cont[_] <: Iterable[_]] extends (A => Try[Cont[B]]){
self =>
def apply(arg0: A) = Try(unsafe(arg0))
protected def unsafe(arg0: A): Cont[B]
def map[C](f: B => C) = new Query[A,C,Cont]{
def unsafe(arg0: A) = self unsafe arg0 map f
}
@wheaties
wheaties / gist:5762459
Created June 12, 2013 02:37
A shapeless Predicate?
import shapeless._
import Poly._
import TypeOperators._
trait PredF extends (Id ~>> Boolean){
self =>
def apply[T](arg: T): Boolean
def or(that: Id ~>> Boolean) = new PredF{
@wheaties
wheaties / Cache.scala
Created July 24, 2013 19:10
Cache.scala (this time public)
asdf//In reality, this is all we need, no?
//See https://github.com/jsr107/jsr107spec
trait Cache[Key,Value]{
def get(key: Key): Option[Key]
def getOrElse(key: Key, value: Value) = get(key) getOrElse value
def set(key: Key, value: Value): Value
def contains(key: Key): Boolean
def replace(key: Key, value: Value): Option[Value] ={
val previous = get(key)
set(key, value)
@wheaties
wheaties / tree.scala
Created September 16, 2013 15:56
Some Tree Ideas
//Haven't even checked if this compiles
//Just an idea
trait TreeLike[A, This <: IterableLike[TreeLike[A, This], This]]{
def root: A
def children: This
def map[B, That](f: A => B): TreeLike[A, That] = over(this, f).run
protected def over[B, That](tree: TreeLike[A, This], f: A => B): Trampoline[TreeLike[B, That]] =
@wheaties
wheaties / Tree.scala
Last active December 23, 2015 09:19
Scalaz Trampoline 7.0.2 on 2.10.2 fail...
case class CTree[@specialized(Double) Value](root: Value,
children: Map[Int, ComputedTree[Value]] = Map.empty[Int, CTree[Value]]){
def map[A](f: Value => A): ComputedTree[A] = walk(this, f).run
protected def walk[A](tree: CTree[Value], f: Value => A): Trampoline[CTree[A]] = tree match {
case CTree(value, childs) if childs.isEmpty => done(CTree(f(value)))
case CTree(value, childs) => step(childs.toList, f) map (CTree(f(value), _))
}
@wheaties
wheaties / bb-bullet.js
Created October 29, 2013 03:03
Backbone-Nvd3 Example
var Bullet = {
orient: function(arg){
this.chart = this.chart.orient(arg);
return this;
},
ranges: function(arg){
this.chart = this.chart.ranges(arg);
return this;
},
markers: function(arg){
@wheaties
wheaties / deferedTemplate.js
Created November 2, 2013 17:08
Handlebars Defered Template w/ Backbone example use case
//Assumes jQuery, produces a lazy load template function.
var Template = function(el){
var $el = $(el),
template = undefined;
//Compile template and store once.
return function(args){
if(!template){
template = Handlebars.compile($el.html());
}