https://github.com/funcool/canal
DEPRECATED: A channel monad for cats library
https://github.com/funcool/canal
Last synced: about 1 month ago
JSON representation
DEPRECATED: A channel monad for cats library
- Host: GitHub
- URL: https://github.com/funcool/canal
- Owner: funcool
- License: bsd-2-clause
- Created: 2015-03-25T18:44:51.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-08-13T05:22:04.000Z (over 10 years ago)
- Last Synced: 2025-10-02T02:55:35.089Z (3 months ago)
- Language: Clojure
- Homepage:
- Size: 202 KB
- Stars: 7
- Watchers: 7
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# cats-channels #
**Deprecated**: The new version is now merged into cats library.
[](http://clojars.org/cats/cats-channel)
A channel monad for [cats library](https://github.com/funcool/cats). It works with both, Clojure and
ClojureScript.
## Install ##
The simplest way to use _cats-channel_ in a Clojure project is by including
it as a dependency in your *_project.clj_*:
```clojure
[cats/cats-channel "0.1.0"]
```
## Getting Started ##
In asynchronous environments with clojure and clojurescript we tend to use core.async, because it
is a very powerfull abstraction.
It would be awesome to be able to work with channel as a monadic type, and combine it with error
monads for short-circuiting async computations that may fail.
Let's start using channel as a functor:
```clojure
(require '[cats.core :as m])
(require '[cats.monad.channel :as channel])
(require '[cljs.core.async :refer [chan put! 3
```
The channel type also fulfills the monad abstraction, let see it in action:
```clojure
(def result (m/mlet [a (channel/with-value 2)
b (channel/with-value 3)]
(m/return (+ a b))))
( 5
```
But the best of all is coming: combine the channel monad with error monads. It allows to build very
concise and simple asynchronous APIs. Let see how you can use it your application:
```clojure
(require '[cats.monad.either :as either])
;; Declare a monad transformer
(def either-chan-m
(either/either-transformer channel/channel-monad))
;; A success example
( #
```
As you can see, the code looks very similar to the previos example, with the exception that
the value in a channel is not a simple plain value, is an either instance.
Let's see what happens if some computation fails in the mlet composition:
```clojure
( #
```
The result is the expected short-circuiting left, without unexpected nullpointer exceptions
or similar issues.
With this compositional power, you can model your asynchronous API with a complete
error handling using any error monad (in this case Either).
## Faq ##
**Why is not part of cats library directly?**
Because channel monad depends on core async and we do not want make core.async
as mandatory dependency.