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 / RowVectorPhantom.sc
Last active April 10, 2017 07:42
size-safe vector
// A row vector of Ts, with the number of rows encoded as a type.
// Key invariants on methods are encoded through types and witnesses.
@tinvariant[R >= 0)]
trait RowVectorPhantom[T, R <: XInt] {
def apply[I]
(implicit i: ValueOf[I])
(implicit iIsPositive: I >= 0, iIsWithinRange: I < R): T
def rows(implicit r: ValueOf[R]): Int = r.value
}
// Sketches of various meta-encodings of monoids
// Naive typeclass trait
{
trait Monoid[M] {
def zero: M
def append(m1: M, m2: M): M
}
implicit object DoubleSumMonoid extends Monoid[Double] {
import scala.reflect.ClassTag
trait MyCollection[T]
{
def ping(c2: MyCollection[T]): Unit
def pong[C1 <: MyCollection[T] : ClassTag](c1: C1): Unit
def pung[C1 <: MyCollection[T], C2 <: MyCollection[T]](c1: C1, c2: C2)(implicit C1: ClassTag[C1], C2: ClassTag[C2]): Unit
}
class AListyCollection[T] extends MyCollection[T]
/* This models a door.
Doors can be opened and closed, and can also be locked and unlocked.
However, a door can only be opened and closed when locked, and can only be locked and unlocked when closed.
The exercise is to write a fully type-safe, tagless API for this that only exposes legal moves, and where the door,
open/closed and locked/unlocked implementations can potentially be freely composed.
*/
/* We start with a door.
@drdozer
drdozer / binnedHistogram.scala
Created November 29, 2016 11:47
Sketch of some ideas for data plotting DSL
// build n histograms as (n, histogram) pairs
val histograms = for {
n <- 1 to N
} yield (
n,
histogram(
dataPoints = for
{
t <- (min of time from tradeHistory) to (max of time from tradeHistory - n)
} yield price of tradeHistory(t + n) - price of tradeHistory(t)
@drdozer
drdozer / gist:f17a37ad0064b4dd8066
Last active August 29, 2015 14:20
Fuse multiple requests into a single publisher
object RequestsManager {
case class Start(requests: () => DatabasePublisher[Request])
case class OnError(t: Throwable)
case object OnComplete
}
class RequestsManager() extends Actor with ActorPublisher[Request] {
import RequestsManager._
override def receive: Receive = {
@drdozer
drdozer / XsdSimpleTypes.scala
Last active August 29, 2015 14:15
xsd built-in data types in scala - a sketch
trait XsdSimpleTypes {
type anySimpleType
type string
type duration
type dateTime
type time
type date
type gYearMonth
type gYear
import simulacrum._
import scala.annotation.implicitNotFound
@implicitNotFound("Could not find constructor for ${T} that takes a single argument of ${A}")
trait Constructor1[+T, -A] {
def apply(a: A): T
}
@implicitNotFound("Could not find destructor for ${T} that decomposes into a single argument of ${A}")
@drdozer
drdozer / Designs.scala
Last active December 17, 2015 19:39
Simple genetic parts simulator
import javafx.scene.paint.Color
import uk.co.turingatemyhamster.gyo._
object Designs {
def main(args: Array[String]) {
val gfp = Reporter("GFP", Color.GREEN)
val gfpCDS = CDS("gfp", gfp)
val rfp = Reporter("RFP", Color.RED)
@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 {