Skip to content

Instantly share code, notes, and snippets.

View erikrozendaal's full-sized avatar

Erik Rozendaal erikrozendaal

  • Zilverline
  • Amsterdam
View GitHub Profile
@erikrozendaal
erikrozendaal / rle.idr
Created December 9, 2013 18:12
Run-length encoding in Idris
rep : (n : Nat) -> a -> List a
rep Z x = []
rep (S k) x = x :: rep k x
data RLE : List Char -> Type where
REnd : RLE []
RChar : (n : Nat) -> (c : Char) -> (rs : RLE xs) -> RLE (rep n c ++ xs)
rle : (xs : List Char) -> RLE xs
rle [] = REnd
@erikrozendaal
erikrozendaal / NewType.swift
Last active January 21, 2016 08:55
Swift helpers voor minimal boilerplace "new type" support
//
// Packages a value into a new, type-safe wrapper. The value can always be safely unwrapped.
//
public protocol NewType: CustomStringConvertible {
// The type of the wrapped value
typealias Unwrapped
// Accessor for the underlying value
var unwrapped: Unwrapped { get }
@erikrozendaal
erikrozendaal / gist:7354327
Last active December 27, 2015 16:19
Creating exceptions with and without stacktrace. Measured on a early 2013 MacBook Pro 13" retina.
$ scala -cp Thyme.jar
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val th = new ichi.bench.Thyme
th: ichi.bench.Thyme = ichi.bench.Thyme@7464fb1c
scala> th.pbenchOff[Exception]("stacktraces")(new Exception("foo") with scala.util.control.NoStackTrace, ftitle = "without")(new Exception("foo"), htitle = "with")
Benchmark comparison (in 20.94 s): stacktraces
@erikrozendaal
erikrozendaal / gist:6294954
Last active December 21, 2015 10:49
Scalaz equivalent to Hibernate's JSR-303 based @notempty annotation
def notNull[A](a: A): Validation[String, A] =
if (a != null) a.success else "may not be null".failure
def size[A <% Traversable[_]](min: Int = 0, max: Int = Int.MaxValue)(a: A): Validation[String, A] =
if (min to max contains a.size) a.success else s"size must be between $min and $max".failure
// Composing notNull and size, like https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/constraints/NotEmpty.java#L48
def notEmpty[A <% Traversable[_]](a: A): Validation[String, A] =
notNull(a) flatMap size(min = 1)
@erikrozendaal
erikrozendaal / delete
Last active December 18, 2015 06:59
Performance of initial implementation in-memory B-Tree vs the standard Scala Red-Black Tree implementation used by TreeSet. The B-Tree is about 5 times more memory efficient, so it works better for large sets due to memory locality effects. Update: initial delete implementation + benchmark numbers added.
Benchmark comparison (in 9.612 s): delete 1 shuffled values
btree vs treeset
Significantly different (p ~= 0)
Time ratio: 0.76712 95% CI 0.73631 - 0.79793 (n=20)
btree 78.60 ns 95% CI 75.96 ns - 81.25 ns
treeset 60.30 ns 95% CI 58.98 ns - 61.62 ns
Benchmark comparison (in 13.16 s): delete 10 shuffled values
btree vs treeset
Significantly different (p ~= 7.567e-11)
Time ratio: 1.06081 95% CI 1.04667 - 1.07495 (n=20)
@erikrozendaal
erikrozendaal / build.sbt
Last active December 13, 2015 23:19
Type driven development - an example Use a recent version of `sbt` to load the example into the scala console: $ sbt console scala> import timeseries._ scala> println(accountTotals)
name := "timeseries"
scalaVersion := "2.10.0"
libraryDependencies ++= Seq(
"joda-time" % "joda-time" % "2.1",
"org.joda" % "joda-convert" % "1.3")
@erikrozendaal
erikrozendaal / CommitPublisher.scala
Created July 22, 2012 12:32
Event sourcing example - part 2
/**
* Publishes successful commits to subscribers.
*/
trait CommitPublisher[Event] {
/**
* Notifies `listener` of all commits that happened `since`. Notification happens asynchronously.
*/
def subscribe(since: StoreRevision)(listener: Commit[Event] => Unit): Subscription
}
@erikrozendaal
erikrozendaal / Post.scala
Created July 1, 2012 18:55
Event sourcing example - part 1
/**
* A specific blog post with its current content.
*/
case class Post(id: PostId, content: PostContent)
/**
* The current state of blog posts, derived from all committed PostEvents.
*/
case class Posts(byId: Map[PostId, Post] = Map.empty, orderedByTimeAdded: Seq[PostId] = Vector.empty) {
def get(id: PostId): Option[Post] = byId.get(id)
@erikrozendaal
erikrozendaal / lenses.scala
Created June 17, 2012 12:03
Lenses in Scala with support for Maps, Seqs, and JSON (using the Jerkson library)
package lenses
import com.codahale.jerkson._, AST._
/**
* Lenses allow retrieving and updating values of type `B` inside some enclosing value of type `A`.
*
* Since lenses can be composed together, arbitrary nesting is automatically taken care of.
*/
case class Lens[A, B](get: A => B, set: (A, B) => A) {
@erikrozendaal
erikrozendaal / 1.sql
Created February 18, 2012 14:21
Play! 2.0
# --- !Ups
create table users (
name text primary key,
age int not null);
# --- !Downs
drop table users;