Skip to content

Instantly share code, notes, and snippets.

@tstone
tstone / implicit-hierarchy.scala
Created March 24, 2014 18:36
Experimenting with how Scala handles multiple implicit conversions in scope when those conversions apply to child types.
scala> class A { val prop = "a" }
scala> class B extends A { override val prop = "b" }
scala> class C extends B
scala> implicit class Foo(a: A) { def value = a.prop }
scala> implicit class Bar(b: B) { def value = b.prop }
scala> val c = new C
scala> c.value
res0: String = b
// Given
trait Common
class A extends Common
class B extends Common
class C extends Common
// These are equivalent...
scala> import mq.models.place._
import mq.models.place._
scala> import mq.models._
import mq.models._
scala> val a = Airport(123, "slug", Address("US"), LatLng(1,-1), PlaceAttributes(), PoiAttributes(), AirportAttributes())
a: mq.models.place.Airport = Airport(123,slug,Address(US,None,None,None,None,None),LatLng(1.0,-1.0),PlaceAttributes(None,List(),None),PoiAttributes(None,None,None,None,None,List(),List(),None,None,None,List(),List(),List(),List(),None,List()),AirportAttributes(,List(),None))
scala> a.name(Some("foo"))
class A
class B extends A
class C extends B
val a = new A
val b = new B
val c = new C
def f[T : ClassTag](a: A) = a match {
case _: T => "is given type"
@tstone
tstone / javascript-in-scala.scala
Last active August 29, 2015 14:00
Javascript in Scala
// Function "add" implemented in javascript
function add(x, y) {
return x + y;
}
// Function "add" implemented in Scala as javascript is actually implementing it behind the scenes
sealed abstract class DynamicType
@tstone
tstone / gist:cb57355fc70cf6661cd9
Last active August 29, 2015 14:00
Twirl conversions
// bool.scala.html
@(param: Boolean)
<h1>Value: @param</h1>
// index.scala.html
@bool(Some(false))
@bool("false")
@tstone
tstone / fitler-first-vs-distinct-after.scala
Last active August 29, 2015 14:02
A little performance test for two approaches to resolving duplicates between two lists
// -- Measuring ---
def time[A](label: String, count: Int = 100)(block: => A): Long = {
// the first test run on the console is compiling it
block
// Take the average of <count> runs
(1 to count).toSeq.map { i =>
val t0 = System.nanoTime()
block
class BadActor extends Actor {
def recieve = {
case Process(id: Int) => process(id)
}
private def process(id: Int) = {
val result = SomeService.get(id)
result onComplete { value =>
sender ! value
@tstone
tstone / angular-filters.scala
Last active August 29, 2015 14:02
Angular Style Filters in Scala/Twirl
// ---- Presenter Pattern ----
// presenters.scala
object presenters {
implicit class StringPresenter(s: String) {
def reverse = Html(s.reverse)
}
implicit DateTimePresenter(dt: DateTime){
def format(f: String) = DateTimeFormat.forPattern(f).print(dt)
class Foo(x: Int) {
def this(x: String) = this(x.toInt)
}
class Bar(x: Int)
object Bar {
def apply(x: String) = new Bar(x.toInt)
}