Futurama is a Clojure library for more deeply integrating async abstractions in the Clojure and JVM ecosystem with Clojure core.async.
It adds support for CompletableFuture and IDeferred to be used in approximately the same fashion as Clojure core.async channels, and for Future and IDeref to be read in a similar manner as well.
Here's a simple example.
(ns user
(:require [futurama.core :refer [async async-for !<!! !<!]])
(:import [java.util.concurrent CompletableFuture]))
(defn future-thing
[what]
(CompletableFuture/completedFuture (format "%s: done" what)))
(async
(println (!<! (future-thing "laundry"))))
;;; => prints out: "laundry: done"
(!<!!
(async-for
[a (range 4)
b (range 4)
:let [c (+ a b)]
:when (and (odd? a) (odd? b))]
(!<! (timeout 50)) ;;; can use !<! inside `async-for` comprehension so the items are evaluated sequentially
[a b c (+ a b c)]))
;;; => returns `[[1 1 2 4] [1 3 4 8] [3 1 4 8] [3 3 6 12]]` and takes slightly over 200ms total time.
(!<!!
(async-for
[a (range 4)
b (range 4)
:let [c (+ a b)]
:when (and (odd? a) (odd? b))]
(async
(!<! (timeout 50)) ;;; here we use !<! inside an async block so we iterate faster through the items
[a b c (+ a b c)])))
;;; => returns `[[1 1 2 4] [1 3 4 8] [3 1 4 8] [3 3 6 12]]` evaluated fully async and takes slightly over 50ms total time.
See the existing tests for more examples.
Futurama is built, tested, and deployed using Clojure Tools Deps.
GNU Make is used to simplify invocation of some commands.
Futurama releases for this project are on Clojars. Simply add the following to your project:
- For any other questions or issues about Futurama free to browse or open a Github Issue.
See CONTRIBUTING.md
YourKit supports open source projects with innovative and intelligent tools for monitoring and profiling Java and .NET applications. YourKit is the creator of YourKit Java Profiler, YourKit .NET Profiler, and YourKit YouMonitor.
Copyright 2024 Jose Gomez
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.