Skip to content

Instantly share code, notes, and snippets.

@nelanka
nelanka / namedThreadPool.scala
Created August 25, 2015 14:08
Named Thread Pool
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(8, new ThreadFactory() {
override def newThread(r: Runnable): Thread = new Thread(Thread.currentThread().getThreadGroup, r, "MyWorker")
}))
@nelanka
nelanka / scalaTest.scala
Last active August 29, 2015 14:27
Scala Test Tips
// 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 {
@nelanka
nelanka / SerializedIdentifierKey.scala
Last active August 29, 2015 14:27
Serialize and deserialize case classes with helper object
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] = {
@nelanka
nelanka / fibTailRec.scala
Created July 31, 2015 14:44
Tail Recursive Fibonacci
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)
}
@nelanka
nelanka / time.scala
Created May 12, 2015 19:08
Time a function in Scala
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block
println("Elapsed time: " + (System.nanoTime - t0) + "ns")
result
}
@nelanka
nelanka / recursiveListFiles.scala
Last active August 29, 2015 14:20
List files in a folder tail recursively
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)
}
@nelanka
nelanka / SerializedCache.scala
Last active August 29, 2015 14:20
Proxy a cache that only uses String key and values
import java.util.concurrent.ConcurrentHashMap
trait StringDeserializer[T] {
def deserialize(s: String): T
}
trait StringSerializable[T] {
def serialize: String
}
@nelanka
nelanka / countryCodes.scala
Last active August 29, 2015 14:19
Country Codes
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")
@nelanka
nelanka / timeoutFuture.scala
Created February 28, 2015 12:45
Timeout Future
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 {
@nelanka
nelanka / keymap.cson
Last active August 29, 2015 14:16
Atom Key Bindings
'.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':