Skip to content

Commit 0da1e88

Browse files
committed
Update Clojure README
1 parent a2d2314 commit 0da1e88

File tree

1 file changed

+87
-32
lines changed

1 file changed

+87
-32
lines changed

language-adaptors/rxjava-clojure/README.md

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,90 @@
1-
# Clojure Adaptor for RxJava
1+
Clojure bindings for RxJava.
22

3+
# Binaries
4+
5+
Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxjava-clojure%22).
6+
7+
Example for Leiningen:
8+
9+
```clojure
10+
[com.netflix.rxjava/rxjava-clojure "x.y.z"]
11+
```
12+
13+
and for Gradle:
14+
15+
```groovy
16+
compile 'com.netflix.rx:rxjava-clojure:x.y.z'
17+
```
18+
19+
and for Maven:
20+
21+
```xml
22+
<dependency>
23+
<groupId>com.netflix.rxjava</groupId>
24+
<artifactId>rxjava-clojure</artifactId>
25+
<version>x.y.z</version>
26+
</dependency>
27+
```
28+
29+
and for Ivy:
30+
31+
```xml
32+
<dependency org="com.netflix.rxjava" name="rxjava-clojure" rev="x.y.z" />
33+
```
34+
35+
# Clojure Bindings
36+
This library provides convenient, idiomatic Clojure bindings for RxJava.
37+
38+
The bindings try to present an API that will be comfortable and familiar to a Clojure programmer that's familiar with the sequence operations in `clojure.core`. It "fixes" several issues with using RxJava with raw Java interop, for example:
39+
40+
* Argument lists are in the "right" order. So in RxJava, the function applied in `Observable.map` is the second argument, while here it's the first argument with one or more Observables as trailing arguments
41+
* Operators take normal Clojure functions as arguments, bypassing need for the interop described below
42+
* Predicates accomodate Clojure's notion of truth
43+
* Operators are generally names as they would be in `clojure.core` rather than the Rx names
44+
45+
There is no object wrapping going on. That is, all functions return normal `rx.Observable` objects, so you can always drop back to Java interop for anything that's missing in this wrapper.
46+
47+
## Basic Usage
48+
Most functionality resides in the `rx.lang.clojure.core` namespace and for the most part looks like normal Clojure sequence manipulation:
49+
50+
```clojure
51+
(require '[rx.lang.clojure.core :as rx])
52+
53+
(->> my-observable
54+
(rx/map (comp clojure.string/lower-case :first-name))
55+
(rx/map clojure.string/lower-case)
56+
(rx/filter #{"bob"})
57+
(rx/distinct)
58+
(rx/into []))
59+
;=> An Observable that emits a single vector of names
60+
```
61+
62+
Blocking operators, which are useful for testing, but should otherwise be avoided, reside in `rx.lang.clojure.blocking`. For example:
63+
64+
```clojure
65+
(require '[rx.lang.clojure.blocking :as rxb])
66+
67+
(rxb/doseq [{:keys [first-name]} users-observable]
68+
(println "Hey," first-name))
69+
;=> nil
70+
```
71+
72+
## What's Missing
73+
This library is an ongoing work in progress driven primarily by the needs of one team at Netflix. As such some things are currently missing:
74+
75+
* Highly-specific operators that we felt cluttered the API and were easily composed from existing operators, especially since we're in not-Java land. For example, `Observable.sumLong()`.
76+
* Most everything involving schedulers
77+
* Most everything involving time
78+
* `Observable.window` and `Observable.buffer`. Who knows which parts of these beasts to wrap?
79+
80+
Of course, contributions that cover these cases are welcome.
81+
82+
# Low-level Interop
383
This adaptor provides functions and macros to ease Clojure/RxJava interop. In particular, there are functions and macros for turning Clojure functions and code into RxJava `Func*` and `Action*` interfaces without the tedium of manually reifying the interfaces.
484

5-
# Basic Usage
85+
## Basic Usage
686

7-
## Requiring the interop namespace
87+
### Requiring the interop namespace
888
The first thing to do is to require the namespace:
989

1090
```clojure
@@ -19,7 +99,7 @@ or, at the REPL:
1999
(require '[rx.lang.clojure.interop :as rx])
20100
```
21101

22-
## Using rx/fn
102+
### Using rx/fn
23103
Once the namespace is required, you can use the `rx/fn` macro anywhere RxJava wants a `rx.util.functions.Func` object. The syntax is exactly the same as `clojure.core/fn`:
24104

25105
```clojure
@@ -34,7 +114,7 @@ If you already have a plain old Clojure function you'd like to use, you can pass
34114
(.reduce (rx/fn* +)))
35115
```
36116

37-
## Using rx/action
117+
### Using rx/action
38118
The `rx/action` macro is identical to `rx/fn` except that the object returned implements `rx.util.functions.Action` interfaces. It's used in `subscribe` and other side-effect-y contexts:
39119

40120
```clojure
@@ -46,7 +126,7 @@ The `rx/action` macro is identical to `rx/fn` except that the object returned im
46126
(rx/action [] (println "Sequence complete"))))
47127
```
48128

49-
## Using Observable/create
129+
### Using Observable/create
50130
As of 0.17, `rx.Observable/create` takes an implementation of `rx.Observable$OnSubscribe` which is basically an alias for `rx.util.functions.Action1` that takes an `rx.Subscriber` as its argument. Thus, you can just use `rx/action` when creating new observables:
51131

52132
```clojure
@@ -59,35 +139,10 @@ As of 0.17, `rx.Observable/create` takes an implementation of `rx.Observable$OnS
59139
(.onCompleted s)))
60140
```
61141

62-
# Gotchas
142+
## Gotchas
63143
Here are a few things to keep in mind when using this interop:
64144

65145
* Keep in mind the (mostly empty) distinction between `Func` and `Action` and which is used in which contexts
66146
* If there are multiple Java methods overloaded by `Func` arity, you'll need to use a type hint to let the compiler know which one to choose.
67147
* Methods that take a predicate (like filter) expect the predicate to return a boolean value. A function that returns a non-boolean value will result in a `ClassCastException`.
68148

69-
# Binaries
70-
71-
Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxjava-clojure%22).
72-
73-
Example for Maven:
74-
75-
```xml
76-
<dependency>
77-
<groupId>com.netflix.rxjava</groupId>
78-
<artifactId>rxjava-clojure</artifactId>
79-
<version>x.y.z</version>
80-
</dependency>
81-
```
82-
83-
and for Ivy:
84-
85-
```xml
86-
<dependency org="com.netflix.rxjava" name="rxjava-clojure" rev="x.y.z" />
87-
```
88-
89-
and for Leiningen:
90-
91-
```clojure
92-
[com.netflix.rxjava/rxjava-clojure "x.y.z"]
93-
```

0 commit comments

Comments
 (0)