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 ddl-parser | |
| (insta/parser | |
| "ddl = (createtable* (!createtable <any>)*)+ | |
| <any> = #'\\S+' | |
| createtable = 'CREATE TABLE' table cols | |
| cols = <'('> columns <','> constraint <');'> | |
| table = identifier | |
| identifier = #'[a-zA-Z_0-9\\.]+' | |
| columns = column (',' column)* | |
| column = name type nullable? |
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
| (defprotocol PStack | |
| "A stack protocol" | |
| (push [this val] "Push element in") | |
| (pop* [this] "Pop element from stack") | |
| (top [this] "Get top element from stack")) | |
| (defrecord FStack [coll] | |
| PStack | |
| (push [_ val] | |
| "Return the stack with the new element inserted" |
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
| trait A { | |
| protected def f : Boolean | |
| } | |
| object A extends A { | |
| protected def f : Boolean = true | |
| } | |
| object B extends A { |
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
| class LoggerLikeStub extends LoggerLike { | |
| import scala.collection.mutable.Buffer | |
| val logMessages: Buffer[String] = Buffer() | |
| override val logger: Logger = null | |
| override def warn(msg: => String) = { | |
| logMessages += msg |
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._ | |
| import scala.concurrent.duration._ | |
| import scala.concurrent.ExecutionContext.Implicits.global | |
| def runWithTimeout[T](timeoutMs: Long)(f: => Future[T]) : T = { | |
| Await.result(f, timeoutMs.millis) | |
| } | |
| runWithTimeout(50) { Future { "result" } } |
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
| class Bar(i: Int) { | |
| val lst = List.empty[Int] | |
| def copy(i: Int = i, lst: List[Int] = lst) = { | |
| val b = new Bar(i = i) | |
| b.lst = lst | |
| } | |
| } |
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
| class Foo { | |
| val quux = "quux" | |
| def bar(baz: () => Unit): Foo = { | |
| baz() | |
| this | |
| } | |
| } | |
| // I would like to write in fluent style like this but I can't reference the | |
| // previous object in the chain |
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
| sealed trait A | |
| case class B() extends A | |
| case class C() extends A | |
| val xs = List(B(), B(), C()) | |
| xs.groupBy(_.getClass) |
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 org.scalacheck.Arbitrary | |
| import org.scalacheck.Arbitrary._ | |
| import org.scalacheck.Gen | |
| def arbitraryCaseClass[A,C](f: A => C)(implicit t: Arbitrary[A]): Arbitrary[C] = Arbitrary(for(v <- arbitrary[A]) yield f(v)) | |
| type MyIdType = String | |
| implicit val arbMyIdType: Arbitrary[MyIdType] = Arbitrary(Gen.identifier) |
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 play.api.libs.json.{Format, JsResult, JsValue, Json} | |
| import shapeless.tag.@@ | |
| trait Money | |
| case class Amount(price: String @@ Money) | |
| private val amountWrites = Json.writes[Amount] | |
| private val amountReads = Json.reads[Amount] |