Skip to content

Instantly share code, notes, and snippets.

Erik Rozendaal erikrozendaal

  • Zilverline
  • Amsterdam
View GitHub Profile
@erikrozendaal
erikrozendaal / NewType.swift
Last active Jan 21, 2016
Swift helpers voor minimal boilerplace "new type" support
View NewType.swift
//
// 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 / rle.idr
Created Dec 9, 2013
Run-length encoding in Idris
View rle.idr
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 / gist:7354327
Last active Dec 27, 2015
Creating exceptions with and without stacktrace. Measured on a early 2013 MacBook Pro 13" retina.
View gist:7354327
$ 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 Dec 21, 2015
Scalaz equivalent to Hibernate's JSR-303 based @notempty annotation
View gist:6294954
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 Dec 18, 2015
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.
View delete
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 Dec 13, 2015
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)
View build.sbt
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 Jul 22, 2012
Event sourcing example - part 2
View CommitPublisher.scala
/**
* 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 Jul 1, 2012
Event sourcing example - part 1
View Post.scala
/**
* 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 Jun 17, 2012
Lenses in Scala with support for Maps, Seqs, and JSON (using the Jerkson library)
View lenses.scala
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) {
View 1.sql
# --- !Ups
create table users (
name text primary key,
age int not null);
# --- !Downs
drop table users;
You can’t perform that action at this time.