Skip to content

Instantly share code, notes, and snippets.

@sam
sam / fold.scala
Last active December 13, 2015 18:29
Fold is awesome.
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.
@sam
sam / Filters.scala
Last active December 13, 2015 18:29
Variations implementing a recursive filter() function in Scala. One using for-comprehensions, one using iterators. Another using a method instead.
// 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
//
@sam
sam / futures.scala
Last active December 13, 2015 17:09
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.
@sam
sam / Bench.scala
Last active December 11, 2015 13:38
Scala vs Ruby. Silly benchmarks.
new testing.Benchmark {
def run = (1 to 1000000).map(i => Math.pow(i,2))
}.runBenchmark(10)
@sam
sam / Lunch.scala
Last active December 10, 2015 15:19
Scala script (with sh shebang so it's self-executing) demonstrating Scala 2.10 String Interpolation, class-name reflection, method overriding, import of another scope into a nested scope, a symbol, a tuple2 (aka Pair) and use of the cons operator.
#!/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 {
@sam
sam / comparison.md
Last active December 10, 2015 00:48
Comparison of Scala and Ruby syntaxes based on examples from "Pragmatic Real-World Scala" presentation by Jonas Bonér: http://www.infoq.com/presentations/Scala-Jonas-Boner

Phonebook

Ruby

phonebook = {
  "Jonas" => "123456",
  "Sara"  => "654321" }

phonebook["Jacob"] = "987654"
// 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
import unfiltered.request._
import unfiltered.response._
val nice = unfiltered.filter.Planify {
case _ => ResponseString("Hello World!")
}
unfiltered.jetty.Http.anylocal.filter(echo).filter(nice).run()
package org.substantiality.blog
import org.scalatra._
class Blog extends ScalatraServlet {
get("/") {
<html>
<body>
<h1>Hello, world!</h1>
package org.substantiality.blog
import org.scalatra._
class Blog extends ScalatraServlet {
get("/") {
<html>
<body>
<h1>Hello, world!</h1>