├── .gitignore ├── project.clj ├── README.md ├── src └── core_async_patterns │ └── core.clj └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject core-async-patterns "0.1.0-SNAPSHOT" 2 | :description "Example patterns for using clojure.core.async" 3 | :url "https://purelyfunctional.tv/core-async-patterns" 4 | :license {:name "CC0 1.0 Universal (CC0 1.0) Public Domain Dedication" 5 | :url "http://creativecommons.org/publicdomain/zero/1.0/"} 6 | :dependencies [[org.clojure/clojure "1.10.0"] 7 | [org.clojure/core.async "0.4.490"]]) 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # core-async-patterns 2 | 3 | Code to accompany the [Clojure core.async Patterns course][patterns], 4 | part of [PurelyFunctional.tv Online Mentoring][mentoring]. 5 | 6 | [patterns]: https://purelyfunctional.tv/courses/core-async-patterns 7 | [mentoring]: https://purelyfunctional.tv/ 8 | 9 | ## License 10 | 11 | [![CC0](http://i.creativecommons.org/p/zero/1.0/88x31.png)](http://creativecommons.org/publicdomain/zero/1.0/) 12 | 13 | To the extent possible under law, the person who associated CC0 with 14 | this work has waived all copyright and related or neighboring rights 15 | to the code in this repository. 16 | 17 | See the `LICENSE` file for more information. 18 | -------------------------------------------------------------------------------- /src/core_async_patterns/core.clj: -------------------------------------------------------------------------------- 1 | (ns core-async-patterns.core 2 | (:require [clojure.core.async :as async]) 3 | (:require [clojure.test :refer [deftest is]])) 4 | 5 | ;; Take with timeout 6 | (defn take-with-timeout!! [c t] 7 | (first (async/alts!! [c (async/timeout t)] :priority true))) 8 | (defn take-with-timeout! [c t] 9 | (async/go 10 | (first (async/alts! [c (async/timeout t)] :priority true)))) 11 | 12 | ;; Put with timeout 13 | (defn put-with-timeout!! [c v t] 14 | (first (async/alts!! [[c v] (async/timeout t)] :priority true))) 15 | (defn put-with-timeout! [c v t] 16 | (async/go 17 | (first (async/alts! [[c v] (async/timeout t)] :priority true)))) 18 | 19 | ;; Go Throw 20 | (defmacro