Skip to content

Instantly share code, notes, and snippets.

@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)

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 / 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:
//
@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 / 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 / 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
case class Foo(a: String, b: Int)
object Foo {
def apply(a: String, b: String) = new Foo(a, b.toInt)
}
val format = jsonFormat2[Foo](new {
def apply(a: String, b: Int): Foo = Foo(a, b)
def unapply(f: Foo) = Foo.unapply(f)
@sam
sam / Overrides.scala
Created July 2, 2014 19:03
To Override or Not To Override (that is the question)
// To Override or Not To Override (that is the question):
trait Foo {
def close: Unit
def bar(baz: String): Int
def zed(y: Int): Int = y * 2
}
@sam
sam / Sender.scala
Created July 29, 2014 14:18
Akka Props are a closure?
// It seems like Props is a closure that gets reused? The first pass will work.
// The next will cause the BatchUpdate Actor to send deadLetters. As if the sender
// terminated prematurely.
query {
Rows(multiStoryQuery(revs.map(_._1)).list)
} pipeTo context.actorOf(Props(new BatchUpdate(context.sender(), revs)), s"batch-update-story-${Math.abs(Random.nextInt())}")
// This works however. So the problem actually seems to be that successive iterations of the above
// version go to the same (now stopped) passed sender.
@sam
sam / Escape.scala
Created August 5, 2014 22:34
Escape all in String
object Lucene {
object Escape extends (String => String) {
private val prefix: Char => PartialFunction[Char, String] = c => { case `c` => s"\\$c" }
private val escapist: PartialFunction[Char, String] = prefix('\\') orElse
prefix('+') orElse
prefix('-') orElse
prefix('!') orElse