clementine 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +4 -2
  3. data/LICENSE.txt +22 -0
  4. data/README.md +11 -6
  5. data/Rakefile +22 -0
  6. data/clementine.gemspec +2 -1
  7. data/ext/clojure-clojurescript-bef56a7/.gitignore +13 -0
  8. data/ext/clojure-clojurescript-bef56a7/Clojurescript.iml +12 -0
  9. data/ext/clojure-clojurescript-bef56a7/README.md +29 -0
  10. data/ext/clojure-clojurescript-bef56a7/benchmark/cljs/benchmark_runner.cljs +155 -0
  11. data/ext/clojure-clojurescript-bef56a7/bin/cljsc +21 -0
  12. data/ext/clojure-clojurescript-bef56a7/bin/cljsc.bat +18 -0
  13. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/bin/cljsc.clj +0 -0
  14. data/ext/clojure-clojurescript-bef56a7/devnotes/README.org +35 -0
  15. data/ext/clojure-clojurescript-bef56a7/devnotes/bcrepl.org +13 -0
  16. data/ext/clojure-clojurescript-bef56a7/devnotes/cljs.org +500 -0
  17. data/ext/clojure-clojurescript-bef56a7/devnotes/corelib.org +583 -0
  18. data/ext/clojure-clojurescript-bef56a7/devnotes/day1.org +203 -0
  19. data/ext/clojure-clojurescript-bef56a7/devnotes/day2.org +44 -0
  20. data/ext/clojure-clojurescript-bef56a7/devnotes/talk.org +126 -0
  21. data/ext/clojure-clojurescript-bef56a7/devnotes/testing +13 -0
  22. data/ext/clojure-clojurescript-bef56a7/devnotes/todo.org +121 -0
  23. data/ext/clojure-clojurescript-bef56a7/epl-v10.html +261 -0
  24. data/ext/clojure-clojurescript-bef56a7/pom.template.xml +88 -0
  25. data/ext/clojure-clojurescript-bef56a7/samples/dom/.gitignore +2 -0
  26. data/ext/clojure-clojurescript-bef56a7/samples/dom/src/dom/test.cljs +48 -0
  27. data/ext/clojure-clojurescript-bef56a7/samples/dom/test.html +30 -0
  28. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/.gitignore +2 -0
  29. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/README.md +53 -0
  30. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/externed-lib.js +7 -0
  31. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/externs.js +3 -0
  32. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-extern.html +14 -0
  33. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-js-dev.html +18 -0
  34. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-js.html +17 -0
  35. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/my-external-lib.js +3 -0
  36. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/src/hello-js/core.cljs +9 -0
  37. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/src/hello-js/extern-example.cljs +5 -0
  38. data/ext/clojure-clojurescript-bef56a7/samples/hello/.gitignore +2 -0
  39. data/ext/clojure-clojurescript-bef56a7/samples/hello/README.md +34 -0
  40. data/ext/clojure-clojurescript-bef56a7/samples/hello/hello-dev.html +18 -0
  41. data/ext/clojure-clojurescript-bef56a7/samples/hello/hello.html +13 -0
  42. data/ext/clojure-clojurescript-bef56a7/samples/hello/src/hello/core.cljs +8 -0
  43. data/ext/clojure-clojurescript-bef56a7/samples/hello/src/hello/foo/bar.cljs +4 -0
  44. data/ext/clojure-clojurescript-bef56a7/samples/nodehello.cljs +18 -0
  45. data/ext/clojure-clojurescript-bef56a7/samples/nodels.cljs +17 -0
  46. data/ext/clojure-clojurescript-bef56a7/samples/repl/.gitignore +2 -0
  47. data/ext/clojure-clojurescript-bef56a7/samples/repl/README.md +101 -0
  48. data/ext/clojure-clojurescript-bef56a7/samples/repl/index.html +27 -0
  49. data/ext/clojure-clojurescript-bef56a7/samples/repl/src/repl/test.cljs +73 -0
  50. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/.gitignore +2 -0
  51. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/README.md +42 -0
  52. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/index-advanced.html +80 -0
  53. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/index.html +88 -0
  54. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/reset.css +48 -0
  55. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/anneal.cljs +66 -0
  56. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/core.cljs +307 -0
  57. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/dom-helpers.cljs +95 -0
  58. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/layout.cljs +100 -0
  59. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/leaderboard.cljs +40 -0
  60. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/radial.cljs +91 -0
  61. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/showgraph.cljs +121 -0
  62. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/timeline.cljs +39 -0
  63. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/style.css +301 -0
  64. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/test_data.txt +1 -0
  65. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/tweet_maps.txt +1 -0
  66. data/ext/clojure-clojurescript-bef56a7/script/benchmark +30 -0
  67. data/ext/clojure-clojurescript-bef56a7/script/bootstrap +70 -0
  68. data/ext/clojure-clojurescript-bef56a7/script/browser-repl +16 -0
  69. data/ext/clojure-clojurescript-bef56a7/script/build +59 -0
  70. data/ext/clojure-clojurescript-bef56a7/script/clean +5 -0
  71. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/google-closure-library-third-party.pom.template +59 -0
  72. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/google-closure-library.pom.template +54 -0
  73. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/make-closure-library-jars.sh +87 -0
  74. data/ext/clojure-clojurescript-bef56a7/script/compile +41 -0
  75. data/ext/clojure-clojurescript-bef56a7/script/repl +13 -0
  76. data/ext/clojure-clojurescript-bef56a7/script/repl.bat +13 -0
  77. data/ext/clojure-clojurescript-bef56a7/script/repljs +15 -0
  78. data/ext/clojure-clojurescript-bef56a7/script/repljs.bat +14 -0
  79. data/ext/clojure-clojurescript-bef56a7/script/test +38 -0
  80. data/ext/clojure-clojurescript-bef56a7/script/test-compile +30 -0
  81. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/analyzer.clj +975 -0
  82. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/closure.clj +173 -73
  83. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/compiler.clj +1081 -0
  84. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/core.clj +1158 -0
  85. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/repl.clj +51 -25
  86. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/browser.clj +258 -0
  87. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/reflect.clj +75 -0
  88. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/repl/rhino.clj +6 -5
  89. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/server.clj +173 -0
  90. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/tagged_literals.clj +30 -0
  91. data/ext/clojure-clojurescript-bef56a7/src/cljs/cljs/core.cljs +7197 -0
  92. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejs.cljs +1 -1
  93. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejs_externs.js +0 -0
  94. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejscli.cljs +1 -1
  95. data/ext/clojure-clojurescript-bef56a7/src/cljs/cljs/reader.cljs +551 -0
  96. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/dom.cljs +59 -13
  97. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/event.cljs +0 -0
  98. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/net.cljs +8 -7
  99. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/repl.cljs +2 -2
  100. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/core/reducers.cljs +298 -0
  101. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/data.cljs +162 -0
  102. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/reflect.cljs +48 -0
  103. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/set.cljs +0 -0
  104. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/string.cljs +4 -10
  105. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/walk.cljs +0 -0
  106. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/zip.cljs +0 -0
  107. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/binding_test.cljs +7 -0
  108. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/binding_test_other_ns.cljs +3 -0
  109. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/core_test.cljs +1678 -0
  110. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/import_test.cljs +11 -0
  111. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/import_test/foo.cljs +5 -0
  112. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/letfn_test.cljs +19 -0
  113. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/macro_test.cljs +6 -0
  114. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/macro_test/macros.clj +5 -0
  115. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test.cljs +14 -0
  116. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test/bar.cljs +3 -0
  117. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test/foo.cljs +7 -0
  118. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/reader_test.cljs +124 -0
  119. data/ext/clojure-clojurescript-bef56a7/test/cljs/clojure/data_test.cljs +22 -0
  120. data/ext/clojure-clojurescript-bef56a7/test/cljs/clojure/string_test.cljs +97 -0
  121. data/ext/clojure-clojurescript-bef56a7/test/cljs/foo/ns_shadow_test.cljs +9 -0
  122. data/ext/clojure-clojurescript-bef56a7/test/cljs/test_runner.cljs +26 -0
  123. data/lib/clementine.rb +3 -24
  124. data/lib/clementine/clojurescript_engine.rb +9 -48
  125. data/lib/clementine/clojurescript_engine/base.rb +15 -0
  126. data/lib/clementine/clojurescript_engine/jruby.rb +46 -0
  127. data/lib/clementine/{clojurescript_engine_mri.rb → clojurescript_engine/mri.rb} +17 -10
  128. data/lib/clementine/version.rb +1 -1
  129. data/test/clojurescript_engine_test.rb +36 -14
  130. metadata +177 -83
  131. data/vendor/assets/lib/clojure.jar +0 -0
  132. data/vendor/assets/lib/compiler.jar +0 -0
  133. data/vendor/assets/lib/goog.jar +0 -0
  134. data/vendor/assets/lib/js.jar +0 -0
  135. data/vendor/assets/src/clj/cljs/compiler.clj +0 -1341
  136. data/vendor/assets/src/clj/cljs/core.clj +0 -702
  137. data/vendor/assets/src/clj/cljs/repl/browser.clj +0 -341
  138. data/vendor/assets/src/cljs/cljs/core.cljs +0 -3330
  139. data/vendor/assets/src/cljs/cljs/reader.cljs +0 -360
