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
implicit def aToEither[A,B](a: A): Either[A, B] = Left(a) | |
implicit def bToEither[A,B](b: B): Either[A, B] = Right(b) | |
def f(input: Either[String, Int]) = input match { | |
case Left(s) => println(s"Hello, $s") | |
case Right(i) => println(s"You are $i years old") | |
} | |
scala> f(10) | |
You are 10 years old |
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
class HttpClient { | |
def GET(url: String)(implicit context: ExecutionContext) = ??? | |
} | |
class SomeService(http: HttpClient = new HttpClient) { | |
def getSomething(implicit context: ExecutionContext) = ??? | |
} | |
class ServiceB { | |
def foo(arg: String)(implicit context: ExecutionContext) = |
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
trait Example { self => | |
def mode(m: String): self.type | |
} | |
case class Foo extends Example { | |
def mode(m: String) = this.copy(mode = m) | |
} | |
// abstract type, polymorhpic |
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
implicit class Func1Sugar[A,B](f: Function1[A, Option[B]]) { | |
def apply(a: Option[A]): Option[B] = a.flatMap(f) | |
} |
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
class Foo(x: Int) { | |
def this(x: String) = this(x.toInt) | |
} | |
class Bar(x: Int) | |
object Bar { | |
def apply(x: String) = new Bar(x.toInt) | |
} |
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
// ---- Presenter Pattern ---- | |
// presenters.scala | |
object presenters { | |
implicit class StringPresenter(s: String) { | |
def reverse = Html(s.reverse) | |
} | |
implicit DateTimePresenter(dt: DateTime){ | |
def format(f: String) = DateTimeFormat.forPattern(f).print(dt) |
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
class BadActor extends Actor { | |
def recieve = { | |
case Process(id: Int) => process(id) | |
} | |
private def process(id: Int) = { | |
val result = SomeService.get(id) | |
result onComplete { value => | |
sender ! value |
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
implicit class StringOps(s: String) { | |
val one = { println("one"); "one" } | |
val two = { println("two"); "two" } | |
lazy val three = { println("three"); "value" } | |
} | |
scala> "asdf".three | |
one |
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
// -- Measuring --- | |
def time[A](label: String, count: Int = 100)(block: => A): Long = { | |
// the first test run on the console is compiling it | |
block | |
// Take the average of <count> runs | |
(1 to count).toSeq.map { i => | |
val t0 = System.nanoTime() | |
block |
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
// bool.scala.html | |
@(param: Boolean) | |
<h1>Value: @param</h1> | |
// index.scala.html | |
@bool(Some(false)) | |
@bool("false") |