Skip to content

Instantly share code, notes, and snippets.

View leandrob13's full-sized avatar

Leandro Bolivar leandrob13

View GitHub Profile
@leandrob13
leandrob13 / EncoderDerivation.scala
Created January 5, 2020 22:39
Encoder derivation functions for xml Encoder[T] typeclass
import scala.compiletime.{erasedValue, summonFrom, constValue}
import deriving._
import scala.xml._
trait EncoderDerivation {
inline def typeClass[T](x: T, label: String, format: TagFormat): NodeSeq = summonFrom {
case s: Encoder[T] => s.toXml(x, label, format)
}
@leandrob13
leandrob13 / FutureOps.java
Created December 17, 2019 16:47
Example of sequence and traverse for java's CompletableFuture.
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
public class FutureOps {
public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> ls) {
return ls.stream()
.reduce(
test("TaskLocal.bind cleanup without async boundary") {
import scala.concurrent.duration.Duration
val local: TaskLocal[Boolean] = TaskLocal[Boolean](false).runSyncUnsafe(Duration.Inf)
def loop(n: Int = 1000): Task[Unit] =
if (n > 0) attempt.flatMap(_ => loop(n - 1))
else Task.unit
import minitest.SimpleTestSuite
import monix.execution.Scheduler
object TaskLocalSuite extends SimpleTestSuite {
implicit val ec: Scheduler = monix.execution.Scheduler.Implicits.global
implicit val opts = Task.defaultOptions.enableLocalContextPropagation
testAsync("TaskLocal.bind clears local with no async boundary") {
val local = TaskLocal(0)
import minitest.SimpleTestSuite
import monix.execution.Scheduler
object TaskLocalSuite extends SimpleTestSuite {
implicit val ec: Scheduler = monix.execution.Scheduler.Implicits.global
implicit val opts = Task.defaultOptions.enableLocalContextPropagation
testAsync("Local.apply") {
val test =
for {
import monix.eval.Task
import monix.execution.Scheduler
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{Inside, MustMatchers, WordSpec}
import scala.concurrent.Future
class TracingIdsTest extends WordSpec with MustMatchers with ScalaFutures with Inside {
import monix.eval.Task
import monix.execution.{CancelableFuture, Scheduler}
case class TracingIds(ids: Map[String, String]) extends TracingContext {
import TracingIds._
def asCurrent[T](t: Task[T]): Task[T] =
TracingIds.bind(this)(t)
def execute[T](t: Task[T])(implicit sch: Scheduler, opt: Task.Options): CancelableFuture[T] = {
import monix.eval.{Task, TaskLocal}
import monix.execution.{CancelableFuture, Scheduler}
trait TracingContext {
def asCurrent[T](t: Task[T]): Task[T]
def execute[T](t: Task[T])(implicit sch: Scheduler, opt: Task.Options): CancelableFuture[T]
}

I have been doing some follow up to TaskLocal and I have put together some tests to illustrate some of the findings.

TaskLocal.bind looks like this:

  def bind[R](value: A)(task: Task[R]): Task[R] =
    Task.suspend {
      val saved = ref.value
      ref.update(value)
     // the cleanup occurs in the thread where the task executes.

Keybase proof

I hereby claim:

  • I am leandrob13 on github.
  • I am leandrob13 (https://keybase.io/leandrob13) on keybase.
  • I have a public key ASAcSFjE2RJe6nAChUDEdAQGQVC1ART7TD7aAi5zCqeOJwo

To claim this, I am signing this object: