(ns test.gadjett (:require [gadjett.linear :as glinear :refer [intersection-point-func linear-equation intersection-point find-below-and-above-y linear-equation-func highest-below-x linear-y linear-x-func log-x-intersection-point interpolate-linear-y linear-y-func intersection-point-from-2-lines-points lowest-above-x find-keys-with-value log-x-linear-y lowest-above-y log-x-linear-equation calc-interpolated-values linear-x interpolate-linear-x below-and-above-y highest-below-y find-keys-with-values-in]] [gadjett.collections :as gcoll :refer [compact unflatten-keys sequence->map sequence-of-maps->map =without-keys? dissoc-in split-by-predicate-positions mapify submap? positions filter-map vec->map assoc-cyclic infinity subsequence edn-zip fix-blank-lines map-nested-vals map-to-object deep-merge deep-merge-with mean map-object-kv flatten-keys flatten-keys* compactize-map compactize-map-recursive partition-between take-from-map map-with-index map-object-with-key deep-merge* range-till-end nearest-of-seq branches-and-leaves remove-ending-comments remove-blank-lines filter-branches out-of-bound? map-2d-vec split-by-predicate collify abs append-cyclic map-2d-vec-kv max-and-min index-of nearest-of-ss map-reverse-hierarchy map-object my-replace join-them]]))

Click here to make these examples interactive with ClojureScript.

gadjett.async-utils

aaaa-load-ns

(aaaa-load-ns)
(require '[clojure.core.async :refer [<!]])
(require-macros '[cljs.core.async.macros :refer [go go-loop]])  
(require '[gadjett.async-utils :refer [safe-memoize-async memoize-async go-map go-map-object go-seq->map wait-for-msg wait-for-condition parallel safe-parallel safe-serial]])

go-map

(go-map f coll)

Usage: (go-map f coll)

Returns a channel that will receive the result of f applied to each item of coll once all the data is available. The calculations are done in parallel.


(defn async-square [x] (go (* x x))) (go (println (<! (go-map async-square [1 2 3]))))

go-map-object

(go-map-object f m)

Usage: (go-map-object f m)

Returns channel that will receive a map with the same keys as m and with the values transformed by f. Where f is an asynchronous function.


(defn async-square [x] (go (* x x))) (go (println (<! (go-map-object async-square {:a 1 :b 2}))))

go-seq->map

(go-seq->map f lst)

memoize-async

(memoize-async f)

Usage: (memoize-async f)

Same as clojure.core.memoize for asynchronous functions (e.g. functions that return a chan). Returns a channel with the memoized result of the function call.


(defn async-square [x] (go (* x x))) (go (println (<! ((memoize-async async-square) 4))))

parallel

(parallel chans)

Receives a sequence of channels and returns a channel which will receive a sequence with the result of all the channels. Data is read from the channels in parallel. The result is a sequence where each element in the sequence is the result of the corresponding channel in the sequence.

safe-memoize-async

(safe-memoize-async f success-test-fn)

Usage: (safe-memoize-async f success-test-fn)

Same as memoize-async, but will only memoize the result if it passes the success-test-fn.

(def foo (safe-memoize-async (fn [x]
                             (go
                               (println "running the function with: " x)
                               (if (zero? x)
                                 [:error]
                                 [:ok x])))
                           #(= :ok (first %))))

(go (println (<! (foo 0)))
  (println (<! (foo 0)))
  (println (<! (foo 1)))
  (println (<! (foo 1))))

safe-parallel

(safe-parallel chans)

Receives a sequence of channels and fail-test function and returns a channel which will receive a sequence with the result of all the channels or with error if one of the channels failed. Data is read from the channels in parallel. if one of the channels failed - according to the fail-test function the result is the error message in other case the result is a sequence where each element in the sequence is the result of the corresponding channel in the sequence.

safe-serial

(safe-serial functions)

Receives a vector of {:f-name name :f-deps dependent-functions :f async-function}, and runs the functions one after the other. Each function is called with the result of its dependent functnios. Returns a channel with the result of all the functions, or with an error if one of the functions failed.

wait-for-condition

(wait-for-condition f interval-in-msec)

returns a channel that will be activated when the predicate f returns true.

(let [start (js/Date.)]
(go 
  (println "start:" (js/Date.))
  (<! (wait-for-condition #(>= (- (js/Date.) start) 1000) 100))
  (println "done:" (js/Date.))))

wait-for-msg

(wait-for-msg c msg)