Skip to content

Commit ad7d9c4

Browse files
committed
comp tweak, ireduce for vecs
1 parent 6044097 commit ad7d9c4

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/clj/clojure/core.clj

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,20 +2435,8 @@
24352435
([x y] (f (g x y)))
24362436
([x y z] (f (g x y z)))
24372437
([x y z & args] (f (apply g x y z args)))))
2438-
([f g h]
2439-
(fn
2440-
([] (f (g (h))))
2441-
([x] (f (g (h x))))
2442-
([x y] (f (g (h x y))))
2443-
([x y z] (f (g (h x y z))))
2444-
([x y z & args] (f (g (apply h x y z args))))))
2445-
([f1 f2 f3 & fs]
2446-
(let [fs (reverse (list* f1 f2 f3 fs))]
2447-
(fn [& args]
2448-
(loop [ret (apply (first fs) args) fs (next fs)]
2449-
(if fs
2450-
(recur ((first fs) ret) (next fs))
2451-
ret))))))
2438+
([f g & fs]
2439+
(reduce1 comp (list* f g fs))))
24522440

24532441
(defn juxt
24542442
"Takes a set of functions and returns a fn that is the juxtaposition

src/jvm/clojure/lang/PersistentVector.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.util.List;
1818
import java.util.concurrent.atomic.AtomicBoolean;
1919

20-
public class PersistentVector extends APersistentVector implements IObj, IEditableCollection{
20+
public class PersistentVector extends APersistentVector implements IObj, IEditableCollection, IReduce{
2121

2222
public static class Node implements Serializable {
2323
transient public final AtomicBoolean edit;
@@ -260,6 +260,24 @@ public void remove(){
260260

261261
public Iterator iterator(){return rangedIterator(0,count());}
262262

263+
public Object reduce(IFn f){
264+
throw new UnsupportedOperationException();
265+
}
266+
267+
public Object reduce(IFn f, Object init){
268+
int step = 0;
269+
for(int i=0;i<cnt;i+=step){
270+
Object[] array = arrayFor(i);
271+
for(int j =0;j<array.length;++j){
272+
init = f.invoke(init,array[j]);
273+
if(RT.isReduced(init))
274+
return ((IDeref)init).deref();
275+
}
276+
step = array.length;
277+
}
278+
return init;
279+
}
280+
263281
public Object kvreduce(IFn f, Object init){
264282
int step = 0;
265283
for(int i=0;i<cnt;i+=step){

0 commit comments

Comments
 (0)