@@ -7,7 +7,8 @@
7
7
  ;; You must not remove this notice, or any other, from this software.
8
8
 
9
9
  (ns clojure.browser.dom
10
- (:require [goog.dom :as gdom]))
10
+ (:require [goog.dom :as gdom]
11
+ [goog.object :as gobject]))
11
12
 
12
13
  (defn append [parent & children]
13
14
  (apply gdom/append parent children)
@@ -41,15 +42,16 @@
41
42
  ([this attrs children]
42
43
  (log "string (-element " this " " attrs " " children ")")
43
44
  (let [str-attrs (if (and (map? attrs) (seq attrs))
44
- (.strobj (reduce (fn [m [k v]]
45
- (log "m = " m)
46
- (log "k = " k)
47
- (log "v = " v)
48
- (when (or (keyword? k)
49
- (string? k))
50
- (assoc m (name k) v)))
51
- {}
52
- attrs))
45
+ (reduce (fn [o [k v]]
46
+ (let [o (if (nil? o) (js-obj) o)]
47
+ (log "o = " o)
48
+ (log "k = " k)
49
+ (log "v = " v)
50
+ (when (or (keyword? k)
51
+ (string? k))
52
+ (doto o (aset (name k) v)))))
53
+ (js-obj)
54
+ attrs)
53
55
  nil)]
54
56
  (log-obj str-attrs)
55
57
  (if (seq children)
@@ -58,12 +60,12 @@
58
60
  str-attrs
59
61
  (map -element children))
60
62
  (gdom/createDom (name this)
61
- str-attrs)))))
63
+ str-attrs)))))
62
64
 
63
- Vector
65
+ PersistentVector
64
66
  (-element
65
67
  [this]
66
- (log "Vector (-element " this ")")
68
+ (log "PersistentVector (-element " this ")")
67
69
  (let [tag (first this)
68
70
  attrs (second this)
69
71
  children (drop 2 this)]
@@ -102,5 +104,49 @@
102
104
  (defn insert-at [parent child index]
103
105
  (gdom/insertChildAt parent child index))
104
106
 
107
+ (defn ensure-element
108
+ "Coerce the argument to a dom element if possible."
109
+ [e]
110
+ (cond (keyword? e) (get-element e)
111
+ (string? e) (html->dom e)
112
+ :else e))
113
+
114
+ (defn replace-node
115
+ "Replace old-node with new-node. old-node can be an element or a
116
+ keyword which is the id of the node to replace. new-node can be an
117
+ element or an html string."
118
+ [old-node new-node]
119
+ (let [old-node (ensure-element old-node)
120
+ new-node (ensure-element new-node)]
121
+ (gdom/replaceNode new-node old-node)
122
+ new-node))
123
+
124
+ (defn set-text
125
+ "Set the text content for the passed element returning the
126
+ element. If a keyword is passed in the place of e, the element with
127
+ that id will be used and returned."
128
+ [e s]
129
+ (gdom/setTextContent (ensure-element e) s))
130
+
131
+ (defn get-value
132
+ "Get the value of an element."
133
+ [e]
134
+ (.-value (ensure-element e)))
135
+
136
+ (defn set-properties
137
+ "Set properties on an element"
138
+ [e m]
139
+ (gdom/setProperties (ensure-element e)
140
+ (apply gobject/create (interleave (keys m) (vals m)))))
141
+
142
+ (defn set-value
143
+ "Set the value property for an element."
144
+ [e v]
145
+ (set-properties e {"value" v}))
146
+
147
+ (defn click-element
148
+ [e]
149
+ (.click (ensure-element e) ()))
150
+
105
151
  ;; TODO CSS class manipulation
106
152
  ;; TODO Query syntax
@@ -104,7 +104,7 @@ Includes a common API over XhrIo, CrossPageChannel, and Websockets."
104
104
  ([this on-connect-fn]
105
105
  (.connect this on-connect-fn))
106
106
  ([this on-connect-fn config-iframe-fn]
107
- (connect this on-connect-fn config-iframe-fn (.body js/document)))
107
+ (connect this on-connect-fn config-iframe-fn (.-body js/document)))
108
108
  ([this on-connect-fn config-iframe-fn iframe-parent]
109
109
  (.createPeerIframe this iframe-parent config-iframe-fn)
110
110
  (.connect this on-connect-fn)))
@@ -127,16 +127,17 @@ Includes a common API over XhrIo, CrossPageChannel, and Websockets."
127
127
  per the CrossPageChannel API."
128
128
  ([]
129
129
  (when-let [config (.getParameterValue
130
- (goog.Uri. (.href (.location js/window)))
130
+ (goog.Uri. (.-href (.-location js/window)))
131
131
  "xpc")]
132
132
  (goog.net.xpc.CrossPageChannel. (gjson/parse config))))
133
133
  ([config]
134
134
  (goog.net.xpc.CrossPageChannel.
135
- (.strobj (reduce (fn [sum [k v]]
136
- (when-let [field (get xpc-config-fields k)]
137
- (assoc sum field v)))
138
- {}
139
- config)))))
135
+ (reduce (fn [sum [k v]]
136
+ (if-let [field (get xpc-config-fields k)]
137
+ (doto sum (aset field v))
138
+ sum))
139
+ (js-obj)
140
+ config))))
140
141
 
141
142
  ;; WebSocket is not supported in the 3/23/11 release of Google
142
143
  ;; Closure, but will be included in the next release.
@@ -31,7 +31,7 @@
31
31
  (catch js/Error e
32
32
  {:status :exception :value (pr-str e)
33
33
  :stacktrace (if (.hasOwnProperty e "stack")
34
- (.stack e)
34
+ (.-stack e)
35
35
  "No stacktrace available.")}))]
36
36
  (pr-str result)))
37
37
 
@@ -105,5 +105,5 @@
105
105
  (net/connect repl-connection
106
106
  (constantly nil)
107
107
  (fn [iframe]
108
- (set! iframe.style.display
108
+ (set! (.-display (.-style iframe))
109
109
  "none")))))
@@ -0,0 +1,298 @@
1
+ ; Copyright (c) Rich Hickey. All rights reserved.
2
+ ; The use and distribution terms for this software are covered by the
3
+ ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4
+ ; which can be found in the file epl-v10.html at the root of this distribution.
5
+ ; By using this software in any fashion, you are agreeing to be bound by
6
+ ; the terms of this license.
7
+ ; You must not remove this notice, or any other, from this software.
8
+
9
+ (ns ^{:doc
10
+ "A library for reduction and parallel folding. Alpha and subject
11
+ to change. Note that fold and its derivatives require
12
+ jsr166y.jar for fork/join support. See Clojure's pom.xml for the
13
+ dependency info."
14
+ :author "Rich Hickey"}
15
+ clojure.core.reducers
16
+ (:refer-clojure :exclude [reduce map mapcat filter remove take take-while drop flatten])
17
+ (:require [clojure.walk :as walk]
18
+ [cljs.core :as core]))
19
+
20
+ (defn reduce
21
+ "Like core/reduce except:
22
+ When init is not provided, (f) is used.
23
+ Maps are reduced with reduce-kv"
24
+ ([f coll] (reduce f (f) coll))
25
+ ([f init coll]
26
+ (if (map? coll)
27
+ (-kv-reduce coll f init)
28
+ (-reduce coll f init))))
29
+
30
+ #_
31
+ (defprotocol CollFold
32
+ (coll-fold [coll n combinef reducef]))
33
+
34
+ ;;; TODO: update docstring for CLJS
35
+ #_
36
+ (defn fold
37
+ "Reduces a collection using a (potentially parallel) reduce-combine
38
+ strategy. The collection is partitioned into groups of approximately
39
+ n (default 512), each of which is reduced with reducef (with a seed
40
+ value obtained by calling (combinef) with no arguments). The results
41
+ of these reductions are then reduced with combinef (default
42
+ reducef). combinef must be associative, and, when called with no
43
+ arguments, (combinef) must produce its identity element. These
44
+ operations may be performed in parallel, but the results will
45
+ preserve order."
46
+ ([reducef coll] (fold reducef reducef coll))
47
+ ([combinef reducef coll] (fold 512 combinef reducef coll))
48
+ ([n combinef reducef coll]
49
+ (coll-fold coll n combinef reducef)))
50
+
51
+ (def fold reduce)
52
+
53
+ (defn reducer
54
+ "Given a reducible collection, and a transformation function xf,
55
+ returns a reducible collection, where any supplied reducing
56
+ fn will be transformed by xf. xf is a function of reducing fn to
57
+ reducing fn."
58
+ ([coll xf]
59
+ (reify
60
+ cljs.core/IReduce
61
+ (-reduce [this f1]
62
+ (-reduce this f1 (f1)))
63
+ (-reduce [_ f1 init]
64
+ (-reduce coll (xf f1) init)))))
65
+
66
+ (defn folder
67
+ "Given a foldable collection, and a transformation function xf,
68
+ returns a foldable collection, where any supplied reducing
69
+ fn will be transformed by xf. xf is a function of reducing fn to
70
+ reducing fn."
71
+ ([coll xf]
72
+ (reify
73
+ cljs.core/IReduce
74
+ (-reduce [_ f1]
75
+ (-reduce coll (xf f1) (f1)))
76
+ (-reduce [_ f1 init]
77
+ (-reduce coll (xf f1) init))
78
+
79
+ #_
80
+ CollFold
81
+ #_
82
+ (coll-fold [_ n combinef reducef]
83
+ (coll-fold coll n combinef (xf reducef))))))
84
+
85
+ (defcurried map
86
+ "Applies f to every value in the reduction of coll. Foldable."
87
+ {}
88
+ [f coll]
89
+ (folder coll
90
+ (fn [f1]
91
+ (rfn [f1 k]
92
+ ([ret k v]
93
+ (f1 ret (f k v)))))))
94
+
95
+ (defcurried mapcat
96
+ "Applies f to every value in the reduction of coll, concatenating the result
97
+ colls of (f val). Foldable."
98
+ {}
99
+ [f coll]
100
+ (folder coll
101
+ (fn [f1]
102
+ (rfn [f1 k]
103
+ ([ret k v]
104
+ (reduce f1 ret (f k v)))))))
105
+
106
+ (defcurried filter
107
+ "Retains values in the reduction of coll for which (pred val)
108
+ returns logical true. Foldable."
109
+ {}
110
+ [pred coll]
111
+ (folder coll
112
+ (fn [f1]
113
+ (rfn [f1 k]
114
+ ([ret k v]
115
+ (if (pred k v)
116
+ (f1 ret k v)
117
+ ret))))))
118
+
119
+ (defcurried flatten
120
+ "Takes any nested combination of sequential things (lists, vectors,
121
+ etc.) and returns their contents as a single, flat foldable
122
+ collection."
123
+ {}
124
+ [coll]
125
+ (folder coll
126
+ (fn [f1]
127
+ (fn
128
+ ([] (f1))
129
+ ([ret v]
130
+ (if (sequential? v)
131
+ (-reduce (flatten v) f1 ret)
132
+ (f1 ret v)))))))
133
+
134
+ (defcurried remove
135
+ "Removes values in the reduction of coll for which (pred val)
136
+ returns logical true. Foldable."
137
+ {}
138
+ [pred coll]
139
+ (filter (complement pred) coll))
140
+
141
+ (defcurried take-while
142
+ "Ends the reduction of coll when (pred val) returns logical false."
143
+ {}
144
+ [pred coll]
145
+ (reducer coll
146
+ (fn [f1]
147
+ (rfn [f1 k]
148
+ ([ret k v]
149
+ (if (pred k v)
150
+ (f1 ret k v)
151
+ (reduced ret)))))))
152
+
153
+ (defcurried take
154
+ "Ends the reduction of coll after consuming n values."
155
+ {}
156
+ [n coll]
157
+ (reducer coll
158
+ (fn [f1]
159
+ (let [cnt (atom n)]
160
+ (rfn [f1 k]
161
+ ([ret k v]
162
+ (swap! cnt dec)
163
+ (if (neg? @cnt)
164
+ (reduced ret)
165
+ (f1 ret k v))))))))
166
+
167
+ (defcurried drop
168
+ "Elides the first n values from the reduction of coll."
169
+ {}
170
+ [n coll]
171
+ (reducer coll
172
+ (fn [f1]
173
+ (let [cnt (atom n)]
174
+ (rfn [f1 k]
175
+ ([ret k v]
176
+ (swap! cnt dec)
177
+ (if (neg? @cnt)
178
+ (f1 ret k v)
179
+ ret)))))))
180
+
181
+ ;;do not construct this directly, use cat
182
+ (deftype Cat [cnt left right]
183
+ cljs.core/ICounted
184
+ (-count [_] cnt)
185
+
186
+ cljs.core/ISeqable
187
+ (-seq [_] (concat (seq left) (seq right)))
188
+
189
+ cljs.core/IReduce
190
+ (-reduce [this f1] (-reduce this f1 (f1)))
191
+ (-reduce
192
+ [_ f1 init]
193
+ (-reduce
194
+ right f1
195
+ (-reduce left f1 init)))
196
+
197
+ #_
198
+ CollFold
199
+ #_
200
+ (coll-fold
201
+ [this n combinef reducef]
202
+ (-reduce this reducef)))
203
+
204
+ (defn cat
205
+ "A high-performance combining fn that yields the catenation of the
206
+ reduced values. The result is reducible, foldable, seqable and
207
+ counted, providing the identity collections are reducible, seqable
208
+ and counted. The single argument version will build a combining fn
209
+ with the supplied identity constructor. Tests for identity
210
+ with (zero? (count x)). See also foldcat."
211
+ ([] (array))
212
+ ([ctor]
213
+ (fn
214
+ ([] (ctor))
215
+ ([left right] (cat left right))))
216
+ ([left right]
217
+ (cond
218
+ (zero? (count left)) right
219
+ (zero? (count right)) left
220
+ :else
221
+ (Cat. (+ (count left) (count right)) left right))))
222
+
223
+ (defn append!
224
+ ".adds x to acc and returns acc"
225
+ [acc x]
226
+ (doto acc (.add x)))
227
+
228
+ (defn foldcat
229
+ "Equivalent to (fold cat append! coll)"
230
+ [coll]
231
+ (fold cat append! coll))
232
+
233
+ (defn monoid
234
+ "Builds a combining fn out of the supplied operator and identity
235
+ constructor. op must be associative and ctor called with no args
236
+ must return an identity value for it."
237
+ [op ctor]
238
+ (fn m
239
+ ([] (ctor))
240
+ ([a b] (op a b))))
241
+
242
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243
+ (comment
244
+ (require '[clojure.core.reduce :as r])
245
+ (def v (take 1000000 (range)))
246
+ (reduce + 0 (r/map inc [1 2 3 4]))
247
+ (into [] (r/take 12 (range 100)))
248
+ (into [] (r/drop 12 (range 100)))
249
+ (reduce + 0 (r/filter even? [1 2 3 4]))
250
+ (into [] (r/filter even? [1 2 3 4]))
251
+ (reduce + (filter even? [1 2 3 4]))
252
+ (dotimes [_ 10] (time (reduce + 0 (r/map inc v))))
253
+ (dotimes [_ 10] (time (reduce + 0 (map inc v))))
254
+ (dotimes [_ 100] (time (reduce + 0 v)))
255
+ (dotimes [_ 100] (time (reduce + 0 v)))
256
+ (dotimes [_ 20] (time (reduce + 0 (r/map inc (r/filter even? v)))))
257
+ (dotimes [_ 20] (time (reduce + 0 (map inc (filter even? v)))))
258
+ (reduce + 0 (r/take-while even? [2 4 3]))
259
+ (into [] (r/filter even? (r/flatten (r/remove #{4} [[1 2 3] 4 [5 [6 7 8]] [9] 10]))))
260
+ (into [] (r/flatten nil))
261
+ )
262
+
263
+ (comment
264
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; fold impls ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265
+ (defn- foldvec
266
+ [v n combinef reducef]
267
+ (cond
268
+ (empty? v) (combinef)
269
+ (<= (count v) n) (reduce reducef (combinef) v)
270
+ :else
271
+ (let [split (quot (count v) 2)
272
+ v1 (subvec v 0 split)
273
+ v2 (subvec v split (count v))
274
+ fc (fn [child] #(foldvec child n combinef reducef))]
275
+ (fjinvoke
276
+ #(let [f1 (fc v1)
277
+ t2 (fjtask (fc v2))]
278
+ (fjfork t2)
279
+ (combinef (f1) (fjjoin t2)))))))
280
+
281
+ (extend-protocol CollFold
282
+ Object
283
+ (coll-fold
284
+ [coll n combinef reducef]
285
+ ;;can't fold, single reduce
286
+ (reduce reducef (combinef) coll))
287
+
288
+ clojure.lang.IPersistentVector
289
+ (coll-fold
290
+ [v n combinef reducef]
291
+ (foldvec v n combinef reducef))
292
+
293
+ clojure.lang.PersistentHashMap
294
+ (coll-fold
295
+ [m n combinef reducef]
296
+ (.fold m n combinef reducef fjinvoke fjtask fjfork fjjoin)))
297
+
298
+ )