Back
pmap (clj)
(source)function
(pmap f coll)
(pmap f coll & colls)
Like map, except f is applied in parallel. Semi-lazy in that the
parallel computation stays ahead of the consumption, but doesn't
realize the entire result unless required. Only useful for
computationally intensive functions where the time of f dominates
the coordination overhead.
Examples
clojure
(ns clojure.test-clojure.reducers
(:require [clojure.core.reducers :as r]
[clojure.test.generative :refer (defspec)]
[clojure.data.generators :as gen])
(:use clojure.test))
(defspec reduced-always-returns
(fn [probe to-end]
(let [len (+ probe to-end 1)
nums (range len)
m (zipmap nums nums)]
(reduced-at-probe m probe)))
[^{:tag `gen-num} probe ^{:tag `gen-num} to-end]
(assert (= :foo %)))
(deftest test-fold-runtime-exception
(is (thrown? IndexOutOfBoundsException
(let [test-map-count 1234
k-fail (rand-int test-map-count)]
(r/fold (fn ([])
([ret [k v]])
([ret k v] (when (= k k-fail)
(throw (IndexOutOfBoundsException.)))))
(zipmap (range test-map-count) (repeat :dummy)))))))
jepsen-io/jepsen
(ns yugabyte.ycql.bank
(:refer-clojure :exclude [test])
(:require [clojure.tools.logging :refer [debug info warn]]
[clojure.core.reducers :as r]
[jepsen.client :as client]
[jepsen.checker :as checker]
[jepsen.generator :as gen]
[jepsen.tests.bank :as bank]
[jepsen.checker.timeline :as timeline]
[knossos.op :as op]
[clojurewerkz.cassaforte.client :as cassandra]
[clojurewerkz.cassaforte.cql :as cql]
[clojurewerkz.cassaforte.query :as q :refer :all]
[yugabyte.ycql.client :as c]))
(invoke! [this test op]
(c/with-errors op #{:read}
(case (:f op)
:read
(let [as (shuffle (:accounts test))]
(->> as
(mapv (fn [x]
;; TODO - should be wrapped in a transaction after we
;; support transactions with selects.
(->> (cql/select-with-ks conn keyspace
(str table-name x)
(where [[= :id x]]))
first
:balance)))
(zipmap as)
(assoc op :type :ok, :value)))
clojure/core.typed
;copied from clojure.tools.analyzer.passes.constant-lifter
(ns clojure.core.typed.analyzer.common.passes.constant-lifter
(:require [clojure.core.typed.analyzer.common :as common]
[clojure.core.typed.analyzer.common.utils :refer [const-val]]))
(defmethod constant-lift :map
[{:keys [keys vals form env] :as ast}]
(if (and (every? :literal? keys)
(every? :literal? vals)
(empty? (meta form)))
(let [c (into (empty form)
(zipmap (mapv const-val keys)
(mapv const-val vals)))
c (if (= (class c) (class form))
c
(apply array-map (mapcat identity c)))]
(merge (dissoc ast :keys :vals :children)
{:op :const
::common/op ::common/const
:val c
:type :map
:literal? true}))
ast))
hoplon/hoplon
(ns hoplon.binding
(:refer-clojure :exclude [binding bound-fn])
(:require [clojure.core :as clj]
[cljs.analyzer :as a]))
(defmacro binding
"See clojure.core/binding."
[bindings & body]
(let [env (assoc &env :ns (a/get-namespace a/*cljs-ns*))
value-exprs (take-nth 2 (rest bindings))
bind-syms (map #(:name (a/resolve-existing-var env %)) (take-nth 2 bindings))
bind-syms' (map (partial list 'quote) bind-syms)
set-syms (repeatedly (count bind-syms) gensym)
setfn (fn [x y]
{:push! `(fn []
(let [z# ~x]
(set! ~x ~y)
(fn [] (set! ~x z#))))})
thunkmaps (map setfn bind-syms set-syms)]
(a/confirm-bindings env bind-syms)
`(let [~@(interleave set-syms value-exprs)]
(hoplon.binding/push-thread-bindings ~(zipmap bind-syms' thunkmaps))
(try ~@body (finally (hoplon.binding/pop-thread-bindings))))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/zipmap (t/All [k v] [(t/Seqable k) (t/Seqable v) :-> #?(:cljs (t/Map k v)
:default (APersistentMap k v))])
cc/map (t/All [c a b :..] (t/IFn [[a :-> c] :-> (t/Transducer a c)]
[[a b :.. b :-> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) :.. b :-> (t/NonEmptyASeq c)]
[[a b :.. b :-> c] (t/Seqable a) (t/Seqable b) :.. b :-> (t/ASeq c)]))
cc/mapv (t/All [c a b :..] (t/IFn [[a b :.. b :-> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) :.. b :-> (t/NonEmptyAVec c)]
[[a b :.. b :-> c] (t/Seqable a) (t/Seqable b) :.. b :-> (t/AVec c)]))
cc/mapcat (t/All [c a b :..] (t/IFn
[[a :-> (t/Seqable c)] :-> (t/Transducer a c)]
[[a b :.. b :-> (t/Seqable c)] (t/Seqable a) (t/Seqable b) :.. b :-> (t/ASeq c)]))
cc/cat (t/All [x] (t/Transducer (t/Seqable x) x))
#?@(:cljs [] :default [
cc/pmap (t/All [c a b :..] (t/IFn [[a b :.. b :-> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) :.. b :-> (t/NonEmptyASeq c)]
[[a b :.. b :-> c] (t/Seqable a) (t/Seqable b) :.. b :-> (t/ASeq c)]))
cc/pcalls (t/All [r] [[:-> r] :* :-> (t/ASeq r)])
])
PacktPublishing/Clojure-Programming-Cookbook
(use 'clojure.data.xml)
(use '[clojure.java.io :only [writer reader input-stream output-stream]])
(require '[clojure.core.async :as async])
(with-open [in (reader (input-stream "test_0.xml"))]
(doall
(pmap
#(async/>!! in-chan %)
(line-seq in))))