Skip to content

Instantly share code, notes, and snippets.

@johnynek
johnynek / twophase.scala
Last active October 2, 2020 22:36
A two-phase commit Transaction Monad. See: http://en.wikipedia.org/wiki/Two-phase_commit_protocol
// Run this with scala <filename>
/**
* A Two-phase commit Monad
*/
trait Transaction[+T] {
def map[U](fn: T => U): Transaction[U] = flatMap { t => Constant(fn(t)) }
def flatMap[U](fn: T => Transaction[U]): Transaction[U] =
FlatMapped(this, fn)
@kevinwright
kevinwright / ZeeingEvent.scala
Created July 10, 2012 12:19
Typesafe conversion from List[Any] to a case class, via shapeless
case class ZeeingEvent(
zid: String,
kind: String,
showId: String,
show_name: Option[String],
time: DateTime
) {
require (kind == "StartedZeeing" || kind == "EndedZeeing")
}
@SethTisue
SethTisue / gist:2942990
Created June 17, 2012 00:34
add zipWith to Iterable
// add zipWith to Iterable; thank you stackoverflow.com/q/3895813
implicit class RichIterable[A, CC[X] <: Iterable[X]](xs: CC[A]) {
type Builder[C] = collection.generic.CanBuildFrom[Nothing, C, CC[C]]
def zipWith[B, C](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: Builder[C]): CC[C] =
xs.zip(ys).map(f.tupled)(collection.breakOut)
}
@ghostm
ghostm / MongoProtoUser.scala
Created November 11, 2010 05:53
MetaMegaProtoUser that uses Mongo
/*
Copyright 2012 Matthew Henderson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software