def common[T<:Equals](a: T, b: T): Option[T] = {
if(a == b) Some(a) else None
}
def common[T<:AnyVal](a: T, b: T): Option[T] = {
if(a == b) Some(a) else None
}
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 com.github.sam.examples | |
object Quasiquotes extends App { | |
val universe = reflect.runtime.universe | |
import universe._ | |
import reflect.runtime.currentMirror | |
import scala.tools.reflect.ToolBox |
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
libraryDependencies ++= { | |
CrossVersion.partialVersion(scalaVersion.value) match { | |
case Some((2, 11)) => commonDependencies ++ Seq( | |
"com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.1.1") | |
case _ => commonDependencies ++ Seq( | |
"com.typesafe" %% "scalalogging-slf4j" % "1.0.1") | |
} | |
} |
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
object HomepagePresenter { | |
import newsroom.api | |
import TupleFutureUnzip._ | |
def apply(context: Context)(implicit ec: ExecutionContext): Future[HomepagePresenter] = { | |
// prepare all our futures since a for-comprehension would serialize our work (and that would be bad). | |
val future_Tree = ChanneledPresenter.tree | |
val future_Bulletin = api.bulletins.current |
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
// Example values. | |
val applyTitle = true | |
val title = Some("This is my Title!") | |
// We want to map these values to Option[Option[String]] | |
// where Option[String] is our value, and the outer Option[_] | |
// is an indicator on wether we should later use the value or not. | |
// | |
// eg: We want to support three use-cases: | |
// |
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 presentation.json | |
import org.scalautils._ | |
import Accumulation._ | |
case class ErrorMessage(message: String, parameters: Any*) { | |
override def toString = message.format(parameters: _*) | |
} | |
case class Person(name: String, age: Int) |
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
name := "foo" | |
version := "1.0-SNAPSHOT" | |
scalaVersion := "2.10.3" | |
scalacOptions in ThisBuild ++= Seq( | |
"-language:_", | |
"-feature", | |
"-unchecked", |
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
# (Assuming you have OpenJDK 7 and Homebrew installed.) | |
# Install SBT: | |
brew install sbt | |
# Clone an example application: | |
git clone [email protected]:ssmoot/scala-map-benchmarks.git | |
# Download the Scala compiler version specified by the project... | |
# and download the SBT version specified by the project... |
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 scala.concurrent._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.duration._ | |
import scala.language.postfixOps | |
object Main extends App { | |
def future = Future { | |
Thread sleep 5000 | |
println("Future Completed") |
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
// Imagine you have a lot of futures. You don't want to make them synchronous with a for-comprehension, so you want to zip them. | |
// Unfortunately you'll get nested tuples from that, and if you have more than two, that gets ugly quick. | |
// ScalaZ has a solution with the |@| operator, but it's more general and seems to really torture the compiler. | |
// This is an alternative for this specific scenario (zip several async Futures together and map them), and is much faster | |
// (in IntelliJ at least). | |
import my.api | |
import TupleFutureUnzip._ | |
(ChanneledPresenter.tree, api.photos.all) unzip(new PhotosPresenter(_, _)) |