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
// Definitions: | |
def t(f: => Any) { val start = System.currentTimeMillis; val res = f; println("Time: " + (System.currentTimeMillis - start) + "ms"); res} | |
// "Vanilla" version | |
def fib(num: Long): Long = if (num < 2) num else fib(num - 1) + fib(num - 2) | |
// Tail recursive version | |
def fibTR(num: Long) = { | |
@scala.annotation.tailrec | |
def fibtr(n: Long, nxt: Long, res: Long): Long = if (n == 0) res else fibtr(n - 1, res + nxt, nxt) |
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 | |
Welcome to Scala version 2.9.0.RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> def time(work: => Any) { val start = System.currentTimeMillis; work; println("Time: " + (System.currentTimeMillis - start) + "ms")} | |
time: (work: => Any)Unit | |
scala> time(Thread.sleep(10)) | |
Time: 11ms |
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
(defn transfer [from-account to-account amount] | |
(dosync | |
(if (> amount @(:balance from-account)) | |
(throw (new Exception "Not enough money!"))) | |
(alter (:balance from-account) - amount) | |
(alter (:balance to-account) + amount))) | |
(defrecord Account [account-no balance]) | |
(def account-a (Account. 1 (ref 1000))) |
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 reflect.Manifest | |
object CucumberScalaDsl extends Application { | |
/* Revision. Does not work because Manifest.classType uses erased types. | |
Hence you will get a ClassCastException when using a double in stead of an integer for example. | |
Thanks fredriv for spotting this! | |
type stringFunction = Function1[String, Unit] | |
type intStringFunction = Function2[Int, String, Unit] |