Skip to content
This repository has been archived by the owner on Oct 22, 2022. It is now read-only.

Claypoole enhancements by @vincentjames501 #7

Merged
merged 3 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pom.xml.asc
.nrepl-port
.cpcache/

### IntelliJ/Cursive ###
.idea/
*.iml

### Emacs ###
# -*- mode: gitignore; -*-
*~
Expand Down
14 changes: 9 additions & 5 deletions resources/claypoole/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{:linters
{:claypoole {:level :warning}}
:hooks {:analyze-call {com.climate.claypoole/upfor hooks.claypoole.upfor/upfor
com.climate.claypoole/pdoseq hooks.claypoole.upfor/upfor
com.climate.claypoole/pfor hooks.claypoole.upfor/upfor}}}
{:linters {:claypoole {:level :warning}}
:hooks {:analyze-call {com.climate.claypoole/future hooks.claypoole/future
com.climate.claypoole/completable-future hooks.claypoole/completable-future
com.climate.claypoole/pdoseq hooks.claypoole/pdoseq
com.climate.claypoole/pmap hooks.claypoole/pmap
com.climate.claypoole/pvalues hooks.claypoole/pvalues
com.climate.claypoole/upvalues hooks.claypoole/upvalues
com.climate.claypoole/pfor hooks.claypoole/pfor
com.climate.claypoole/upfor hooks.claypoole/upfor}}}
43 changes: 43 additions & 0 deletions resources/claypoole/.clj-kondo/hooks/claypoole.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns hooks.claypoole
(:refer-clojure :exclude [future pmap pvalues])
(:require [clj-kondo.hooks-api :as api]))

(defn pool-and-body
[token]
(fn [{:keys [:node]}]
(let [[pool & body] (rest (:children node))
new-node (api/list-node
[(api/token-node 'fn) ;; we produce a fn and not do to
;; prevent redundant do warnings
(api/vector-node [])
pool
(api/list-node
(list*
(api/token-node token)
body))])]
{:node (with-meta new-node
(meta node))})))

(defn pool-with-binding-vec-or-exprs-and-body
[token]
(fn [{:keys [:node]}]
(let [[pool binding-vec-or-exprs & body] (rest (:children node))
new-node (api/list-node
[(api/token-node token)
binding-vec-or-exprs
(api/list-node
(list* (api/token-node 'fn)
(api/vector-node [])
pool
body))])]
{:node (with-meta new-node
(meta node))})))

(def future (pool-and-body 'future))
(def completable-future (pool-and-body 'future))
(def pdoseq (pool-with-binding-vec-or-exprs-and-body 'doseq))
(def pmap (pool-and-body 'map))
(def pvalues (pool-and-body 'pvalues))
(def upvalues (pool-and-body 'pvalues))
(def pfor (pool-with-binding-vec-or-exprs-and-body 'for))
(def upfor (pool-with-binding-vec-or-exprs-and-body 'for))
34 changes: 0 additions & 34 deletions resources/claypoole/.clj-kondo/hooks/claypoole/upfor.clj

This file was deleted.

15 changes: 15 additions & 0 deletions resources/claypoole/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Claypoole

See the `.clj-kondo` directory for configuration for the [Claypoole](https://github.com/TheClimateCorporation/claypoole) library.

The example effectively remaps the following to their `clojure.core` counterparts:



| Original | Remapped |
|--------------------------------------------|-----------|
| `com.climate.claypoole/future` | `future` |
| `com.climate.claypoole/completable-future` | `future` |
| `com.climate.claypoole/pdoseq` | `doseq` |
| `com.climate.claypoole/pmap` | `map` |
| `com.climate.claypoole/pvalues` | `pvalues` |
| `com.climate.claypoole/upvalues` | `pvalues` |
| `com.climate.claypoole/pfor` | `for` |
| `com.climate.claypoole/upfor` | `for` |
81 changes: 60 additions & 21 deletions resources/claypoole/src/example.clj
Original file line number Diff line number Diff line change
@@ -1,32 +1,71 @@
(ns example
(:require [com.climate.claypoole :as cp]))

(cp/upfor []) ;; no bindings provided
(def pool (cp/threadpool 8))
(def coll [1 2 3])

(def pool)
(defn -main
[& _]
;; future
(cp/future pool (println "Hi from future"))

(cp/upfor pool []) ;; cp/upfor with no bindings
;; pdoseq
(cp/pdoseq 8 [x coll]
(println "Doseqing over" x))
(cp/pdoseq pool [x coll :when (= x 1)]
(println "Doseqing over" x))

(cp/upfor pool [x (range 10)]) ;; unused binding x
;; pmap
(cp/pmap 8
(fn [x] (println "Mapping over" x))
coll)
(cp/pmap pool
(fn [x y] (println "Mapping over" [x y]))
coll
coll)

(cp/upfor pool [x (range 10)
:let [y (inc x)]] ;; let syntax is recognized
y)
;; pvalues
(println "Parallel values"
(cp/pvalues 8
(do (Thread/sleep 100)
(+ 1 2))
(+ 3 4)))
(println "Parallel values"
(cp/pvalues pool
(do (Thread/sleep 100)
(+ 1 2))
(+ 3 4)))

;; From the README of claypoole:
;; upvalues
(println "Ordered parallel values"
(cp/upvalues 8
(do (Thread/sleep 100)
(+ 1 2))
(+ 3 4)))
(println "Ordered parallel values"
(cp/upvalues pool
(do (Thread/sleep 100)
(+ 1 2))
(+ 3 4)))

(defn myfn [_ _])
;; pfor
(cp/pfor 8 [x coll]
(println "For over" x))
(cp/pfor pool [x coll
y coll
:when (and (= x 1) (= y 1))]
(println "For over" [x y]))

(def ordered
(let [pool (cp/priority-threadpool 5)]
(cp/pfor pool [x (range 10)
y (range x)]
(myfn x y))))
;; upfor
(cp/upfor 8 [x coll]
(println "Ordered for over" x))
(cp/upfor pool [x coll
y coll
:when (and (= x 1) (= y 1))]
(println "Ordered for over" [x y]))

(def unordered
(let [pool ;; pool binding is used
(cp/priority-threadpool 5)]
(cp/upfor pool [x (range 10)
:let [ys (range x)] ;; :let syntax is recognized
y ys]
(myfn x y))))
(cp/upfor 8) ;; arity warning with correct location

(let [x 8] ;; binding is used
(cp/upfor x [y coll] (println y)))
)