This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.Scheduler | |
import scala.concurrent.duration.FiniteDuration | |
import scala.concurrent.{ExecutionContext, Future, Promise} | |
object TimeoutFuture { | |
private class CancellableExecution[T](body: => T) extends Function[Unit, T] { | |
private val runningThread = new AtomicReference[Thread](null) | |
private val isCancelled = new AtomicBoolean(false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.Scheduler | |
import scala.concurrent.duration.FiniteDuration | |
import scala.concurrent.{ExecutionContext, Future, Promise} | |
object DelayedFuture { | |
def apply[T](scheduler: Scheduler, delay: FiniteDuration)(body: => T)(implicit ec: ExecutionContext): Future[T] = { | |
val delayPromise = Promise[Unit]() | |
scheduler.scheduleOnce(delay)(delayPromise.success(())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Ema { | |
def apply(period: Int)(data: Vector[Double]): Vector[Double] = { | |
if (period < 1) throw new IllegalArgumentException(s"Period ($period) is smaller than 1") | |
if (period > data.size) throw new IllegalArgumentException(s"Period ($period) is bigger than data size (${data.size})") | |
val factor = 2d / (period + 1) | |
data.iterator | |
.drop(1) | |
.scanLeft(data.head) { (prevEma, value) => value * factor + (1d - factor) * prevEma } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final case class InformationSize(size: Double, unit: InformationUnit) extends Ordered[InformationSize] { | |
import InformationSize.{safeAdd, safeDivide, safeMultiply} | |
import InformationUnit._ | |
if (size < 0d) throw new IllegalArgumentException(s"Unsupported negative size: $size") | |
def toBits: Double = to(Bit) | |
def toKiloBits: Double = to(KiloBit) | |
def toMegaBits: Double = to(MegaBit) | |
def toBytes: Double = to(Byte) |