Skip to content

Instantly share code, notes, and snippets.

View jedws's full-sized avatar

Jed Wesley-Smith jedws

View GitHub Profile
@jedws
jedws / TreiberStack.scala
Last active July 4, 2016 12:53
Pattern vs Catamorphism? an exercise in different approaches to implementing a simple data-structure
import com.atlassian.util.scala.concurrent.Atomic //https://bitbucket.org/jwesleysmith/atlassian-util-scala/src/tip/src/main/scala/com/atlassian/util/scala/concurrent/Atomic.scala
package patterns {
final class TreiberStack[A] {
private[this] val head = Atomic[Node[A]](End)
@annotation.tailrec
def pop: Option[A] = head.value match {
case l @ Link(a, prev) => if (head.compareAndSet(l, prev)) Some(a) else pop
case _ => None
sealed trait Heading {
val turn = Turn(this) _
}
case object N extends Heading
case object S extends Heading
case object E extends Heading
case object W extends Heading
object Turn {
def apply(h: Heading)(t: Turn) = t(h)
val nonIgnoredFields = {
@annotation.tailrec
def loop(k: Class[_], fields: List[Array[Field]]): List[Array[Field]] = {
if (k == null) fields
else
loop(k.getSuperclass, k.getDeclaredFields.filterNot(ignoreField _).reverse :: fields)
}
loop(klass, Nil).flatten
}
object Sort {
def qsort[A, CC[A] <: TraversableLike[A, CC[A]]](c: CC[A])(implicit ord : Ordering[A], bf: CanBuildFrom[CC[A], A, CC[A]]) : CC[A] = {
if (c.isEmpty || c.tail.isEmpty) c
else {
val pivot = c.head
val (low: CC[A], high: CC[A]) = c.tail.partition{ ord.lt(_, pivot) }
qsort(low) ++ bf().+=(pivot).result ++ qsort(high)
}
}
}
@jedws
jedws / Threaded.scala
Created August 17, 2011 07:40
alternate Threaded
trait Threaded[T] extends Runnable {
@volatile private var stopping = false
@volatile private var thread: Thread = _
def afterPropertiesSet() {
thread = new Thread(this)
thread.start()
}
@jedws
jedws / conway.scala
Created September 2, 2011 00:44
functional version of conway's life
/**
* A functional Conway's game of life.
*/
package object conway {
type Coord[A] = (Int, Int) => A
type Calculator = Coord[Coord[Boolean] => Boolean]
type Size = Int
val nextCell: Boolean => Int => Boolean =
@jedws
jedws / conwaydef.scala
Created September 5, 2011 22:58
alternate Functional Conway with def instead of function vals
/**
* A functional Conway's game of life.
*/
package object conwaydef {
type Coord[A] = (Int, Int) => A
type Calculator = Coord[Coord[Boolean] => Boolean]
type Size = Int
def nextCell(old: Boolean)(mates: Int) = if (mates > 3) false else if (mates == 3) true else (old && mates == 2)
@jedws
jedws / conway3.scala
Created September 6, 2011 21:41
Another conway
package object conway3 {
type Gen = (Int, Int) => Boolean
type Calculator = Gen => Gen
type Size = Int
val nextCell: Boolean => Int => Boolean =
old => mates => if (mates > 3) false else if (mates == 3) true else (old && mates == 2)
val wrapper: Size => Int => Int =
@jedws
jedws / ZipWith.scala
Created September 28, 2011 03:03
ZipWith adds a zipWith(f: A => B): C[(A, B)] to any collection
object ZipWith {
implicit def pimpToZipWith[A, CC[A] <: Iterable[A]](cc: CC[A]) = new ZipWith(cc)
class ZipWith[A, CC[A] <: Iterable[A]](cc: CC[A]) {
import collection.generic.CanBuildFrom
def zipWith[B](f: A => B)(implicit cbf: CanBuildFrom[CC[A], (A, B), CC[(A, B)]]): CC[(A, B)] = {
val builder = cbf()
cc foreach { a => builder += (a -> f(a)) }
builder.result
@jedws
jedws / BankersQueue.scala
Created January 14, 2012 23:27
Data Structures - A Persistent Queue
/**
* A simple implementation of a Banker's Queue http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf 3.4.2.
*
* Note that this implementation differs slightly from the one described in the original
* paper in that the reverse operation is performed only when the out/front list is empty.
* This is done purely to simplify this example implementation.
*/
sealed trait BankersQueue[+A] {
/** The head element of the queue, or an exception if empty. */
def head: A