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
// GAME ENGINE | |
trait View[A] { | |
protected def update(before: A, after: A): Unit | |
def onModelChange(before: A, after: A): Unit = | |
if(before != after) { | |
update(before, after) | |
} |
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
val snakes = Map( | |
16 -> 6, | |
47 -> 26, | |
49 -> 11, | |
56 -> 53, | |
62 -> 18, | |
64 -> 60, | |
87 -> 24, | |
92 -> 72, | |
95 -> 75, |
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.io.Source | |
val reverseLetters = ( ('a' to 'z') zip ('a' to 'z').reverse ).toMap.withDefault(identity) | |
val words = Source.fromFile("/usr/share/dict/words").getLines.toVector | |
def isWordMatch(word: String) = { | |
val reverseLetterWord = word.toLowerCase.map(reverseLetters) | |
reverseLetterWord.reverse == word | |
} |
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.netaporter.precanned.dsl.fancy._ | |
val animalApi = httpServerMock(system).bind(8766) | |
animalApi expect | |
get and path("/animals") and query("name" -> "giraffe") and | |
respond using | |
resource("/responses/giraffe.json") end() |
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.ActorSystem | |
import akka.testkit.{TestActorRef, EventFilter, TestKit} | |
import org.scalatest.{OneInstancePerTest, BeforeAndAfterAll, WordSpecLike} | |
class PassingTest | |
extends TestKit(ActorSystem("MyActorTest")) | |
with WordSpecLike | |
with BeforeAndAfterAll | |
with OneInstancePerTest { |
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.ActorSystem | |
import akka.testkit.{TestActorRef, EventFilter, TestKit} | |
import org.scalatest.{BeforeAndAfterAll, WordSpecLike} | |
class FailingTest | |
extends TestKit(ActorSystem("MyActorTest")) | |
with WordSpecLike | |
with BeforeAndAfterAll { | |
override def afterAll() { |
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.{ActorLogging, Actor} | |
import scala.concurrent.duration._ | |
class MyTestActor extends Actor with ActorLogging { | |
import context.dispatcher | |
def receive = { | |
case 'Crash => scheduler.scheduleOnce(100.millis)(log.error("Crash")) | |
} | |
def scheduler = context.system.scheduler | |
} |
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
f recoverWith { | |
case _: TimeoutException ⇒ | |
log.error("Alerts API took too long") | |
Future.failed(new TimeoutException("Alerts API Timeout")) | |
} |
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
f recoverWith { | |
case _: TimeoutException ⇒ | |
Future.failed(new TimeoutException("Alerts API Timeout")) | |
} |
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
//Experiment trying to mimic the java syntax | |
// isTrue ? thing: otherThing | |
//NOTE: This is just for proving a point about scala's great expressivness. | |
// Don't use this for anything real. In real code use if(isTrue) thing else otherThing | |
class QuestionWithSuccess[T](cond: Boolean, success: T) { def |(fail: T) = if(cond) success else fail } | |
class Question(cond: Boolean) { def ?[T](t: T) = new QuestionWithSuccess(cond, t) } | |
implicit def boolToQuestion(b: Boolean) = new Question(b) | |
true ? "ham" | "cheese" |
NewerOlder