Skip to content

Instantly share code, notes, and snippets.

import scala.collection.immutable.{TreeMap,SortedMap}
def fromListWith[A,K<%Ordered[K]](seq:Iterable[(K,A)])(f: (A,A)=>A):SortedMap[K,A] =
seq.foldLeft(TreeMap.empty[K,A]){
(map:TreeMap[K,A],entry) =>
val (key,ele) = entry
// update entry by either applying f to existing elements or
// just starting a new entry
map.update(key,map.get(key).map(f(ele,_)).getOrElse(ele))
}
@jrudolph
jrudolph / TowersOfHanoi.scala
Created February 19, 2009 13:51
Scala-Metaprogramming: Towers of Hanoi
/*
* This is the Towers of Hanoi example from the prolog tutorial [1]
* converted into Scala, using implicits to unfold the algorithm at
* compile-time.
*
* [1] http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_3.html
*/
object TowersOfHanoi {
import scala.reflect.Manifest
// This code is based on an idea of Rafael de F. Ferreira and was implemented as a response to
// Jim McBeath's blog post http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html
// Everyone is free to use, modify, republish, sell or give away this work without prior consent from anybody.
object Scotch {
sealed abstract class Preparation
case object Neat extends Preparation
case object OnTheRocks extends Preparation
case object WithWater extends Preparation
unzip -q $1 -d $1.dir &&
find $1.dir ! -executable -type d -exec chmod +rwx {} \; &&
find $1.dir ! -readable -exec chmod +r {} \; &&
rm $1 &&
mv $1.dir $1 &&
echo $1
final class MyRange(start: Int, limit: Int, step: Int) extends IndexedSeq[Int] {
val length: Int = {
def plen(start: Int, limit: Int, step: Int) =
if (limit <= start) 0 else (limit - start - 1) / step + 1
if (step > 0) plen(start, limit, step)
else plen(limit, start, -step)
}
override def apply(idx: Int) = {
if (idx < 0 || idx >= length) throw new IndexOutOfBoundsException(idx.toString)
start + idx * step
#!/usr/bin/perl -w
# This is derived from the script posted by Thomas Rast in this mailing list post:
# http://n2.nabble.com/Blamming-a-diff-between-two-commits-td2340836.html
sub parse_hunk_header {
my ($line) = @_;
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
$line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
$o_cnt = 1 unless defined $o_cnt;
trait MapReduce[E, R] {
type S
def map(e: E): S
def reduce(s1: S, s2: S): S
def finish(s: S): R
}
def linear[E, R](seq: Array[E], mr: MapReduce[E, R]): R =
mr.finish(seq.map(mr.map).reduceLeft(mr.reduce))
def binarySearch[B, A <% Ordered[B]](a: Array[A], v: B) = {
def recurse(low: Int, high: Int): Option[Int] = (low + high) / 2 match {
case _ if high < low => None
case mid if a(mid) > v => recurse(low, mid - 1)
case mid if a(mid) < v => recurse(mid + 1, high)
case mid => Some(mid)
}
recurse(0, a.size - 1)
}
import org.quartz
import org.quartz._
class ScalaJob extends Job {
def execute(ctx: JobExecutionContext): Unit = {
ctx.getMergedJobDataMap.get("closure").asInstanceOf[JobExecutionContext => Unit].apply(ctx)
}
}
object QuartzTest {
// Tony Morris' Optional Exercises
// see http://scala-programming-language.1934581.n4.nabble.com/Further-understanding-scala-Option-td2334540.html
// Scala version 2.8.0.final
// http://scalacheck.googlecode.com/files/scalacheck_2.8.0-1.8-SNAPSHOT.jar
/*
Below are 15 exercises. The task is to emulate the scala.Option API
without using Some/None subtypes, but instead using a fold (called a
catamorphism).