Skip to content

Instantly share code, notes, and snippets.

View drdozer's full-sized avatar

Matthew Pocock drdozer

  • Newcastle upon Tyne, England
View GitHub Profile
@drdozer
drdozer / gist:1246425
Created September 27, 2011 22:19
A lazily sorted vector
import scala.collection.GenSeqLike
import scala.collection.mutable.ArraySeq
/**
* A lazily sorted vector.
*
* Some operations may force some sorting, so return a LazySortedVector as well as the expected result.
*/
sealed trait LazySortedVector[E] {
/** Vector length. */
@drdozer
drdozer / MBiMap.scala
Created December 24, 2011 12:34
Exploration of Map data-structures
import java.util.NoSuchElementException
trait MBiMap[K, V] {
def insert(k: K, v: V): MBiMap[K, V]
def remove(k: K): MBiMap[K, V]
def lookup(k: K): V
}
object MBiMap {
@drdozer
drdozer / Graph.scala
Created December 28, 2011 10:27
Simplified Graph trait
trait Graph[V, E] {
type Col[A]
type Incidence[A]
def vertices: Col[V]
def edges: Col[E]
def incidenceOption(e: E): Incidence[V]
}
@drdozer
drdozer / GraphPipe.scala
Created January 24, 2012 16:59
Graphvizs examples
import uk.co.turingatemyhamster.graphvizs.exec
val gOut = dot2dotG(Graph(false, GraphType.Digraph, Some("g"),
EdgeStatement("a1") -> "a2" -> "a3" -> "a4",
EdgeStatement("a1") -> "a4" ))
@drdozer
drdozer / VolumeAdjuster.scala
Created March 22, 2012 21:06
Implicits example
/**
* Adjust the volume on something that produces noise.
*/
@annotation.implicitNotFound("Could not work out how to adjust the volume for a ${A}.")
trait VolumeAdjuster[A] {
def makeLouder(a: A): Unit
def makeQuieter(a: A): Unit
}
case class TV()
@drdozer
drdozer / Mixins.scala
Created April 5, 2012 14:29
ultra-lightweight reactive swing
/** A mixin that exposes the text and document of any Swing component using the Document abstraction.
The text updates when the input is accepted by the component. The document text reflects what is typed. */
trait TextVar {
/** Things that must be provided by any class that this is mixed into. */
self : {
def getText(): String
def setText(s: String): Unit
def getDocument(): Document
def addActionListener(al: ActionListener): Unit
} =>
@drdozer
drdozer / cnat.scala
Created August 22, 2012 16:00
type-encoding of natural numbers
trait CNat {
type succ <: CNat
type pred <: CNat
}
trait _0 extends CNat {
type pred = _0
type succ = CSucc[_0]
}
package sbol.device
import java.net.URI
import sbol.util.{Annotated, Described, Identified}
/**
* A Device is the unit of functional design composition.
*
* @author Matthew Pocock
*/
@drdozer
drdozer / SerializableSafe.scala
Last active December 10, 2015 15:08
Scala witness for serializable data
/** Witness that a type is safe to serialize. */
@implicitNotFound(msg="Could not show ${S} to be serialization-safe")
trait SerializationSafe[S]
/** Serialization-safe witnesses. */
object SerializationSafe {
/** If you are serializable, you are serialization-safe. */
implicit def serializableIsSafe[S <: Serializable](s: S) = new SerializationSafe[S] {}
@drdozer
drdozer / 1_sbol_core_current.scala
Last active December 17, 2015 16:09
SBOL data model
// some types so that we know cardinalities
type Optional[T] // 0..1
type Required[T] // 1
type Many[T] // 0..*
// a value range restriction
type PositiveInteger // x: Int => x > 0
// a whole load of DNA components grouped for whatever reason
trait Collection {