phonebook = {
"Jonas" => "123456",
"Sara" => "654321" }
phonebook["Jacob"] = "987654"
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 things = Seq(1,2,3,4,5,6).map(Some(_)) ++ Seq(None, None) | |
for(thing <- things) | |
yield thing.fold { | |
println("Bummer Dude") | |
} { value => | |
value % 2 | |
println("whatever man") | |
println(""" | |
See? Long things can happen in the "success" scenario. |
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
// Normally you define the type of a value like so: | |
// | |
// val name:String = "bob" | |
// | |
// The pattern here is varname:Type = Value | |
// If you need to specify the Type for an anonymous function, | |
// it'll follow the pattern: | |
// | |
// (ParamemterType1, ParameterType2) => ReturnType | |
// |
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 concurrent._ | |
import ExecutionContext.Implicits.global | |
// Sleep each for 10 seconds: | |
for { | |
result1 <- future { Thread.sleep(10000); 1 } | |
result2 <- future { Thread.sleep(10000); 2 } | |
} println(s"result1 -> $result1, result2 -> $result2") | |
// So that took about 20 seconds. for-comprehensions are a way to flatten loops. |
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
new testing.Benchmark { | |
def run = (1 to 1000000).map(i => Math.pow(i,2)) | |
}.runBenchmark(10) |
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
#!/bin/sh | |
exec scala "$0" "$@" | |
!# | |
// The above prologue simply lets us make this script self-executable, as long as you have scala installed. | |
// As far as I can tell, a "main class" file *MUST* declare only one object in the top-level scope. | |
// So our other objects need to be in other files/packages, or defined in an inner-scope. Which is | |
// why we have a Lunch.Pizza object instead of declaring the Pizza object at the top-level scope. | |
object Lunch { | |
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
// Symbols converted to Strings in Scala retain their literal notation | |
// with the leading apostraphe, so we have to drop dat to get the same | |
// output as Ruby. | |
Seq('bradley, 'ryan, 'sam, 'scott).map(_.toString.drop(1).toUpperCase) | |
// Here's a for-comprehension, which doesn't look as nice in the trivial | |
// example, but if you're going to nest maps/filters/etc, can be much | |
// more readable in the complex/compound operations: | |
for(name <- Seq('bradley, 'ryan, 'sam, 'scott)) yield name.toString.drop(1).toUpperCase |
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 unfiltered.request._ | |
import unfiltered.response._ | |
val nice = unfiltered.filter.Planify { | |
case _ => ResponseString("Hello World!") | |
} | |
unfiltered.jetty.Http.anylocal.filter(echo).filter(nice).run() |
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 org.substantiality.blog | |
import org.scalatra._ | |
class Blog extends ScalatraServlet { | |
get("/") { | |
<html> | |
<body> | |
<h1>Hello, world!</h1> |
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 org.substantiality.blog | |
import org.scalatra._ | |
class Blog extends ScalatraServlet { | |
get("/") { | |
<html> | |
<body> | |
<h1>Hello, world!</h1> |