Giter Club home page Giter Club logo

fibers's Introduction

Fibers for Scala

This library implements fibers (or coroutines) on top of delimited continuations as provided by the Scala CPS compiler plugin.

Usage

In your build.sbt file:

// Enable CPS plugin
addCompilerPlugin("org.scala-lang.plugins" % "scala-continuations-plugin_2.12.2" % "1.0.3"),
libraryDependencies += "org.scala-lang.plugins" %% "scala-continuations-library" % "1.0.3",
scalacOptions += "-P:continuations:enable",

// Depend on fibers-core
resolvers += Resolver.bintrayRepo("mulk", "maven")
libraryDependencies ++= Seq(
  "eu.mulk" %% "fibers-core" % "0.1.0",
)

Examples

Awaiting futures and tasks and emitting values:

import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
import monix.reactive.Observable

import eu.mulk.fibers.Fiber._

import scala.util.Success

val slowBackgroundTask = Task.delay(100)

def produceNumbers: Unit @fiber[Int] = {
  val Success(x) = await(slowBackgroundTask)
  emit(x)
  emit(x*2)
  emit(x*3)
}

val observable: Observable[Int] = run(produceNumbers)
observable.foreachL(println).runAsync  // => 100, 200, 300

Using fiber-local state:

import monix.eval.Task
import monix.execution.Scheduler.Implicits.global
import monix.reactive.Observable

import eu.mulk.fibers.Fiber._

def emitFiberVar: Unit @fiber[Int] = {
  emit(getFiberVar)
}

def produceNumbers: Unit @fiber[Int] = {
  putFiberVar[Int](100)
  emitFiberVar
}

val observable: Observable[Int] = run(produceNumbers)
observable.foreachL(println).runAsync  // => 100

For more examples, see the unit tests.

fibers's People

Contributors

benkard avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.