This file contains hidden or 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
// The TestKit class is just: | |
class TestKit(_system: ActorSystem) extends { implicit val system = _system } with TestKitBase | |
// TestKitBase uses `system` in it's constructor so _it must be defined before extending TestKitBase_! | |
// You can't use `this` within alternate constructors however, so the solution isn't as easy as: | |
class MySpec extends TestKit(ActorSystem(this.getClass.getSimpleName)) | |
// So let's ignore `TestKit` and focus on `TestKitBase`. That way we can have our own constructors that execute before | |
// TestKitBase's constructor. |
This file contains hidden or 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.concurrent.ExecutionContext.Implicits.global | |
import scala.concurrent.Future | |
val a = Future { 1 } // completes immediately. | |
val b = Future { Thread.sleep(1000); 2 } // is the last to complete. | |
val c = Future { Thread.sleep(100); 3 } // completes after 1 but before 2. | |
Future.sequence(Seq(a, b, c)) map println | |
// > List(1, 2, 3) |
This file contains hidden or 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
def resize(current: Long, target: Long, iteration: Long = 1): Unit = { | |
val step = Math.max(current - (current / 7), target) | |
printf("[%04d] RESIZE FROM %d TO %d\n", iteration, current, step) | |
if(step > target) | |
resize(step, target, iteration + 1) | |
} |
This file contains hidden or 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
// This code won't compile without some Play dependencies being satisfied. | |
// Like Controller, formats.parsing, etc. It's just for reference. | |
object ArticlesController extends Controller { | |
import java.util.Locale | |
// Our application has several translations for an Article: | |
val locales: List[Locale] = Seq(Locale.US, Locale.JAPAN) | |
def create = Action { |
This file contains hidden or 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
object Main extends testing.Benchmark { | |
val r = """[\r\n\s]""".r | |
val s = """ | |
| Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
| tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim | |
| veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea | |
| commodo consequat. Duis aute irure dolor in reprehenderit in voluptate | |
| velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat |
This file contains hidden or 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
object MyController extends Controller with StrictLogging { | |
def index = | |
AdminAction.async { implicit request => | |
request.method match { | |
case "GET" => | |
Future.successful(Ok("GET")) | |
case "POST" => |
This file contains hidden or 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
object Lucene { | |
object Escape extends (String => String) { | |
private val prefix: Char => PartialFunction[Char, String] = c => { case `c` => s"\\$c" } | |
private val escapist: PartialFunction[Char, String] = prefix('\\') orElse | |
prefix('+') orElse | |
prefix('-') orElse | |
prefix('!') orElse |
This file contains hidden or 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
// It seems like Props is a closure that gets reused? The first pass will work. | |
// The next will cause the BatchUpdate Actor to send deadLetters. As if the sender | |
// terminated prematurely. | |
query { | |
Rows(multiStoryQuery(revs.map(_._1)).list) | |
} pipeTo context.actorOf(Props(new BatchUpdate(context.sender(), revs)), s"batch-update-story-${Math.abs(Random.nextInt())}") | |
// This works however. So the problem actually seems to be that successive iterations of the above | |
// version go to the same (now stopped) passed sender. |
This file contains hidden or 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
// To Override or Not To Override (that is the question): | |
trait Foo { | |
def close: Unit | |
def bar(baz: String): Int | |
def zed(y: Int): Int = y * 2 | |
} |
This file contains hidden or 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
case class Foo(a: String, b: Int) | |
object Foo { | |
def apply(a: String, b: String) = new Foo(a, b.toInt) | |
} | |
val format = jsonFormat2[Foo](new { | |
def apply(a: String, b: Int): Foo = Foo(a, b) | |
def unapply(f: Foo) = Foo.unapply(f) |