def qsort[A <% Ordered[A]](v: Seq[A]): Seq[A] = v.headOption match {
case Some(x) => (qsort(v.tail.filter(_ < x)) :+ x) ++ qsort(v.tail.filter(_ >= x))
case _ => Seq()
}
This file contains 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
/* | |
Given a map defined like: | |
def map[A, B](f: A => B)(btree: BTree[A]): BTree[B] = | |
fold((_:A) => Leaf(f(_)).asInstanceOf[BTree[B]])((x:BTree[B]) => (y:BTree[B]) => Fork(x,y))(btree) | |
when applied on a tree ((1 2) (3 4)) | |
map((x:Int) => x*2)(BTree(1 :: 2 :: 3 :: 4 :: Nil)) | |
results in ((<function1> <function1>) (<function1> <function1>)) |
This file contains 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
scala> trait YouCantMatchMe | |
defined trait YouCantMatchMe | |
scala> object YesICan extends Traitor[YouCantMatchMe] | |
defined module YesICan | |
scala> def uncatchable_?(any: Any) = any match { | |
| case YesICan(uncatchable) => Some(uncatchable) | |
| case _ => None | |
| } |
This file contains 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
//performance is not everything | |
//can this be parallel? | |
def isPalindrome(list: List[Int]) = { | |
(list zip list.reverse filter (a => a._1 != a._2) size) == 0 | |
} |
This file contains 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 sbt._ | |
import Keys._ | |
import sbt.Package._ | |
import java.util.jar.Attributes.Name._ | |
import com.github.siasia.WebPlugin._ | |
//unashamed copy of scalaz's build | |
object SlogBuild extends Build { | |
override lazy val settings = super.settings :+ |
This file contains 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
#!/bin/sh | |
L=`dirname $0`/../lib | |
cp=`echo $L/*.jar|sed 's/ /:/g'` | |
exec scala -classpath $cp -savecompiled $0 $@ | |
!# | |
# shebang magic from http://www.eamonn.org/blog/archives/425 | |
println("hello world") |
This file contains 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 Control.Monad (ap) | |
type Pos = (Int, Int) | |
type Direction = Pos -> Pos | |
direction :: Pos -> [Direction] -> [Pos] | |
direction p [] = [] | |
direction p (x:xs) = x p : direction p xs | |
-- another way | |
-- direction p ds = ap ds [p] |
This file contains 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 add = """.*: what is (\d*) plus (\d*)""".r | |
q match { | |
case add(a, b) =>(a.toInt + b.toInt).toString | |
case bond() => // well we don't want to reveal too much | |
case banana() => //but bond and banana ought to get the attention. | |
case _ => "" | |
} |
This file contains 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
:silent | |
//the following are helper functions | |
def p = if(readLine() == "q") sys.exit | |
def pp[A]: ( => A) => Unit = a => {readLine() ; a} | |
object > { | |
def > : String => Unit = println _ | |
def >[A] : (String, => A) => Unit = (a,b) => {print("> " + a); readLine(); readLine(); println(" ----> " + b); println} | |
} |
OlderNewer