This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Threaded[T] extends Runnable { | |
| @volatile private var stopping = false | |
| @volatile private var thread: Thread = _ | |
| def afterPropertiesSet() { | |
| thread = new Thread(this) | |
| thread.start() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 |
OlderNewer