Skip to content

Instantly share code, notes, and snippets.

View optician's full-sized avatar
🦉

Danila Matveev optician

🦉
  • Yerevan, Armenia
View GitHub Profile
@optician
optician / CommandsByMatch
Created January 24, 2015 04:56
One entry point for commands. "Match" variant.
class WebSocketActor(out: ActorRef) extends Actor {
def receive = {
case msg: JsValue => distributeCommands(msg)
}
def distributeCommands(req: JsValue): Unit = {
(req \ "command").asOpt[String] match {
case Some("get all accounts") => mock.getAllAccounts pipeTo out
case Some("add account") => mock.addAccount pipeTo out
case Event(newConnection: ActorRef, FSMData.Datum(challengeRef, channels, state)) =>
context.watch(newConnection)
newConnection ! CurrentState(stateName).json
stay() using FSMData.Datum(challengeRef, channels + newConnection, state)
@optician
optician / resj.scala
Created March 25, 2015 19:51
Read and extract stats from jmeter.log
val f = scala.io.Source.fromFile("jm.log")
def trunk(value: Double): Double = BigDecimal(value).setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble
def percentile(ms: Map[Long, Long], amount: Long, lvls: List[Int]): List[Long] = {
val ls = ms.toList.sortBy(x => x._1)
val lvlValues = lvls map (lvl => Math.ceil(amount*lvl/100f).toLong)
def search(a: Long, xs: List[(Long, Long)], lvlValue: Long): Long = xs match {
case v::xs if a+v._2 < lvlValue => search(a+v._2, xs, lvlValue)
case v::xs => v._1
val f = scala.io.Source.fromFile("jm.log")
def trunk(value: Double): Double = BigDecimal(value).setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble
def percentile(ms: Map[Long, Long], amount: Long, lvls: List[Int]): List[Long] = {
val ls = ms.toList.sortBy(x => x._1)
val lvlValues = lvls map (lvl => Math.ceil(amount*lvl/100f).toLong)
def search(a: Long, xs: List[(Long, Long)], lvlValue: Long): Long = xs match {
case v::xs if a+v._2 < lvlValue => search(a+v._2, xs, lvlValue)
case v::xs => v._1
@optician
optician / Main.scala
Last active August 29, 2015 14:26
Akka-http client sample
(Source.single(request -> 42) via Http().superPool() map {
case (Success(res), flag) => res.entity.toStrict(1 seconds).map(x => new String(x.data.toArray))
case (Failure(err), flag) => Future.failed(err)
}).mapAsyncUnordered(2)(???)
import org.scalameter._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
object Main {
/*
import pushka.json._
import pushka.annotation._
// First case.
// Simple as is.
@pushka
case class A(subModel: B, i: Int, str: String)
@pushka
case class B(field1: Int, field2: List[Int])
@optician
optician / gist:1f620045ad6ee4fce634b4f0e64fb27e
Created December 11, 2016 11:32 — forked from debasishg/gist:8172796
A collection of links for streaming algorithms and data structures
  1. General Background and Overview
@optician
optician / py-vs-haskell.scala
Created October 23, 2017 06:53
scala/java version. About 11sec
import scala.io._
import java.io._
import scala.collection.mutable.ArrayBuffer
object Main {
def main(args: Array[String]): Unit = {
var i = 0
var v1: ArrayBuffer[String] = new ArrayBuffer(10000)
var v2: ArrayBuffer[String] = new ArrayBuffer(10000)
Source.stdin.getLines.foreach{
@optician
optician / scalac.scala
Created March 23, 2018 08:10
PartialFunction.scala
class A{
val x: PartialFunction[Any, Int] = {
case Some(x: Int) => x
}
}