funcool/canal

Name: canal

Owner: funcool

Description: DEPRECATED: A channel monad for cats library

Created: 2015-03-25 18:44:51.0

Updated: 2017-09-04 17:31:36.0

Pushed: 2015-08-13 05:22:04.0

Homepage:

Size: 207

Language: Clojure

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

cats-channels

Deprecated: The new version is now merged into cats library.

Clojars Project

A channel monad for cats library. 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:

s/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:

uire '[cats.core :as m])
uire '[cats.monad.channel :as channel])
uire '[cljs.core.async :refer [chan put! <!!]])

eclare arbitrary channel with initial value
 mychan (channel/with-value 2))

se channel as a functor
 (m/fmap inc mychan))
> 3

The channel type also fulfills the monad abstraction, let see it in action:

 result (m/mlet [a (channel/with-value 2)
                 b (channel/with-value 3)]
          (m/return (+ a b))))
 result)
> 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:

uire '[cats.monad.either :as either])

eclare a monad transformer
 either-chan-m
ither/either-transformer channel/channel-monad))

 success example
 (m/with-monad either-chan-m
   (m/mlet [a (channel/with-value (either/right 2))
            b (channel/with-value (either/right 3))]
     (m/return (+ a b)))))
> #<Right [5]>

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:

 (m/with-monad either-chan-m
   (m/mlet [a (channel/with-value (either/left "Some error"))
            b (channel/with-value (either/right 3))]
     (m/return (+ a b)))))
> #<Left [Some error]>

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.