Skip to content

Instantly share code, notes, and snippets.

View theon's full-sized avatar

Ian Forsey theon

View GitHub Profile
// 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)
}
@theon
theon / snakes_and_ladders.scala
Created April 8, 2019 19:27
My son's copy of Snakes and Ladders. Looks like worse case game would be 480ish turns XD
val snakes = Map(
16 -> 6,
47 -> 26,
49 -> 11,
56 -> 53,
62 -> 18,
64 -> 60,
87 -> 24,
92 -> 72,
95 -> 75,
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
}
@theon
theon / PrecannedExample.scala
Created January 28, 2014 11:36
Pre-canned example
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()
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 {
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() {
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
}
f recoverWith {
case _: TimeoutException ⇒
log.error("Alerts API took too long")
Future.failed(new TimeoutException("Alerts API Timeout"))
}
f recoverWith {
case _: TimeoutException ⇒
Future.failed(new TimeoutException("Alerts API Timeout"))
}
//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"