Skip to content

Instantly share code, notes, and snippets.

@sam
sam / CompilationExample.scala
Created February 5, 2016 19:55
Caching compilation results errors
def compile(): List[CompiledScript] = {
val outputCache = directory / ".cache"
if(!outputCache.exists()) outputCache.mkdirs()
val engine: IMain = new ScriptEngineManager().getEngineByName("scala").asInstanceOf[IMain]
engine.settings.usejavacp.value = true
// TODO: This breaks things. No idea why.
// engine.settings.outdir.value = outputCache.getCanonicalPath
directory.listFiles().toList collect {
@sam
sam / ApplicationController.scala
Last active December 15, 2015 19:40
How to provide your own custom MessagesApi in PlayFramework when using a "base" subproject for branded projects.
package com.wieck.base
package controllers
import play.api.{Application, Play, mvc, i18n}, mvc._, i18n._
abstract class ApplicationController extends Controller {
private val messagesApiCache = Application.instanceCache[BaseMessagesApi]
implicit def messages(implicit lang: Lang): Messages = new Messages(lang, messagesApiCache(Play.current))
@sam
sam / I18nComponents.scala
Created December 15, 2015 16:30
Custom sub-project Messages overriding example
trait I18nComponents {
def environment: Environment
def configuration: Configuration
// NOTE line 10! Load subproject messages, and override with available root project messages.
lazy val messagesApi: MessagesApi = new DefaultMessagesApi(environment, configuration, langs) {
override protected def loadAllMessages: Map[String, Map[String, String]] = {
langs.availables.map(_.code).map { lang =>
(lang, loadMessages("messages.mysubproject." + lang) ++ loadMessages("messages." + lang))
val firstLove = future {
Thread.sleep(500)
"i love you"
}
val thenBetray = firstLove map {
case loveLetter => {
Console.println(loveLetter)
Thread.sleep(500)
"not really"
}
@sam
sam / Fallback.scala
Created November 5, 2015 16:20
Example on how to fallback several Options with `orElse` with is a method to create a fallback chain for Options of the same type.
case class PhotoListItem(title: Option[String], filename: Option[String]) {
// Not bad:
def displayTitle(implicit messages: Messages): String = {
(title, filename) match {
case (Some(title), _) => title // So I want the title if present.
case (_, Some(filename)) => filename // Fallback to filename.
case _ => messages("label.titleNotAvailable") // Otherwise return a canned message.
}
}
@sam
sam / ListContainsExtractor.scala
Created October 9, 2015 18:43
Trying to come up with an Extractor I can pass input to other than my match object.
case class Contains(test: String) {
def unapply(list: List[_]): Boolean = list contains test
}
@sam
sam / ShowMeTheCurry.scala
Created September 29, 2015 20:43
Is there a way to zip nested functions A => T and B => T into a (A, B) => T?
object helpers {
type Closeable = { def close() }
def using[T, S <: Closeable](source: S)(f: S => T) = {
val result = f(source) // Simplified for the example.
source.close()
result
}
}
@sam
sam / Job.scala
Last active September 28, 2015 18:22
class Job extends Actor {
def receive = idle
def idle: Receive = {
case Parse(_uri) =>
uri = _uri
requestor = context.sender()
sources ! Open(_uri)
@sam
sam / PartialFunctionChainingWithInheritance.scala
Created September 28, 2015 18:00
Trying to figure out how to chain partial functions with inheritance...
trait Parser { self: Actor =>
def parse: PartialFunction[Opened, Unit]
def receive: Receive = parse orElse(super.receive) andThen(_ => done())
def collectWork: Receive
def done(): Unit = {
context become collectWork
@sam
sam / Scraper.scala
Created September 28, 2015 15:04
Simple "overwatch" actor to shut down the system when done.
import akka.actor._
object Scraper {
def props = Props[Scraper]
}
class Scraper extends Actor with ActorLogging with Unhandled {
import MigrationProtocol._