• Stars
    star
    1,017
  • Rank 45,237 (Top 0.9 %)
  • Language
    Clojure
  • Created over 10 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A compatibility layer for event-driven abstractions

Clojars Project cljdoc badge CircleCI

Manifold provides basic building blocks for asynchronous programming, and can be used as a translation layer between libraries which use similar, but incompatible, abstractions.

Manifold provides two core abstractions: deferreds, which represent a single asynchronous value, and streams, which represent an ordered sequence of asynchronous values.

A detailed discussion of Manifold's rationale can be found here. Full documentation can be found here.

Leiningen:

[manifold "0.4.1"]

deps.edn:

manifold/manifold {:mvn/version "0.4.1"}

Deferreds

A deferred in Manifold is similar to a Clojure promise:

> (require '[manifold.deferred :as d])
nil

> (def d (d/deferred))
#'d

> (d/success! d :foo)
true

> @d
:foo

However, similar to Clojure's futures, deferreds in Manifold can also represent errors. Crucially, they also allow for callbacks to be registered, rather than simply blocking on dereferencing.

> (def d (d/deferred))
#'d

> (d/error! d (Exception. "boom"))
true

> @d
Exception: boom
> (def d (d/deferred))
#'d

> (d/on-realized d
    (fn [x] (println "success!" x))
    (fn [x] (println "error!" x)))
<< ... >>

> (d/success! d :foo)
success! :foo
true

Callbacks are a useful building block, but they're a painful way to create asynchronous workflows. In practice, no one should ever need to use on-realized. Manifold provides a number of operators for composing over deferred values, which can be read about here.

Streams

Manifold's streams provide mechanisms for asynchronous puts and takes, timeouts, and backpressure. They are compatible with Java's BlockingQueues, Clojure's lazy sequences, and core.async's channels. Methods for converting to and from each are provided.

Manifold differentiates between sources, which emit messages, and sinks, which consume messages. We can interact with sources using take! and try-take!, which return deferred values representing the next message. We can interact with sinks using put! and try-put!, which return a deferred values which will yield true if the put is successful, or false otherwise.

We can create a stream using (manifold.stream/stream):

> (require '[manifold.stream :as s])
nil
> (def s (s/stream))
#'s
> (s/put! s 1)
<< ... >>
> (s/take! s)
<< 1 >>

A stream is both a sink and a source; any message sent via put! can be received via take!. We can also create sinks and sources from other stream representations using ->sink and ->source:

> (require '[clojure.core.async :as a])
nil
> (def c (a/chan))
#'c
> (def s (s/->source c))
#'s
> (a/go (a/>! c 1))
#object[clojure.core.async.impl.channels.ManyToManyChannel 0x7...
> @(s/take! s)
1

We can also turn a Manifold stream into a different representation by using connect to join them together:

> (def s (s/stream))
#'s
> (def c (a/chan))
#'c
> (s/connect s c)
nil
> (s/put! s 1)
<< true >>
> (a/<!! c)
1

Manifold can use any transducer, which are applied via transform. It also provides stream-specific transforms, including zip, reduce, buffer, batch, and throttle. To learn more about streams, go here.

Clojurescript

A Clojurescript implementation of Manifold can be found here: dm3/manifold-cljs.

License

Copyright ยฉ 2014-2022 Zach Tellman

Distributed under the MIT License.

More Repositories

1

aleph

Asynchronous streaming communication for Clojure - web server, web client, and raw TCP/UDP
Clojure
2,544
star
2

kibit

There's a function for that!
Clojure
1,765
star
3

seesaw

Seesaw turns the Horror of Swing into a friendly, well-documented, Clojure library
Clojure
1,445
star
4

etaoin

Pure Clojure Webdriver protocol implementation
Clojure
913
star
5

marginalia

Ultra-lightweight literate programming for clojure inspired by docco
Clojure
816
star
6

secretary

A client-side router for ClojureScript.
Clojure
773
star
7

hickory

HTML as data
Clojure
634
star
8

claypoole

Claypoole: Threadpool tools for Clojure
Clojure
607
star
9

pretty

Library for helping print things prettily, in Clojure - ANSI fonts, formatted exceptions
Clojure
597
star
10

rewrite-clj

Rewrite Clojure code and edn
Clojure
576
star
11

potemkin

some ideas which are almost good
Clojure
568
star
12

pomegranate

A sane Clojure API for Maven Artifact Resolver + dynamic runtime modification of the classpath
Clojure
504
star
13

gloss

speaks in bytes, so you don't have to
Clojure
483
star
14

camel-snake-kebab

A Clojure[Script] library for word case conversions
Clojure
476
star
15

cljss

Clojure Style Sheets โ€” CSS-in-JS for ClojureScript
Clojure
455
star
16

clooj

clooj, a lightweight IDE for clojure
Clojure
421
star
17

byte-streams

A Rosetta stone for JVM byte representations
Clojure
417
star
18

durable-queue

a disk-backed queue for clojure
Clojure
381
star
19

useful

Some Clojure functions we use all the time, and so can you.
Clojure
365
star
20

metrics-clojure

A thin faรงade around Coda Hale's metrics library.
Clojure
343
star
21

virgil

Recompile Java code without restarting the REPL
Clojure
302
star
22

citrus

State management library for Rum
Clojure
274
star
23

ordered

Ordered sets and maps, implemented in pure clojure
Clojure
253
star
24

clj-ssh

SSH commands via jsch
Clojure
227
star
25

dirigiste

centrally-planned object and thread pools
Java
204
star
26

iapetos

A Clojure Prometheus Client
Clojure
176
star
27

primitive-math

for the discerning arithmetician
Clojure
170
star
28

humanize

Produce human readable strings in clojure
Clojure
156
star
29

digest

Digest algorithms (md5, sha1 ...) for Clojure
Clojure
156
star
30

clj-yaml

YAML encoding and decoding for Clojure
Clojure
120
star
31

byte-transforms

methods for hashing, compressing, and encoding bytes
Clojure
104
star
32

ring-buffer

A persistent ring-buffer in Clojure
Clojure
96
star
33

tentacles

An Octocat is nothing without his tentacles
Clojure
80
star
34

fs

File system utilities for Clojure. (forked from Raynes/fs)
Clojure
72
star
35

lein-marginalia

A Marginalia plugin to Leiningen
HTML
69
star
36

meta

A meta-repo for clj-commons discussions
46
star
37

ring-gzip-middleware

GZIP your Ring responses
Clojure
41
star
38

rewrite-cljs

Traverse and rewrite Clojure/ClojureScript/EDN from ClojureScript
Clojure
41
star
39

formatter

Building blocks and discussion for building a common Clojure code formatter
36
star
40

vizdeps

Visualize Leiningen dependencies using Graphviz
Clojure
33
star
41

zprint-clj

Node.js wrapper for ZPrint Clojure source code formatter
Clojure
13
star
42

infra

Infrastructure for clj-commons
Clojure
2
star
43

clj-commons.github.io

Clojure Commons Site
HTML
1
star