This gist has been upgraded to a blog post here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> trait Assoc[K] { type V ; val value: V } | |
defined trait Assoc | |
scala> def mkAssoc[V0](k: String, v: V0): Assoc[k.type] { type V = V0 } = | |
| new Assoc[k.type] { type V = V0 ; val value = v } | |
mkAssoc: [V0](k: String, v: V0)Assoc[k.type]{type V = V0} | |
scala> implicit def nameAssoc = mkAssoc("Name", "Mary") | |
nameAssoc: Assoc[String("Name")]{type V = String} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.twitter.util.{Duration, Time} | |
/** | |
* Sort of a lazy val, but that refreshes itselfs every so often; or a temporary cache for a value T, | |
* will be refreshed at a minimun at the specified interval. | |
* In practice, the refresh is done only when the value is accessed, so there are no guarantees | |
* to when it will actually be refreshed. You can just be sure that it won't be refreshed if two calls | |
* are made within `every`. | |
*/ | |
class RefreshEvery[T](every: Duration)(refresh: => T) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cosine distance and pearson correlation | |
// see http://ow.ly/vtm44 | |
package com.allenday | |
import com.twitter.scalding._ | |
import com.twitter.scalding.mathematics._ | |
import com.twitter.scalding.mathematics.Matrix._ | |
class SimJob(args : Args) extends Job(args) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import syntax.typeable._ | |
import syntax.typeable._ | |
scala> val wat: Any = List(1, 2, 3, 4) | |
wat: Any = List(1, 2, 3, 4) | |
scala> wat.cast[List[Int]].map(_.sum) | |
res0: Option[Int] = Some(10) | |
scala> val wat2: Any = "foo" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> :paste | |
// Entering paste mode (ctrl-D to finish) | |
case class Address(street : String, city : String, postcode : String) | |
case class Person(name : String, age : Int, address : Address) | |
// Exiting paste mode, now interpreting. | |
defined class Address | |
defined class Person |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aux | |
object Scaladores extends App { | |
implicit class AddOf(val num : Int) extends AnyVal { | |
def of[T <: MeasureUnit] = Value[T](num) | |
} | |
trait MeasureUnit | |
trait x[A <: MeasureUnit, B <: MeasureUnit] extends MeasureUnit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor._ | |
// need an actor system, make it implicit to it's used by all our repl based actors | |
implicit val system = ActorSystem("replActorSystem") | |
// this gives us an easy way to define simple actors | |
import ActorDSL._ | |
// make an actor that can be used to receive responses... just a good practice | |
implicit val sender = actor(new Act { become { case msg => println(s"Console sender recvd: $msg") } } ) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.generic.CanBuildFrom | |
import scala.collection.immutable.MapLike | |
import scala.collection.mutable | |
/** | |
* Immutable version of the PrefixMap demonstrated in the Scala collections | |
* guide here: http://www.scala-lang.org/docu/files/collections-api/collections-impl_6.html | |
* | |
* This version also has a smarter remove method (doesn't leave behind dead nodes with empty values) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Nested[A] { | |
def length(a: A): Int | |
def clone(a: A): A | |
} | |
object Nested { | |
implicit def nested[A] = new Nested[A] { | |
def length(a: A): Int = 1 | |
def clone(a: A): A = a | |
} |
NewerOlder