Skip to content

Commit bccec8a

Browse files
committed
Adds smap (chain) and amap
1 parent 014db94 commit bccec8a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Diff for: project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject jtk-dvlp/core.async-helpers "3.0.2"
1+
(defproject jtk-dvlp/core.async-helpers "3.1.0-SNAPSHOT"
22
:description
33
"Helper pack for core.async"
44

Diff for: src/jtk_dvlp/async.cljc

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns jtk-dvlp.async
22
(:refer-clojure
3-
:exclude [map reduce into])
3+
:exclude [map pmap amap reduce into])
44

55
#?(:cljs
66
(:require-macros
@@ -116,3 +116,33 @@
116116
"Like `core.async/into` but carries thrown exception (will convert to `ExceptionInfo`) as result."
117117
[coll ch]
118118
(reduce conj coll ch))
119+
120+
(defn smap
121+
"Applies async function `<f` on every item of seqs `xs` *chaining its execution to make sure its run sequentially*. All seqs of `xs` must have the same length. Returns vector of all results applying `<f`. Supports error handling.
122+
123+
Also see `amap`"
124+
[<f & xs]
125+
(go-loop [result [], xs xs]
126+
(if (ffirst xs)
127+
(do
128+
(let [next-result
129+
(->> xs
130+
(mapv first)
131+
(apply <f)
132+
(<!))]
133+
134+
(recur
135+
(conj result next-result)
136+
(mapv next xs))))
137+
138+
result)))
139+
140+
(def chain smap)
141+
142+
(defn- amap
143+
"Applies async function `<f` on every item of seqs `xs`. All seqs of `xs` must have the same length. Returns vector of all results applying `<f`. Supports error handling.
144+
145+
Also see `smap`"
146+
[<f & xs]
147+
(->> (apply clojure.core/map <f xs)
148+
(map vector)))

0 commit comments

Comments
 (0)