Skip to content

Instantly share code, notes, and snippets.

case class Person(firstName: String, lastName: String)
trait Constructable[T] {
def construct: T
}
implicit object IntIsConstructable extends Constructable[Int] {
def construct = 42
}
implicit object StringIsConstructable extends Constructable[String] {
def construct = "Hello World"
//My take on: http://solog.co/47/10-scala-one-liners-to-impress-your-friends/
//1. Multiple Each Item in a List by 2
//(1 to 10) map { _ * 2 }
(1 to 10) map (2*)
//2. Sum a List of Numbers
//(1 to 1000).reduceLeft( _ + _ )
(1 to 1000).sum
@kevinwright
kevinwright / XML Traversal
Created September 30, 2011 08:34
Deeper than flatmap, taking on whole trees in one bite!
def flatTraverse[T](node: Node)(pfn: PartialFunction[Node, TraversableOnce[T]]): Seq[T] = {
traverse(node)(pfn).flatten
}
def traverse[T](node: Node)(pfn: PartialFunction[Node, T]): Seq[T] = {
def inner(n: Node, acc: List[T]): List[T] = n match {
case x if (pfn isDefinedAt x) => pfn(x) :: acc
case e: Elem => (e.child.toList map {inner(_, Nil)}).flatten ::: acc
case _ => acc
}
@kevinwright
kevinwright / PimpMyFilter.scala
Created October 4, 2011 22:11
Pimpin' + on a T => Either[ERR,T] for easy chaining
class Filters[T] {
//Some boilerplate-busting aliases
type FilterResult = Either[Rejection, T]
type FilterFunc = (T) => FilterResult
//Handy helpers
//Rejection can carry as much info as you wish;
// Filter name, value in error, an exception, etc.
case class Rejection(input: T, msg: String)
@kevinwright
kevinwright / Boot.scala
Created November 28, 2011 19:30
Scalate in Akka
...
lazy val engine: TemplateEngine = {
val ctx = CaptureContext.context.get
val srcDirectories = ctx.getRealPath("/") match {
case path: String => List(new File(path))
case null => List()
}
println("Scalate got src directories " + srcDirectories)
new TemplateEngine(srcDirectories)
@kevinwright
kevinwright / Complex.scala
Created March 31, 2012 00:15 — forked from faermanj/Complex.scala
exploring-scala-1
def + + [B>: A, That] (that: TraversableOnce [B]) (implicit bf: CanBuildFrom [List [A], B, That]): That
@kevinwright
kevinwright / Initializer.scala
Created May 28, 2012 14:21
Init spray 1.0 with a customisable ActorSystem name
package com.zeebox.starwatch
import javax.servlet.{ServletContextEvent, ServletContextListener}
import java.net.InetAddress
import akka.actor.ActorSystem
import cc.spray.SprayServletSettings
import akka.config.ConfigurationException
import akka.util.{Reflect, Switch}
@kevinwright
kevinwright / ActorSystemFactory.scala
Created May 28, 2012 18:38
Building a better ActorSystem (for spray)
package com.zeebox.starwatch
import akka.actor.ActorSystem
import akka.util.Reflect
import java.net.InetAddress
import com.typesafe.config.ConfigFactory
import org.slf4j.LoggerFactory
object ActorSystemFactory {
val log = LoggerFactory.getLogger(this.getClass)
@kevinwright
kevinwright / ZeeingEvent.scala
Created July 10, 2012 12:19
Typesafe conversion from List[Any] to a case class, via shapeless
case class ZeeingEvent(
zid: String,
kind: String,
showId: String,
show_name: Option[String],
time: DateTime
) {
require (kind == "StartedZeeing" || kind == "EndedZeeing")
}