Skip to content

Instantly share code, notes, and snippets.

@sam
sam / Quasiquotes.scala
Created May 7, 2014 14:59
I keep getting the errors in `errors.sh` when I try to compile or run my Quasiquotes example main class.
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
@sam
sam / build.sbt
Created April 23, 2014 18:54
Cross compiling with scala-logging.
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")
}
}
@sam
sam / HomepagePresenter.scala
Created April 22, 2014 20:29
Demonstrating different techniques for handling lots of Futures.
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
@sam
sam / BooleanOptions.scala
Last active August 29, 2015 13:57
Examples of using Booleans to filter optional values.
// 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:
//

Scalaisms

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
}
@sam
sam / ParseWithLocalization.scala
Last active August 29, 2015 13:57
Brainstorming how to localize error messages with scalautil.org's Or and Every.
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)
@sam
sam / build.sbt
Created October 25, 2013 22:11
Example build.sbt containing Git project refs.
name := "foo"
version := "1.0-SNAPSHOT"
scalaVersion := "2.10.3"
scalacOptions in ThisBuild ++= Seq(
"-language:_",
"-feature",
"-unchecked",
@sam
sam / example.sh
Created October 22, 2013 18:36
From Zero to Run with SBT
# (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...
@sam
sam / Example.scala
Last active December 26, 2015 05:39
Example of passing a function to Future.map. This might not do what you think...
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")
@sam
sam / Example.scala
Created October 11, 2013 20:43
Makes mapping lots of futures pretty/easy.
// 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(_, _))