Skip to content

Instantly share code, notes, and snippets.

View beranradek's full-sized avatar

Radek Beran beranradek

View GitHub Profile
import Contexts.myDbOperationContext
concurrent.Future {
// Nejaky blokujici kod pristupujici k databazi
}
play {
my-db-context {
fork-join-executor {
parallelism-factor = 1.0
parallelism-max = 200
}
}
}
object Contexts {
implicit val myDbOperationContext: ExecutionContext =
Akka.system.dispatchers.lookup("my-db-context")
}
import play.api.libs.concurrent.Execution.Implicits._
def someAsyncAction = Action {
Async {
WS.get("http://www.someservice.com").get().map {
response =>
Ok("Response code: " + response.status)
}
}
}
package dojo
import java.net.URL
import java.io.File
import scala.io.Source
import java.io.InputStream
import scala.util.Try
import scala.util.Success
trait Resource[R] {
case class Person(firstName: String, lastName: String, male: Boolean)
val people = List(
Person("Veronika", "Skalova", false),
Person("Jana", "Heroutova", false),
Person("Miroslav", "Klepis", true))
for (Person(_, lastName, false) <- people) yield "Pani " + lastName
// Pani Skalova, Pani Heroutova
val myTuple = (1, 2)
val (x, y) = myTuple // (x, y) je constructor pattern pro tuple
println(x) // 1
val addOperation = Add(Number(1), Add(Number(2), Number(3)))
val Add(Number(_), secondOperand @ Add(_, b)) = addOperation
// Add(Number(_), secondOperand @ Add(_, b)) je constructor pattern
// s vnořenými constructor patterny pro první a druhý parametr,
// b je matchováno variable patternem a match. hodnota je do něj uložena
println(b) // Number(3)
// Pattern matching for defining partial functions
val numToRoman: PartialFunction[Int, String] = {
case 4 => "IV"
case 5 => "V"
case 6 => "VI"
}
println(numToRoman.isDefinedAt(7)) // false
try {
processFile("input.txt")
} catch {
case ex: FileNotFoundException => println("Missing file exception: " +
ex.getMessage())
case ex: IOException => println("IO exception: " + ex.getMessage())
}
val n: Any = -20
n match {
case i : Int if (i > 0) => "positive int"
case s : String if (s.length() > 0) => "non-empty string"
}