Skip to content

Instantly share code, notes, and snippets.

Pierre Andrews Mortimerp9

Block or report user

Report or block Mortimerp9

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@milessabin
milessabin / gist:aae285025a32fac0f5c1
Last active Aug 26, 2017
Trivial type safe heterogenous map using only dependent method types, singleton-typed String literal keys and implicits.
View gist:aae285025a32fac0f5c1
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}
@Mortimerp9
Mortimerp9 / RefreshEvery.scala
Created May 2, 2014
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.
View RefreshEvery.scala
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) {
@allenday
allenday / scalding_cos_cor.scala
Last active Aug 29, 2015
Scalding cosine and correlation coefficient
View scalding_cos_cor.scala
// 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) {
@milessabin
milessabin / gist:9991675
Created Apr 5, 2014
shapeless's Typeable in action ...
View gist:9991675
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"
View gist:9969589
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
View gist:8812750

Hi, looking for scalac flags?

This gist has been upgraded to a blog post here.

View MeasureUnit.scala
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
@langley
langley / Find an Akka 2.0 Actor in the REPL
Last active May 7, 2017
Determine whether an akka actor exists or not in the repl. Play 2.2.1 implies Akka 2.2.0 Using this trick means you don't need to maintain your own list of existing actors. However you'll need to actually use it from w/in an actor (notice that the the answer is a message)
View Find an Akka 2.0 Actor in the REPL
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") } } )
@ryanlecompte
ryanlecompte / gist:7287415
Last active Dec 27, 2015
Immutable PrefixMap
View gist:7287415
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)
*/
@ryanlecompte
ryanlecompte / gist:5988194
Last active Dec 19, 2015
Deeply copying arbitrarily-deep nested arrays via type classes! (thanks for the typeclass idea from Erik Osheim)
View gist:5988194
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
}
You can’t perform that action at this time.