Get http headers with akka routing dsl

I’m new to Scala, and this problem frustrates me. How can I get all headers from request? val route = { path(“lol”) { //get httpHeaders complete(HttpResponse()) } } Answer You have at least two options here: a) Using extractRequest directive: val route = { path(“example”) { extractRequest { request => request.headers // Returns `Seq[HttpHeader]`; do … Read more

Split Akka Stream Source into two

I have an Akka Streams Source which I want to split into two sources according to a predicate. E.g. having a source (types are simplified intentionally): val source: Source[Either[Throwable, String], NotUsed] = ??? And two methods: def handleSuccess(source: Source[String, NotUsed]): Future[Unit] = ??? def handleFailure(source: Source[Throwable, NotUsed]): Future[Unit] = ??? I would like to be … Read more

‘val’ or ‘var’, mutable or immutable?

I can define a variable (by var) that is immutable: var x = scala.collection.immutable.Set(“aaaaaa”,”bbbbbb”) println(x.isInstanceOf[scala.collection.immutable.Set[String]]) x += “cccc” println(x.isInstanceOf[scala.collection.immutable.Set[String]]) This results in: true true += method is not a member of scala.collection.immutable.Set, so what is happening? Answer The compiler looks for x.+= …, and if it can’t find it, then it tries to transform the … Read more

How to schedule an hourly job with Play Framework 2.1?

In Play 1 it was simply: @Every(value = “1h”) public class WebsiteStatusReporter extends Job { @Override public void doJob() throws Exception { // do something } } What is the equivalent for Play 2.1? I know that Play uses akka and I found this code sample: import play.api.libs.concurrent.Execution.Implicits._ Akka.system.scheduler.schedule(0.seconds, 30.minutes, testActor, “tick”) But being a … Read more

Scala – Combine two lists in an alternating fashion

How do I merge 2 lists in such a way that the resulting list contains the elements of 2 lists in alternating fashion in Scala. Input: val list1 = List(“Mary”, “a”, “lamb”) val list2 = List(“had”, “little”) Output: List(“Mary”, “had”, “a”, “little”, “lamb”) Answer What you’re looking for is usually called “intersperse” or “intercalate” and … Read more