Skip to content

Instantly share code, notes, and snippets.

@d6y
d6y / moverload.scala
Created November 1, 2014 17:23
Enumerations & Method Overload
scala> :paste
// Entering paste mode (ctrl-D to finish)
object Colours extends Enumeration {
val Red, Amber, Green = Value
}
object WeekDays extends Enumeration {
val Mon,Tue,Wed,Thu,Fri = Value
}
@d6y
d6y / epatmat.scala
Created November 1, 2014 17:24
Enumerations & Pattern Matching
scala> :paste
// Entering paste mode (ctrl-D to finish)
def traffic(colour: Colours.Value) = colour match {
case Colours.Green => "Go"
}
// Exiting paste mode, now interpreting.
traffic: (colour: Colours.Value)String
@d6y
d6y / simple.scala
Created November 1, 2014 17:24
Enumerations & Simple Sealed Traits and Objects
object WeekDay {
sealed trait EnumVal
case object Mon extends EnumVal
case object Tue extends EnumVal
case object Wed extends EnumVal
case object Thu extends EnumVal
case object Fri extends EnumVal
val daysOfWeek = Seq(Mon, Tue, Wed, Thu, Fri)
}
@d6y
d6y / planets.scala
Created November 1, 2014 17:25
Enuerations and case objects
object SolarSystemPlanets {
sealed abstract class Planet(
val orderFromSun : Int,
val name : String,
val mass : Kilogram,
val radius : Meter) extends Ordered[Planet] {
def compare(that: Planet) = this.orderFromSun - that.orderFromSun
@d6y
d6y / using.scala
Created November 1, 2014 17:26
Enuerations and case objects: usage
scala> import SolarSystemPlanets._
import SolarSystemPlanets._
scala> println(planets)
TreeSet(Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune)
scala> EARTH < MARS
res1: Boolean = true
scala> planets.filter(_.radius > 7.0e6)
@d6y
d6y / CustomResourceFilter.scala
Last active August 29, 2015 14:09
Re-writing in the container
package code.filter
import javax.servlet._
import http._
import java.util.regex._
// A wrapper around an HTTP Servlet Request which removes any /cached/:id from a URI
case class ResouceRequest(req: HttpServletRequest) extends HttpServletRequestWrapper(req) {
// The URI without the /cached/:id part:
@d6y
d6y / notify.scala
Created November 19, 2014 07:48
Notify example
import play.api.libs.json._
case class Notify(email: Option[String] = None)
implicit val reader = Json.reads[Notify]
Json.parse(""" { "email": "foo@example.com" } """).asOpt[Notify]
// res0: Option[Notify] = Some(Notify(Some(foo@example.com)))
Json.parse(""" { } """).asOpt[Notify]
// res1: Option[Notify] = Some(Notify(None))
@d6y
d6y / val.scala
Created November 27, 2014 15:29
Exercise for the reader
import org.specs2._
class ProblemSpec extends Specification {
def is = s2"""
Validation should
combine two errors into one $twoBecomeOne
combine error and success into error $perfectionOnly
combine two success into one $oneForAll
"""
@d6y
d6y / console.txt
Created December 18, 2014 07:19
Console Scala Check
> test:console
scala> val f: Int => Int = _ + 1
f: Int => Int = <function1>
scala> val g: Int => Int = _ * 13
g: Int => Int = <function1>
scala> import scala.util._
import scala.util._
@d6y
d6y / config.xml
Last active August 29, 2015 14:13
Papertrail logback example configuation
<configuration>
<appender class="com.papertrailapp.logback.Syslog4jAppender" name="SYSLOG-TCP">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{MMM dd HH:mm:ss} myapp: %-5level %logger{35}: %m%n%xEx</pattern>
</layout>
<syslogConfig class="org.productivity.java.syslog4j.impl.net.tcp.TCPNetSyslogConfig">
<host>${PAPERTRAIL_HOST}</host>
<port>${PAPERTRAIL_PORT}</port>
<sendLocalName>false</sendLocalName>