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
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(8, new ThreadFactory() { | |
override def newThread(r: Runnable): Thread = new Thread(Thread.currentThread().getThreadGroup, r, "MyWorker") | |
})) |
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
// Retry until it succeeds | |
class ExampleSpec extends FlatSpec with Eventually { | |
eventually { | |
futureInt.get shouldBe 1 | |
} | |
} | |
// Extended patience for integration tests | |
class ExampleSpec extends FlatSpec with Eventually with IntegrationPatience { | |
eventually { |
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 IdentifierKey(idType: String, id: String) { | |
override def toString: String = SerializedIdentifierKey(this) | |
} | |
object SerializedIdentifierKey extends (IdentifierKey => String) { | |
val delimiter = ':' | |
def apply(i: IdentifierKey): String = i.idType + SerializedIdentifierKey.delimiter + i.id | |
def unapply(s: String): Option[IdentifierKey] = { |
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 fib(n: Int): Int = { | |
@tailrec | |
def recFib(a: Int, b: Int, i: Int): Int = i match { | |
case 0 => a | |
case _ => recFib(b, a + b, i - 1) | |
} | |
recFib(0, 1, n) | |
} |
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 time[R](block: => R): R = { | |
val t0 = System.nanoTime() | |
val result = block | |
println("Elapsed time: " + (System.nanoTime - t0) + "ns") | |
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
def recursiveListFiles(file: File): Array[File] = { | |
@tailrec | |
def trImpl(dirs: Array[File], files: Array[File]): Array[File] = { | |
if (dirs.isEmpty) { | |
files | |
} | |
else { | |
val (subdirs, moreFiles) = dirs.flatMap(_.listFiles).span(_.isDirectory) | |
trImpl(subdirs, files ++ moreFiles) | |
} |
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 java.util.concurrent.ConcurrentHashMap | |
trait StringDeserializer[T] { | |
def deserialize(s: String): T | |
} | |
trait StringSerializable[T] { | |
def serialize: String | |
} |
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 java.util.Locale | |
lazy val countryNameToCode = Locale.getISOCountries.map{ countryCode => | |
(new Locale("", countryCode).getDisplayCountry, countryCode) | |
}.toMap | |
lazy val countryCodeToName = countryNameToCode.map(_.swap) | |
countryCodeToName("HK") | |
countryCodeToName("GB") | |
countryCodeToName("US") |
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 java.util.concurrent.{Executors, TimeoutException} | |
import scala.concurrent.duration._ | |
import scala.concurrent.{ExecutionContext, Future, Promise} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
def timeoutFuture[T](f: Future[T], after: Duration)(implicit ec: ExecutionContext) = { | |
val p = Promise[T]() | |
p tryCompleteWith f | |
val action = new Runnable { |
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
'.editor': | |
'ctrl-alt-l': 'pretty-json:prettify' | |
'atom-workspace atom-text-editor:not(.mini)': | |
'ctrl-d': 'editor:duplicate-lines' | |
'atom-text-editor:not(.mini)': | |
'ctrl-y': 'editor:delete-line' | |
'atom-workspace': |