All Projects → Verizon → Delorean

Verizon / Delorean

Licence: apache-2.0
Convert Task to Future, and Future to Task

Programming Languages

scala
5932 projects
scalaz
18 projects

Projects that are alternatives of or similar to Delorean

Structured Text Tools
A list of command line tools for manipulating structured text data
Stars: ✭ 6,180 (+9709.52%)
Mutual labels:  conversion
Csprojtovs2017
Tooling for converting pre 2017 project to the new Visual Studio 2017 format.
Stars: ✭ 876 (+1290.48%)
Mutual labels:  conversion
Vchsm
C++ 11 algorithm implementation for voice conversion using harmonic plus stochastic models
Stars: ✭ 38 (-39.68%)
Mutual labels:  conversion
Vim Pandoc
pandoc integration and utilities for vim
Stars: ✭ 734 (+1065.08%)
Mutual labels:  conversion
Unicopy
Unicode command-line codepoint dumper
Stars: ✭ 16 (-74.6%)
Mutual labels:  conversion
Bib Rdf Pipeline
Scripts and configuration for converting MARC bibliographic records into RDF
Stars: ✭ 27 (-57.14%)
Mutual labels:  conversion
Alfred Convert
Convert between different units in Alfred
Stars: ✭ 560 (+788.89%)
Mutual labels:  conversion
Ut4x Converter
Helps converting old Unreal Tournament maps to new Unreal Tournament 4 game.
Stars: ✭ 46 (-26.98%)
Mutual labels:  conversion
Convertpcltocore
17 Steps to Convert your PCL to .NET Standard
Stars: ✭ 10 (-84.13%)
Mutual labels:  conversion
Cryptii
Web app and framework offering modular conversion, encoding and encryption
Stars: ✭ 971 (+1441.27%)
Mutual labels:  conversion
Docconv
Converts PDF, DOC, DOCX, XML, HTML, RTF, etc to plain text
Stars: ✭ 735 (+1066.67%)
Mutual labels:  conversion
Simple Java Mail
Simple API, Complex Emails (JavaMail smtp wrapper)
Stars: ✭ 821 (+1203.17%)
Mutual labels:  conversion
Ssp
C++ CSV parser
Stars: ✭ 30 (-52.38%)
Mutual labels:  conversion
Tslint To Eslint Config
Converts your TSLint configuration to the closest possible ESLint equivalent. 🚀
Stars: ✭ 680 (+979.37%)
Mutual labels:  conversion
0xc
Easy base conversion in emacs
Stars: ✭ 42 (-33.33%)
Mutual labels:  conversion
Unitsnet
Makes life working with units of measurement just a little bit better.
Stars: ✭ 641 (+917.46%)
Mutual labels:  conversion
Chatify
A Laravel package that allows you to add a complete user messaging system into your new/existing Laravel application.
Stars: ✭ 885 (+1304.76%)
Mutual labels:  conversion
Normalize country
Convert country names and codes to a standard.
Stars: ✭ 60 (-4.76%)
Mutual labels:  conversion
Darknet2caffe
Convert Darknet model to Caffe's
Stars: ✭ 46 (-26.98%)
Mutual labels:  conversion
Units Converter
A simple utility library to measure and convert between units
Stars: ✭ 31 (-50.79%)
Mutual labels:  conversion

Delorean

image

Build Status Maven Central

Trivial library for correct and stable conversions between Task and Future. To use:

For scalaz-7.1.x:

libraryDependencies += "io.verizon.delorean" %% "core" % "1.2.40-scalaz-7.1"

For scalaz-7.2.x:

libraryDependencies += "io.verizon.delorean" %% "core" % "1.2.40-scalaz-7.2"

scalaz-7.0.x is not supported.

Cross builds are available for 2.10.x, 2.11.x and for 2.12.x

Usage

Keep it simple:

import delorean._       // conversions are in the package object

val f: Future[Foo] = ...
val t: Task[Foo] = f.toTask
val f2: Future[Foo] = t.unsafeToFuture

As a general rule of thumb:

def toTask[A](f: Future[A]) = Task { Await.result(f, Duration.Inf) }
def unsafeToFuture[A](t: Task[A]) = Future { t.run }

Obviously, the above is misleading since the definitions of these functions are non-blocking, but this should give you an idea of their rough semantics.

There are a few subtle things that are hidden here by the pretty syntax.

  • toTask is lazy in the Future parameter, meaning that evaluation of the method receiver will be deferred. This is significant as Future is eagerly evaluated and caching. It is ideal to ensure that you do not accidentally eagerly evaluate Future values before they are fed into toTask (note: we break this rule in the above example, since f is a val)
  • The Task resulting from toTask will have sane semantics (i.e. it will behave like a normal Task) with respect to reevaluation and laziness, provided that you are disciplined and ensure that the Future value that is the dispatch receiver is not eagerly evaluated elsewhere (e.g. in a val). This means that you can reason about the results of toTask in a referentially transparent fashion, as if the constituent Future had been constructed as a Task all along.
  • toTask takes an implicit ExecutionContext and an implicit Strategy. You should make sure that the appropriate values of each are in scope. If you are not overriding Scalaz's default implicit Strategy for the rest of your Task composition, then you can just rely on the default Strategy. The point is just to make sure that the Task resulting from toTask is context-shifted into the same thread pool you're using for "normal" Task(s).
  • unsafeToFuture is exactly as unsafe as it sounds, since the Future you get back will be running eagerly. Do not make use of this function unless you are absolutely sure of what you're doing! It is very dangerous, because it defeats the expected laziness of your Task computation. This function is meant primarily for interop with legacy libraries that require values of type scala.concurrent.Future.
  • toTask and unsafeToFuture are not strict inverses. They add overhead and defeat fairness algorithms in both scala.concurrent and scalaz/scalaz-stream. So… uh… don't repeatedly convert back and forth, k?

Pitfalls

There are two major pitfalls here. First, the toTask conversion can only give you a referentially transparent Task if you religiously avoid eagerly caching its dispatch receiver. As a general rule, this isn't a bad idiom:

def f: Future[A] = ???

val t: Task[A] = f.toTask

In other words, f is a def and not a val. With this sort of machinery, toTask will give you a reasonable output. If you eagerly cache its input Future though, the results are on your own head.

Second, unsafeToFuture immediately runs the input Task (as mentioned above). There really isn't anything else that could be done here, since Future is eager, but it's worth mentioning. Additionally, unsafeToFuture makes no attempt to thread-shift the input Task, since in general this is not possible (or necessarily desirable). The resulting Future will be on the appropriate thread pool, and it is certainly safe to flatMap on said Future and treat it normally, but the computation itself will be run on whatever thread scheduler the Task was composed against.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].