Skip to content

Instantly share code, notes, and snippets.

View alexandru's full-sized avatar
😺
Having fun

Alexandru Nedelcu alexandru

😺
Having fun
View GitHub Profile
package different.future
import concurrent.ExecutionContext
import scala.util.Try
/**
* Just a tuple formed from a callback and an ExecutionContext.
*
* @param callback is the function called whenever we've got the value to signal
* @param context is the thread-pool under which our tasks are running

The context:

First of all I must say that the Scheduler in Akka, while useful for Akka's purposes, it isn't adequate as is currently to be exposed as part of Scala's standard library. As part of a project I've been working on, I have defined a Scheduler interface that inherits from ExecutionContext (it's an enhanced execution context) that has the following methods and I believe that all of them are needed:

@alexandru
alexandru / task-proposal.md
Last active April 1, 2018 14:57
Task: A diverging design from Future and Scalaz Task
import scalaz.concurrent.Task
def benchmark[T](repeat: Int)(callback: => T): Unit = {
var lastResult: Any = null
var total = Duration.Zero
for (i <- 0 until repeat) {
val time = System.nanoTime()
val result = callback
val measured = (System.nanoTime() - time).nanos
@alexandru
alexandru / WSDLPlugin.scala
Created January 6, 2016 12:58
For generating classes from WSDL specifications using Apache CXF
import sbt._
import sbt.classpath.ClasspathUtilities
import sbt.Keys._
import java.io.File
import scala.util.control.NonFatal
object WDSLPlugin extends Plugin {
object cxf {
val Config = config("cxf").hide
val wsdl2java = TaskKey[Seq[File]]("wsdl2java", "Generates java files from wsdls")
import scala.concurrent.duration.FiniteDuration
import monix.streams._
def throttleX[T](source: Observable[T], timespan: FiniteDuration): Observable[T] = {
val timespanMillis = timespan.toMillis
sealed trait State
case object StartOrDebounce extends State
case class Emit(startSignal: T, delayUntil: Long) extends State
case class Delay(lastSignal: T, delayUntil: Long) extends State

Immutable values are facts, events that happened, snapshots. Manipulating immutable values by means of pure functions is a deterministic process, meaning that for the same input you always get the same output. But when you introduce time in the picture, now you have streams of events that happen and concurrency and race-conditions.

Non-determinism basically means that you can't rely on the ordering of events. And by not having an order, all bets are off.

sealed trait JsValue
case class JsNumber(v: Double) extends JsValue
case class JsBool(v: Boolean) extends JsValue
case class JsString(v: String) extends JsValue
case class JsObject(map: Map[String, JsValue]) extends JsValue
case class JsArray(list: Seq[JsValue]) extends JsValue
case object JsNull extends JsValue
def convert(map: Map[String, Any]): JsObject = {
import monix.execution._
import monix.execution.RunLoop.FrameId
def fibbonacciWithMacros(iterations: Int, callback: BigInt => Unit)
(implicit s: Scheduler): Unit = {
def loop(idx: Int, a: BigInt, b: BigInt, frameId: FrameId): Unit =
if (idx >= iterations) callback(b) else
RunLoop.step(frameId) { frameId => loop(idx+1, b, a+b, frameId) }
/** Describes the internal state of `withStateFrom`. */
sealed trait WithLatestFrom[+A,+B]
extends Product with Serializable
object WithLatestFrom {
/** Combines each element emitted by `source` with the latest
* element emitted by `b`.
*
* See:
*