Skip to content

Instantly share code, notes, and snippets.

object StreamDeadlock extends App {
import akka.actor._
import akka.stream._
import akka.stream.scaladsl._
import scala.concurrent.Await
implicit val system = ActorSystem()
val noFusingMaterializer = ActorMaterializer(ActorMaterializerSettings(system).withAutoFusing(false))
val fusingMaterializer = ActorMaterializer(ActorMaterializerSettings(system).withAutoFusing(true))
package com.strikead.navarro
import java.util.concurrent.TimeUnit
import ch.qos.logback.classic.{Level, Logger}
import com.codahale.metrics.{Timer, MetricRegistry}
import com.ning.http.client.providers.netty.NettyAsyncHttpProviderConfig
import org.jboss.netty.util.HashedWheelTimer
import org.slf4j.LoggerFactory
import play.api.libs.ws.WSClientConfig
trait TypedKey[V] {
type Value = V
}
object TypedKey {
def apply[V]: TypedKey[V] = new TypedKey[V] {}
}
class TypedMap private (val m: Map[TypedKey[_], Any]) {
def put [K <: TypedKey[_]](k: K)(v: k.Value) = new TypedMap(m + (k -> v))
def get[K <: TypedKey[_]](k: K): Option[k.Value] = m.get(k).asInstanceOf[Option[k.Value]]
def processBatches[T](input: Enumerator[T], parallelism: Int)
(process: T => Future[Unit])
(implicit ec: ExecutionContext): Future[Unit] = {
input.through(Enumeratee.map(process) compose chunker(parallelism)).run(Iteratee.foldM(()) {
case (_, results) => Future.sequence(results) map (_ => ())
})
}
def chunker[T](size: Int): Enumeratee[T, List[T]] =
Enumeratee.grouped(Enumeratee.take(size).transform(Iteratee.getChunks[T]))
object DependentReads extends App {
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._
val json1 = Json.parse( """ { "type": "foo", "foo" : { "x" : 1 }, "bar" : { "y" : 2 } } """)
val json2 = Json.parse( """ { "type": "bar", "foo" : { "x" : 1 }, "bar" : { "y" : 2 } } """)
val typ = (__ \ 'type).read[String]
@alexeevg
alexeevg / enumGrouped.scala
Created June 18, 2014 13:05
Enumeratee.grouped example
object EnumExample extends App {
import play.api.libs.iteratee.{Enumeratee, Enumerator, Iteratee}
import scala.concurrent.ExecutionContext.Implicits.global
val src: Enumerator[Int] = Enumerator(1 to 10 : _*)
val chunker: Enumeratee[Int, List[Int]] = Enumeratee.grouped(Enumeratee.take(3).transform(Iteratee.getChunks[Int]))
val f = src.through(chunker).run(Iteratee.foreach {
chunk: List[Int] => println(chunk)
import Syntax.PreorderReasoning
lists4AppendAssoc: (a: List t) -> (b: List t) -> (c: List t) -> (d: List t) -> (a ++ (b ++ (c ++ d))) = (((a ++ b) ++ c) ++ d)
lists4AppendAssoc a b c d =
(a ++ (b ++ (c ++ d))) ={ appendAssociative a b (c ++ d) }=
((a ++ b) ++ (c ++ d)) ={ appendAssociative (a ++ b) c d }=
(((a ++ b) ++ c) ++ d) QED
-- compare to tactic-based proof: