Skip to content

Instantly share code, notes, and snippets.

@stonegao
stonegao / AkkaKafkaMailboxTest.scala
Created May 1, 2012 07:24 — forked from mardambey/AkkaKafkaMailboxTest.scala
Akka 2.0 actors with Kafka backed durable mailboxes.
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.agent.Agent
import com.typesafe.config.ConfigFactory
import akka.event.Logging
import akka.actor.Props
import kafka.utils.Utils
import java.nio.ByteBuffer
@jdegoes
jdegoes / DataScienceInScala.scala
Created February 8, 2013 15:11
Example code for the Creating a Data Science Platform in Scala talk.
object BenchmarkCommon {
import scala.util.Random
val DatasetSize = 10000
val Iterations = 10000
val ArrayPoolSize = 1000
val ArrayPool = {
def randomArray(): Array[Int] = {
val array = new Array[Int](DatasetSize)
@ryanlecompte
ryanlecompte / gist:5537188
Last active December 17, 2015 02:38
Pimping Iterable to have zipMany/unzipMany operations
// Usage examples:
val a = Vector(1,2,3).zipMany(Vector(1,2,3), Vector(5,6,7), Vector(8,9,10), Vector(11,12,13))
Vector(Vector(1, 1, 5, 8, 11), Vector(2, 2, 6, 9, 12), Vector(3, 3, 7, 10, 13))
a.unzipMany
Vector(Vector(1, 2, 3), Vector(1, 2, 3), Vector(5, 6, 7), Vector(8, 9, 10), Vector(11, 12, 13))
// Implementation
import scala.collection._
import scala.collection.generic.CanBuildFrom
@ryanlecompte
ryanlecompte / gist:5988194
Last active December 19, 2015 16:59
Deeply copying arbitrarily-deep nested arrays via type classes! (thanks for the typeclass idea from Erik Osheim)
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
}
@ryanlecompte
ryanlecompte / gist:7287415
Last active December 27, 2015 07:18
Immutable PrefixMap
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)
*/
@langley
langley / Find an Akka 2.0 Actor in the REPL
Last active May 7, 2017 23:53
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)
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") } } )
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

Hi, looking for scalac flags?

This gist has been upgraded to a blog post here.

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
@milessabin
milessabin / gist:9991675
Created April 5, 2014 13:01
shapeless's Typeable in action ...
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"