Codebase list clojure / fresh-snapshots/upstream
Import upstream version 1.11.1+git20221207.1.527b330+ds Debian Janitor 1 year, 4 months ago
44 changed file(s) with 603 addition(s) and 270 deletion(s). Raw diff Collapse all Expand all
+0
-5
.github/PULL_REQUEST_TEMPLATE less more
0 Hi! This project does not accept pull requests.
1
2 Please see the guidelines for contribution on how to file issues or provide patches:
3
4 https://github.com/clojure/clojure/blob/master/CONTRIBUTING.md
+0
-9
.gitignore less more
0 *.jar
1 target
2 clojure.iws
3 clojure.ipr
4 nbproject/private/
5 maven-classpath
6 maven-classpath.properties
7 .idea/
8 *.iml
44 <artifactId>clojure</artifactId>
55 <name>clojure</name>
66 <packaging>jar</packaging>
7 <version>1.11.1</version>
7 <version>1.12.0-master-SNAPSHOT</version>
88
99 <url>http://clojure.org/</url>
1010 <description>Clojure core environment and runtime library.</description>
2929 <connection>scm:git:git@github.com:clojure/clojure.git</connection>
3030 <developerConnection>scm:git:git@github.com:clojure/clojure.git</developerConnection>
3131 <url>git@github.com:clojure/clojure.git</url>
32 <tag>clojure-1.11.1</tag>
32 <tag>HEAD</tag>
3333 </scm>
3434
3535 <properties>
29232923 (cons (first s) (take-while pred (rest s))))))))
29242924
29252925 (defn drop
2926 "Returns a lazy sequence of all but the first n items in coll.
2926 "Returns a laziness-preserving sequence of all but the first n items in coll.
29272927 Returns a stateful transducer when no collection is provided."
29282928 {:added "1.0"
29292929 :static true}
29402940 result
29412941 (rf result input))))))))
29422942 ([n coll]
2943 (let [step (fn [n coll]
2944 (let [s (seq coll)]
2945 (if (and (pos? n) s)
2946 (recur (dec n) (rest s))
2947 s)))]
2948 (lazy-seq (step n coll)))))
2943 (if (instance? clojure.lang.IDrop coll)
2944 (or (.drop ^clojure.lang.IDrop coll n) ())
2945 (let [step (fn [n coll]
2946 (let [s (seq coll)]
2947 (if (and (pos? n) s)
2948 (recur (dec n) (rest s))
2949 s)))]
2950 (lazy-seq (step n coll))))))
29492951
29502952 (defn drop-last
29512953 "Return a lazy sequence of all but the last n (default 1) items in coll"
31663168 {:added "1.0"
31673169 :static true}
31683170 [coll n]
3171 (if (instance? clojure.lang.IDrop coll)
3172 (.drop ^clojure.lang.IDrop coll n)
31693173 (loop [n n xs (seq coll)]
31703174 (if (and xs (pos? n))
31713175 (recur (dec n) (next xs))
3172 xs)))
3176 xs))))
31733177
31743178 (defn nthrest
31753179 "Returns the nth rest of coll, coll when n is 0."
31763180 {:added "1.3"
31773181 :static true}
31783182 [coll n]
3183 (if (instance? clojure.lang.IDrop coll)
3184 (or (.drop ^clojure.lang.IDrop coll n) ())
31793185 (loop [n n xs coll]
31803186 (if-let [xs (and (pos? n) (seq xs))]
31813187 (recur (dec n) (rest xs))
3182 xs)))
3188 xs))))
31833189
31843190 (defn partition
31853191 "Returns a lazy sequence of lists of n items each, at offsets step
62376243 ([m k f x y z & more]
62386244 (assoc m k (apply f (get m k) x y z more))))
62396245
6240 (defn empty?
6241 "Returns true if coll has no items - same as (not (seq coll)).
6242 Please use the idiom (seq x) rather than (not (empty? x))"
6243 {:added "1.0"
6244 :static true}
6245 [coll] (not (seq coll)))
6246
62476246 (defn coll?
62486247 "Returns true if x implements IPersistentCollection"
62496248 {:added "1.0"
62986297 {:added "1.0"
62996298 :static true}
63006299 [coll] (instance? clojure.lang.Counted coll))
6300
6301 (defn empty?
6302 "Returns true if coll has no items. To check the emptiness of a seq,
6303 please use the idiom (seq x) rather than (not (empty? x))"
6304 {:added "1.0"
6305 :static true}
6306 [coll]
6307 (if (counted? coll)
6308 (zero? (count coll))
6309 (not (seq coll))))
63016310
63026311 (defn reversible?
63036312 "Returns true if coll implements Reversible"
73377346 (when-let [s (seq coll)]
73387347 (let [seg (doall (take n s))]
73397348 (cons seg (partition-all n step (nthrest s step))))))))
7349
7350 (defn splitv-at
7351 "Returns a vector of [(into [] (take n) coll) (drop n coll)]"
7352 {:added "1.12"}
7353 [n coll]
7354 [(into [] (take n) coll) (drop n coll)])
7355
7356 (defn partitionv
7357 "Returns a lazy sequence of vectors of n items each, at offsets step
7358 apart. If step is not supplied, defaults to n, i.e. the partitions
7359 do not overlap. If a pad collection is supplied, use its elements as
7360 necessary to complete last partition upto n items. In case there are
7361 not enough padding elements, return a partition with less than n items."
7362 {:added "1.12"}
7363 ([n coll]
7364 (partitionv n n coll))
7365 ([n step coll]
7366 (lazy-seq
7367 (when-let [s (seq coll)]
7368 (let [p (into [] (take n) s)]
7369 (when (= n (count p))
7370 (cons p (partitionv n step (nthrest s step))))))))
7371 ([n step pad coll]
7372 (lazy-seq
7373 (when-let [s (seq coll)]
7374 (let [p (into [] (take n) s)]
7375 (if (= n (count p))
7376 (cons p (partitionv n step pad (nthrest s step)))
7377 (list (into [] (take n) (concat p pad)))))))))
7378
7379 (defn partitionv-all
7380 "Returns a lazy sequence of vector partitions, but may include
7381 partitions with fewer than n items at the end.
7382 Returns a stateful transducer when no collection is provided."
7383 {:added "1.12"}
7384 ([n]
7385 (partition-all n))
7386 ([n coll]
7387 (partitionv-all n n coll))
7388 ([n step coll]
7389 (lazy-seq
7390 (when-let [s (seq coll)]
7391 (let [seg (into [] (take n) coll)]
7392 (cons seg (partitionv-all n step (drop step s))))))))
73407393
73417394 (defn shuffle
73427395 "Return a random permutation of coll"
1515 import java.util.Comparator;
1616
1717 public abstract class AFunction extends AFn implements IObj, Comparator, Fn, Serializable {
18
19 private static final long serialVersionUID = 4469383498184457675L;
1820
1921 public volatile MethodImplCache __methodImplCache;
2022
1414 import java.io.StringWriter;
1515
1616 public abstract class AMapEntry extends APersistentVector implements IMapEntry{
17
18 private static final long serialVersionUID = -5007980429903443802L;
1719
1820 public Object nth(int i){
1921 if(i == 0)
1313 import java.util.*;
1414
1515 public abstract class APersistentMap extends AFn implements IPersistentMap, Map, Iterable, Serializable, MapEquivalence, IHashEq {
16
17 private static final long serialVersionUID = 6736310834519110267L;
18
1619 int _hash;
1720 int _hasheq;
1821
1717 import java.util.Set;
1818
1919 public abstract class APersistentSet extends AFn implements IPersistentSet, Collection, Set, Serializable, IHashEq {
20
21 private static final long serialVersionUID = 889908853183699706L;
22
2023 int _hash;
2124 int _hasheq;
2225 final IPersistentMap impl;
1818 List,
1919 RandomAccess, Comparable,
2020 Serializable, IHashEq {
21
22 private static final long serialVersionUID = 4667575149454420891L;
23
2124 int _hash;
2225 int _hasheq;
2326
1313 import java.util.*;
1414
1515 public abstract class ASeq extends Obj implements ISeq, Sequential, List, Serializable, IHashEq {
16
17 private static final long serialVersionUID = 4748650717905139299L;
18
1619 transient int _hash;
1720 transient int _hasheq;
1821
1414 */
1515 public class ArityException extends IllegalArgumentException {
1616
17 private static final long serialVersionUID = 2265783180488869950L;
18
1719 final public int actual;
1820
1921 final public String name;
1414 import java.io.Serializable;
1515
1616 public final class ArrayChunk implements IChunk, Serializable {
17
18 private static final long serialVersionUID = -8302142882294545702L;
1719
1820 final Object[] array;
1921 final int off;
1515 import java.math.BigDecimal;
1616
1717 public final class BigInt extends Number implements IHashEq{
18
19 private static final long serialVersionUID = 5097771279236135022L;
1820
1921 final public long lpart;
2022 final public BigInteger bipart;
1212 package clojure.lang;
1313
1414 final public class ChunkedCons extends ASeq implements IChunkedSeq{
15
16 private static final long serialVersionUID = 2773920188566401743L;
1517
1618 final IChunk chunk;
1719 final ISeq _more;
409409 public final Expr meta;
410410 public final boolean initProvided;
411411 public final boolean isDynamic;
412 public final boolean shadowsCoreMapping;
413412 public final String source;
414413 public final int line;
415414 public final int column;
418417 final static Method setMetaMethod = Method.getMethod("void setMeta(clojure.lang.IPersistentMap)");
419418 final static Method setDynamicMethod = Method.getMethod("clojure.lang.Var setDynamic(boolean)");
420419 final static Method symintern = Method.getMethod("clojure.lang.Symbol intern(String, String)");
421 final static Method internVar = Method.getMethod("clojure.lang.Var refer(clojure.lang.Symbol, clojure.lang.Var)");
422
423 public DefExpr(String source, int line, int column, Var var, Expr init, Expr meta, boolean initProvided, boolean isDynamic, boolean shadowsCoreMapping){
420
421 public DefExpr(String source, int line, int column, Var var, Expr init, Expr meta, boolean initProvided, boolean isDynamic){
424422 this.source = source;
425423 this.line = line;
426424 this.column = column;
428426 this.init = init;
429427 this.meta = meta;
430428 this.isDynamic = isDynamic;
431 this.shadowsCoreMapping = shadowsCoreMapping;
432429 this.initProvided = initProvided;
433430 }
434431
474471
475472 public void emit(C context, ObjExpr objx, GeneratorAdapter gen){
476473 objx.emitVar(gen, var);
477
478 if (shadowsCoreMapping)
479 {
480 gen.dup();
481 gen.getField(VAR_TYPE, "ns", NS_TYPE);
482 gen.swap();
483 gen.dup();
484 gen.getField(VAR_TYPE, "sym", SYMBOL_TYPE);
485 gen.swap();
486 gen.invokeVirtual(NS_TYPE, internVar);
487 }
488
489474 if(isDynamic)
490475 {
491476 gen.push(isDynamic);
543528 Var v = lookupVar(sym, true);
544529 if(v == null)
545530 throw Util.runtimeException("Can't refer to qualified var that doesn't exist");
546 boolean shadowsCoreMapping = false;
547531 if(!v.ns.equals(currentNS()))
548532 {
549533 if(sym.ns == null)
550534 {
551535 v = currentNS().intern(sym);
552 shadowsCoreMapping = true;
553536 registerVar(v);
554537 }
555538 // throw Util.runtimeException("Name conflict, can't def " + sym + " because namespace: " + currentNS().name +
593576 Expr meta = mm.count()==0 ? null:analyze(context == C.EVAL ? context : C.EXPRESSION, mm);
594577 return new DefExpr((String) SOURCE.deref(), lineDeref(), columnDeref(),
595578 v, analyze(context == C.EVAL ? context : C.EXPRESSION, RT.third(form), v.sym.name),
596 meta, RT.count(form) == 3, isDynamic, shadowsCoreMapping);
579 meta, RT.count(form) == 3, isDynamic);
597580 }
598581 }
599582 }
1414 import java.io.Serializable;
1515
1616 final public class Cons extends ASeq implements Serializable {
17
18 private static final long serialVersionUID = 6682587018567831263L;
1719
1820 private final Object _first;
1921 private final ISeq _more;
1212 /* Alex Miller, Dec 5, 2014 */
1313
1414 public class Cycle extends ASeq implements IReduce, IPending {
15
16 private static final long serialVersionUID = 4007270937279943908L;
1517
1618 private final ISeq all; // never null
1719 private final ISeq prev;
1616 import java.util.Enumeration;
1717
1818 public class EnumerationSeq extends ASeq{
19
20 private static final long serialVersionUID = 5227192199685595994L;
21
1922 final Enumeration iter;
2023 final State state;
2124
1515 * exception classes.
1616 */
1717 public class ExceptionInfo extends RuntimeException implements IExceptionInfo {
18
19 private static final long serialVersionUID = -1073473305916521986L;
20
1821 public final IPersistentMap data;
1922
2023 public ExceptionInfo(String s, IPersistentMap data) {
1212 package clojure.lang;
1313
1414 public class FnLoaderThunk extends RestFn{
15
16 private static final long serialVersionUID = 2194257205455463687L;
1517
1618 final Var v;
1719 final ClassLoader loader;
0 /**
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
10 package clojure.lang;
11
12 /**
13 * Persistent or algorithmically defined collections can implement IDrop to provide
14 * a means of dropping N items that is more efficient than sequential walking.
15 */
16 public interface IDrop{
17 /**
18 * Returns a collection that is Sequential, ISeq, and IReduceInit. It is also
19 * useful if the returned coll implements IDrop for subsequent use in a
20 * partition-like scenario.
21 *
22 * If n is &lt;= 0, return this.
23 * If n drops to or past the end of the collection, return null.
24 *
25 * @param n Items to drop
26 * @return Collection that is Sequential, ISeq, and IReduceInit
27 */
28 Sequential drop(int n);
29 }
1212 /* Alex Miller, Dec 5, 2014 */
1313
1414 public class Iterate extends ASeq implements IReduce, IPending {
15
16 private static final long serialVersionUID = -78221705247226450L;
1517
1618 private static final Object UNREALIZED_SEED = new Object();
1719 private final IFn f; // never null
1414 import java.util.Iterator;
1515
1616 public class IteratorSeq extends ASeq{
17
18 private static final long serialVersionUID = -2631916503522522760L;
19
1720 final Iterator iter;
1821 final State state;
1922
1515
1616 public final class LazySeq extends Obj implements ISeq, Sequential, List, IPending, IHashEq{
1717
18 private static final long serialVersionUID = 7700080124382322592L;
19
1820 private IFn fn;
1921 private Object sv;
2022 private ISeq s;
1515 import java.util.NoSuchElementException;
1616
1717 /**
18 * Implements the special common case of a finite range based on long start, end, and step.
18 * Implements the special common case of a finite range based on long start, end, and step,
19 * with no more than Integer.MAX_VALUE items.
1920 */
20 public class LongRange extends ASeq implements Counted, IChunkedSeq, IReduce {
21
22 private static final int CHUNK_SIZE = 32;
21 public class LongRange extends ASeq implements Counted, IChunkedSeq, IReduce, IDrop {
22
23 private static final long serialVersionUID = -1467242400566893909L;
2324
2425 // Invariants guarantee this is never an empty or infinite seq
2526 // assert(start != end && step != 0)
2627 final long start;
2728 final long end;
2829 final long step;
29 final BoundsCheck boundsCheck;
30 private volatile LongChunk _chunk; // lazy
31 private volatile ISeq _chunkNext; // lazy
32 private volatile ISeq _next; // cached
33
34 private static interface BoundsCheck extends Serializable {
35 boolean exceededBounds(long val);
36 }
37
38 private static BoundsCheck positiveStep(final long end) {
39 return new BoundsCheck() {
40 public boolean exceededBounds(long val){
41 return (val >= end);
42 }
43 };
44 }
45
46 private static BoundsCheck negativeStep(final long end) {
47 return new BoundsCheck() {
48 public boolean exceededBounds(long val){
49 return (val <= end);
50 }
51 };
52 }
53
54 private LongRange(long start, long end, long step, BoundsCheck boundsCheck){
30 final int count;
31
32 private LongRange(long start, long end, long step, int count){
5533 this.start = start;
5634 this.end = end;
5735 this.step = step;
58 this.boundsCheck = boundsCheck;
59 }
60
61 private LongRange(long start, long end, long step, BoundsCheck boundsCheck, LongChunk chunk, ISeq chunkNext){
62 this.start = start;
63 this.end = end;
64 this.step = step;
65 this.boundsCheck = boundsCheck;
66 this._chunk = chunk;
67 this._chunkNext = chunkNext;
68 }
69
70 private LongRange(IPersistentMap meta, long start, long end, long step, BoundsCheck boundsCheck, LongChunk chunk, ISeq chunkNext){
36 this.count = count;
37 }
38
39 private LongRange(IPersistentMap meta, long start, long end, long step, int count){
7140 super(meta);
7241 this.start = start;
7342 this.end = end;
7443 this.step = step;
75 this.boundsCheck = boundsCheck;
76 this._chunk = chunk;
77 this._chunkNext = chunkNext;
44 this.count = count;
45 }
46
47 // returns exact size of remaining items OR throws ArithmeticException for overflow case
48 static long rangeCount(long start, long end, long step) {
49 // (1) count = ceiling ( (end - start) / step )
50 // (2) ceiling(a/b) = (a+b+o)/b where o=-1 for positive stepping and +1 for negative stepping
51 // thus: count = end - start + step + o / step
52 return Numbers.add(Numbers.add(Numbers.minus(end, start), step), step > 0 ? -1 : 1) / step;
7853 }
7954
8055 public static ISeq create(long end) {
81 if(end > 0)
82 return new LongRange(0L, end, 1L, positiveStep(end));
83 return PersistentList.EMPTY;
56 if(end > 0) {
57 try {
58 return new LongRange(0L, end, 1L, Math.toIntExact(rangeCount(0L, end, 1L)));
59 } catch(ArithmeticException e) {
60 return Range.create(end); // count > Integer.MAX_VALUE
61 }
62 } else {
63 return PersistentList.EMPTY;
64 }
8465 }
8566
8667 public static ISeq create(long start, long end) {
87 if(start >= end)
68 if(start >= end) {
8869 return PersistentList.EMPTY;
89 return new LongRange(start, end, 1L, positiveStep(end));
70 } else {
71 try {
72 return new LongRange(start, end, 1L, Math.toIntExact(rangeCount(start, end, 1L)));
73 } catch(ArithmeticException e) {
74 return Range.create(start, end);
75 }
76 }
9077 }
9178
9279 public static ISeq create(final long start, long end, long step) {
9380 if(step > 0) {
9481 if(end <= start) return PersistentList.EMPTY;
95 return new LongRange(start, end, step, positiveStep(end));
82 try {
83 return new LongRange(start, end, step, Math.toIntExact(rangeCount(start, end, step)));
84 } catch(ArithmeticException e) {
85 return Range.create(start, end, step);
86 }
9687 } else if(step < 0) {
9788 if(end >= start) return PersistentList.EMPTY;
98 return new LongRange(start, end, step, negativeStep(end));
89 try {
90 return new LongRange(start, end, step, Math.toIntExact(rangeCount(start, end, step)));
91 } catch(ArithmeticException e) {
92 return Range.create(start, end, step);
93 }
9994 } else {
10095 if(end == start) return PersistentList.EMPTY;
10196 return Repeat.create(start);
105100 public Obj withMeta(IPersistentMap meta){
106101 if(meta == _meta)
107102 return this;
108 return new LongRange(meta, start, end, step, boundsCheck, _chunk, _chunkNext);
103 return new LongRange(meta, start, end, step, count);
109104 }
110105
111106 public Object first() {
112107 return start;
113108 }
114109
115 public void forceChunk() {
116 if(_chunk != null) return;
117
118 long count;
119 try {
120 count = rangeCount(start, end, step);
121 } catch(ArithmeticException e) {
122 // size of total range is > Long.MAX_VALUE so must step to count
123 // this only happens in pathological range cases like:
124 // (range -9223372036854775808 9223372036854775807 9223372036854775807)
125 count = steppingCount(start, end, step);
126 }
127
128 if (count > CHUNK_SIZE) { // not last chunk
129 long nextStart = start + (step * CHUNK_SIZE); // cannot overflow, must be < end
130 _chunkNext = new LongRange(nextStart, end, step, boundsCheck);
131 _chunk = new LongChunk(start, step, CHUNK_SIZE);
132 } else { // last chunk
133 _chunk = new LongChunk(start, step, (int) count); // count must be <= CHUNK_SIZE
134 }
135 }
136
137110 public ISeq next() {
138 if(_next != null)
139 return _next;
140
141 forceChunk();
142 if(_chunk.count() > 1) {
143 LongChunk smallerChunk = _chunk.dropFirst();
144 _next = new LongRange(smallerChunk.first(), end, step, boundsCheck, smallerChunk, _chunkNext);
145 return _next;
146 }
147 return chunkedNext();
111 if(count > 1) {
112 return new LongRange(start + step, end, step, count-1);
113 } else {
114 return null;
115 }
148116 }
149117
150118 public IChunk chunkedFirst() {
151 forceChunk();
152 return _chunk;
119 return new LongChunk(start, step, count);
153120 }
154121
155122 public ISeq chunkedNext() {
156 return chunkedMore().seq();
123 return null;
157124 }
158125
159126 public ISeq chunkedMore() {
160 forceChunk();
161 if(_chunkNext == null)
162 return PersistentList.EMPTY;
163 return _chunkNext;
164 }
165
166 // fallback count mechanism for pathological cases
167 // returns either exact count or CHUNK_SIZE+1
168 long steppingCount(long start, long end, long step) {
169 long count = 1;
170 long s = start;
171 while(count <= CHUNK_SIZE) {
172 try {
173 s = Numbers.add(s, step);
174 if(boundsCheck.exceededBounds(s))
175 break;
176 else
177 count++;
178 } catch(ArithmeticException e) {
179 break;
180 }
181 }
127 return PersistentList.EMPTY;
128 }
129
130 public Sequential drop(int n) {
131 if(n <= 0) {
132 return this;
133 } else if(n < count) {
134 return new LongRange(start+(step*n), end, step, count - n);
135 } else {
136 return null;
137 }
138 }
139
140 public int count() {
182141 return count;
183 }
184
185 // returns exact size of remaining items OR throws ArithmeticException for overflow case
186 long rangeCount(long start, long end, long step) {
187 // (1) count = ceiling ( (end - start) / step )
188 // (2) ceiling(a/b) = (a+b+o)/b where o=-1 for positive stepping and +1 for negative stepping
189 // thus: count = end - start + step + o / step
190 return Numbers.add(Numbers.add(Numbers.minus(end, start), step), this.step > 0 ? -1 : 1) / step;
191 }
192
193 public int count() {
194 try {
195 long c = rangeCount(start, end, step);
196 if(c > Integer.MAX_VALUE) {
197 return Numbers.throwIntOverflow();
198 } else {
199 return (int) c;
200 }
201 } catch(ArithmeticException e) {
202 // rare case from large range or step, fall back to iterating and counting
203 Iterator iter = this.iterator();
204 long count = 0;
205 while(iter.hasNext()) {
206 iter.next();
207 count++;
208 }
209
210 if(count > Integer.MAX_VALUE)
211 return Numbers.throwIntOverflow();
212 else
213 return (int)count;
214 }
215142 }
216143
217144 public Object reduce(IFn f) {
218145 Object acc = start;
219146 long i = start + step;
220 while(! boundsCheck.exceededBounds(i)) {
147 int n = count;
148 while(n > 1) {
221149 acc = f.invoke(acc, i);
222150 if (acc instanceof Reduced) return ((Reduced)acc).deref();
223151 i += step;
152 n--;
224153 }
225154 return acc;
226155 }
227156
228157 public Object reduce(IFn f, Object val) {
229158 Object acc = val;
159 int n = count;
230160 long i = start;
231161 do {
232162 acc = f.invoke(acc, i);
233163 if (RT.isReduced(acc)) return ((Reduced)acc).deref();
234164 i += step;
235 } while(! boundsCheck.exceededBounds(i));
165 n--;
166 } while(n > 0);
236167 return acc;
237168 }
238169
242173
243174 class LongRangeIterator implements Iterator {
244175 private long next;
245 private boolean hasNext;
176 private int remaining;
246177
247178 public LongRangeIterator() {
248179 this.next = start;
249 this.hasNext = true;
180 this.remaining = count;
250181 }
251182
252183 public boolean hasNext() {
253 return hasNext;
184 return remaining > 0;
254185 }
255186
256187 public Object next() {
257 if (hasNext) {
188 if (remaining > 0) {
258189 long ret = next;
259 try {
260 next = Numbers.add(next, step);
261 hasNext = ! boundsCheck.exceededBounds(next);
262 } catch(ArithmeticException e) {
263 hasNext = false;
264 }
190 next = next + step;
191 remaining = remaining - 1;
265192 return ret;
266193 } else {
267194 throw new NoSuchElementException();
321248 }
322249
323250 }
324 }
251 }
1212 import java.util.Iterator;
1313
1414 public class MapEntry extends AMapEntry{
15
16 private static final long serialVersionUID = -3752414622414469244L;
17
1518 final Object _key;
1619 final Object _val;
1720
1717 import java.util.concurrent.atomic.AtomicReference;
1818
1919 public class Namespace extends AReference implements Serializable {
20
21 private static final long serialVersionUID = -3134444395801383865L;
22
2023 final public Symbol name;
2124 transient final AtomicReference<IPersistentMap> mappings = new AtomicReference<IPersistentMap>();
2225 transient final AtomicReference<IPersistentMap> aliases = new AtomicReference<IPersistentMap>();
4649 return mappings.get();
4750 }
4851
52 /**
53 * An interned mapping is one where a var's ns matches the current ns and its sym matches the mapping key.
54 * Once established, interned mappings should never change.
55 */
56 private boolean isInternedMapping(Symbol sym, Object o){
57 return(o instanceof Var &&
58 ((Var) o).ns == this &&
59 ((Var) o).sym.equals(sym));
60 }
61
4962 public Var intern(Symbol sym){
5063 if(sym.ns != null)
5164 {
5265 throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
5366 }
67
5468 IPersistentMap map = getMappings();
5569 Object o;
5670 Var v = null;
6276 mappings.compareAndSet(map, newMap);
6377 map = getMappings();
6478 }
65 if(o instanceof Var && ((Var) o).ns == this)
79 if(isInternedMapping(sym, o))
6680 return (Var) o;
6781
6882 if(v == null)
6983 v = new Var(this, sym);
7084
71 warnOrFailOnReplace(sym, o, v);
72
73
74 while(!mappings.compareAndSet(map, map.assoc(sym, v)))
75 map = getMappings();
76
77 return v;
78 }
79
80 private void warnOrFailOnReplace(Symbol sym, Object o, Object v){
81 if (o instanceof Var)
82 {
83 Namespace ns = ((Var)o).ns;
84 if (ns == this || (v instanceof Var && ((Var)v).ns == RT.CLOJURE_NS))
85 return;
86 if (ns != RT.CLOJURE_NS)
87 throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
88 }
89 RT.errPrintWriter().println("WARNING: " + sym + " already refers to: " + o + " in namespace: " + name
90 + ", being replaced by: " + v);
85 if(checkReplacement(sym, o, v)){
86 while (!mappings.compareAndSet(map, map.assoc(sym, v)))
87 map = getMappings();
88
89 return v;
90 }
91
92 return (Var) o;
93 }
94
95 /*
96 This method checks if a namespace's mapping is applicable and warns on problematic cases.
97 It will return a boolean indicating if a mapping is replaceable.
98 The semantics of what constitutes a legal replacement mapping is summarized as follows:
99
100 | classification | in namespace ns | newval = anything other than ns/name | newval = ns/name |
101 |----------------+------------------------+--------------------------------------+-------------------------------------|
102 | native mapping | name -> ns/name | no replace, warn-if newval not-core | no replace, warn-if newval not-core |
103 | alias mapping | name -> other/whatever | warn + replace | warn + replace |
104 */
105 private boolean checkReplacement(Symbol sym, Object old, Object neu){
106 if(old instanceof Var) {
107 Namespace ons = ((Var)old).ns;
108 Namespace nns = neu instanceof Var ? ((Var) neu).ns : null;
109
110 if(isInternedMapping(sym, old)){
111 if(nns != RT.CLOJURE_NS){
112 RT.errPrintWriter().println("REJECTED: attempt to replace interned var "
113 + old + " with " + neu + " in " + name + ", you must ns-unmap first");
114 return false;
115 }
116 else
117 return false;
118 }
119 }
120 RT.errPrintWriter().println("WARNING: " + sym + " already refers to: " + old + " in namespace: " + name
121 + ", being replaced by: " + neu);
122 return true;
91123 }
92124
93125 Object reference(Symbol sym, Object val){
95127 {
96128 throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
97129 }
130
98131 IPersistentMap map = getMappings();
99132 Object o;
100133 while((o = map.valAt(sym)) == null)
106139 if(o == val)
107140 return o;
108141
109 warnOrFailOnReplace(sym, o, val);
110
111 while(!mappings.compareAndSet(map, map.assoc(sym, val)))
112 map = getMappings();
113
114 return val;
115
142 if(checkReplacement(sym, o, val)){
143 while (!mappings.compareAndSet(map, map.assoc(sym, val)))
144 map = getMappings();
145
146 return val;
147 }
148
149 return o;
116150 }
117151
118152 public static boolean areDifferentInstancesOfSameClassName(Class cls1, Class cls2) {
1515
1616 public abstract class Obj implements IObj, Serializable {
1717
18 private static final long serialVersionUID = 802029099426284526L;
19
1820 final IPersistentMap _meta;
1921
2022 public Obj(IPersistentMap meta){
2626 * <p>null keys and values are ok, but you won't be able to distinguish a null value via valAt - use contains/entryAt</p>
2727 */
2828
29 public class PersistentArrayMap extends APersistentMap implements IObj, IEditableCollection, IMapIterable, IKVReduce{
29 public class PersistentArrayMap extends APersistentMap implements IObj, IEditableCollection, IMapIterable, IKVReduce, IDrop{
30
31 private static final long serialVersionUID = -2074065891090893601L;
3032
3133 final Object[] array;
3234 static final int HASHTABLE_THRESHOLD = 16;
347349 return null;
348350 }
349351
352 public Sequential drop(int n) {
353 if(array.length > 0) {
354 return ((Seq) seq()).drop(n);
355 } else {
356 return null;
357 }
358 }
359
350360 public IPersistentMap meta(){
351361 return _meta;
352362 }
353363
354 static class Seq extends ASeq implements Counted{
364 static class Seq extends ASeq implements Counted, IReduce, IDrop {
355365 final Object[] array;
356366 final int i;
357367
380390 return (array.length - i) / 2;
381391 }
382392
393 public Sequential drop(int n) {
394 if(n < count()) {
395 return new Seq(array, i + (2 * n));
396 } else {
397 return null;
398 }
399 }
400
383401 public Obj withMeta(IPersistentMap meta){
384402 if(meta() == meta)
385403 return this;
386404 return new Seq(meta, array, i);
387405 }
406
407 public Iterator iterator() {
408 return new Iter(array, i-2, APersistentMap.MAKE_ENTRY);
409 }
410
411 public Object reduce(IFn f) {
412 if(i < array.length) {
413 Object acc = MapEntry.create(array[i], array[i+1]);
414 for(int j=i+2;j < array.length;j+=2){
415 acc = f.invoke(acc, MapEntry.create(array[j], array[j+1]));
416 if(RT.isReduced(acc))
417 return ((IDeref)acc).deref();
418 }
419 return acc;
420 } else {
421 return f.invoke();
422 }
423 }
424
425 public Object reduce(IFn f, Object init) {
426 Object acc = init;
427 for(int j=i;j < array.length;j+=2){
428 acc = f.invoke(acc, MapEntry.create(array[j], array[j+1]));
429 if(RT.isReduced(acc))
430 return ((IDeref)acc).deref();
431 }
432 return acc;
433 }
388434 }
389435
390436 static class Iter implements Iterator{
391 IFn f;
392 Object[] array;
437 final IFn f;
438 final Object[] array;
393439 int i;
394440
395441 //for iterator
2525 */
2626
2727 public class PersistentHashMap extends APersistentMap implements IEditableCollection, IObj, IMapIterable, IKVReduce {
28
29 private static final long serialVersionUID = -8682496769319143320L;
2830
2931 final int count;
3032 final INode root;
1414 import java.util.List;
1515
1616 public class PersistentHashSet extends APersistentSet implements IObj, IEditableCollection {
17
18 private static final long serialVersionUID = 6973890746204954938L;
1719
1820 static public final PersistentHashSet EMPTY = new PersistentHashSet(null, PersistentHashMap.EMPTY);
1921
1414
1515 public class PersistentList extends ASeq implements IPersistentList, IReduce, List, Counted {
1616
17 private static final long serialVersionUID = -8833289659955219995L;
18
1719 private final Object _first;
1820 private final IPersistentList _rest;
1921 private final int _count;
2222 */
2323
2424 public class PersistentQueue extends Obj implements IPersistentList, Collection, Counted, IHashEq{
25
26 private static final long serialVersionUID = 8247184423915313132L;
2527
2628 final public static PersistentQueue EMPTY = new PersistentQueue(null, 0, null, null);
2729
1717 import java.util.NoSuchElementException;
1818
1919 public class PersistentStructMap extends APersistentMap implements IObj{
20
21 private static final long serialVersionUID = -2701411408470234065L;
2022
2123 public static class Def implements Serializable{
2224 final ISeq keys;
1818 import java.util.NoSuchElementException;
1919 import java.util.concurrent.atomic.AtomicReference;
2020
21 public class PersistentVector extends APersistentVector implements IObj, IEditableCollection, IReduce, IKVReduce{
21 public class PersistentVector extends APersistentVector implements IObj, IEditableCollection, IReduce, IKVReduce, IDrop{
22
23 private static final long serialVersionUID = -7896022351281214157L;
2224
2325 public static class Node implements Serializable {
2426 transient public final AtomicReference<Thread> edit;
362364 return init;
363365 }
364366
365 static public final class ChunkedSeq extends ASeq implements IChunkedSeq,Counted{
367 public Sequential drop(int n) {
368 if(n < 0) {
369 return this;
370 } else if(n < cnt) {
371 int offset = n%32;
372 return new ChunkedSeq(this, this.arrayFor(n), n-offset, offset);
373 } else {
374 return null;
375 }
376 }
377
378 static public final class ChunkedSeq extends ASeq implements IChunkedSeq,Counted,IReduce,IDrop{
366379
367380 public final PersistentVector vec;
368381 final Object[] node;
426439
427440 public int count(){
428441 return vec.cnt - (i + offset);
442 }
443
444 public Iterator iterator() {
445 return vec.rangedIterator(i +offset, vec.cnt);
446 }
447
448 public Object reduce(IFn f) {
449 Object acc;
450 if (i +offset < vec.cnt)
451 acc = node[offset];
452 else
453 return f.invoke();
454
455 for(int j=offset+1;j<node.length;++j){
456 acc = f.invoke(acc,node[j]);
457 if(RT.isReduced(acc))
458 return ((IDeref)acc).deref();
459 }
460
461 int step = 0;
462 for(int ii = i +node.length; ii<vec.cnt; ii+=step){
463 Object[] array = vec.arrayFor(ii);
464 for(int j = 0;j<array.length;++j){
465 acc = f.invoke(acc,array[j]);
466 if(RT.isReduced(acc))
467 return ((IDeref)acc).deref();
468 }
469 step = array.length;
470 }
471 return acc;
472 }
473
474 public Object reduce(IFn f, Object init) {
475 Object acc = init;
476 for(int j=offset;j<node.length;++j){
477 acc = f.invoke(acc,node[j]);
478 if(RT.isReduced(acc))
479 return ((IDeref)acc).deref();
480 }
481
482 int step = 0;
483 for(int ii = i +node.length; ii<vec.cnt; ii+=step){
484 Object[] array = vec.arrayFor(ii);
485 for(int j = 0;j<array.length;++j){
486 acc = f.invoke(acc,array[j]);
487 if(RT.isReduced(acc))
488 return ((IDeref)acc).deref();
489 }
490 step = array.length;
491 }
492 return acc;
493 }
494
495 public Sequential drop(int n) {
496 int o = offset + n;
497 if(o < node.length) { // in current array
498 return new ChunkedSeq(vec, node, i, o);
499 } else {
500 int i = this.i +o;
501 if(i < vec.cnt) { // in vec
502 Object[] array = vec.arrayFor(i);
503 int newOffset = i%32;
504 return new ChunkedSeq(vec, vec.arrayFor(i), i-newOffset, newOffset);
505 } else {
506 return null;
507 }
508 }
429509 }
430510 }
431511
1717 */
1818 public class Range extends ASeq implements IChunkedSeq, IReduce {
1919
20 private static final long serialVersionUID = -71973733672395145L;
21
2022 private static final int CHUNK_SIZE = 32;
2123
2224 // Invariants guarantee this is never an "empty" seq
98100 public Obj withMeta(IPersistentMap meta){
99101 if(meta == _meta)
100102 return this;
101 return new Range(meta, end, start, step, boundsCheck, _chunk, _chunkNext);
103 return new Range(meta, start, end, step, boundsCheck, _chunk, _chunkNext);
102104 }
103105
104106 public Object first(){
1616 import java.math.MathContext;
1717
1818 public class Ratio extends Number implements Comparable{
19
20 private static final long serialVersionUID = -576272795628662988L;
21
1922 final public BigInteger numerator;
2023 final public BigInteger denominator;
2124
1111
1212 /* Alex Miller, Dec 5, 2014 */
1313
14 public class Repeat extends ASeq implements IReduce {
14 public class Repeat extends ASeq implements IReduce, IDrop {
15
16 private static final long serialVersionUID = -5140377547192202551L;
1517
1618 private static final long INFINITE = -1;
1719
9698 }
9799 }
98100
101 public Sequential drop(int n) {
102 if(count == INFINITE) {
103 return this;
104 } else {
105 long droppedCount = count-n;
106 if(droppedCount > 0) {
107 return new Repeat(droppedCount, val);
108 } else {
109 return null;
110 }
111 }
99112 }
113
114 }
99 package clojure.lang;
1010
1111 public abstract class RestFn extends AFunction{
12
13 private static final long serialVersionUID = -4319097849151802009L;
14
1215 abstract public int getRequiredArity();
1316
1417 protected Object doInvoke(Object args) {
1111
1212 package clojure.lang;
1313
14 public class StringSeq extends ASeq implements IndexedSeq{
14 import java.util.Iterator;
15 import java.util.NoSuchElementException;
16
17 public class StringSeq extends ASeq implements IndexedSeq,IDrop,IReduceInit{
18
19 private static final long serialVersionUID = 7975525539139301753L;
20
1521 public final CharSequence s;
1622 public final int i;
1723
5056 public int count(){
5157 return s.length() - i;
5258 }
59
60 public Sequential drop(int n) {
61 int ii = i + n;
62 if (ii < s.length()) {
63 return new StringSeq(_meta, s, ii);
64 } else {
65 return null;
66 }
5367 }
68
69 public Object reduce(IFn f, Object start) {
70 Object acc = start;
71 for(int ii=i; ii < s.length(); ii++) {
72 acc = f.invoke(acc, s.charAt(ii));
73 if(RT.isReduced(acc))
74 return ((IDeref)acc).deref();
75 }
76 return acc;
77 }
78
79 }
1616
1717
1818 public class Symbol extends AFn implements IObj, Comparable, Named, Serializable, IHashEq{
19
20 private static final long serialVersionUID = 1191039485148212259L;
21
1922 final String ns;
2023 final String name;
2124 private int _hasheq;
1717
1818
1919 public final class Var extends ARef implements IFn, IRef, Settable, Serializable{
20
21 private static final long serialVersionUID = 8368961370796295279L;
2022
2123 static class TBox{
2224
88 ; Author: Stuart Halloway
99
1010 (ns clojure.test-clojure.rt
11 (:require clojure.set)
11 (:require [clojure.string :as string]
12 clojure.set)
1213 (:use clojure.test clojure.test-helper))
1314
1415 (defn bare-rt-print
7475 (.bindRoot #'example-var 0)
7576 (is (not (contains? (meta #'example-var) :macro))))
7677
77 (deftest last-var-wins-for-core
78 (deftest ns-intern-policies
7879 (testing "you can replace a core name, with warning"
7980 (let [ns (temp-ns)
80 replacement (gensym)]
81 (with-err-string-writer (intern ns 'prefers replacement))
81 replacement (gensym)
82 e1 (with-err-string-writer (intern ns 'prefers replacement))]
83 (is (string/starts-with? e1 "WARNING"))
8284 (is (= replacement @('prefers (ns-publics ns))))))
83 (testing "you can replace a name you defined before"
85 (testing "you can replace a defined alias"
8486 (let [ns (temp-ns)
8587 s (gensym)
8688 v1 (intern ns 'foo s)
87 v2 (intern ns 'bar s)]
88 (with-err-string-writer (.refer ns 'flatten v1))
89 (.refer ns 'flatten v2)
89 v2 (intern ns 'bar s)
90 e1 (with-err-string-writer (.refer ns 'flatten v1))
91 e2 (with-err-string-writer (.refer ns 'flatten v2))]
92 (is (string/starts-with? e1 "WARNING"))
93 (is (string/starts-with? e2 "WARNING"))
9094 (is (= v2 (ns-resolve ns 'flatten)))))
91 (testing "you cannot intern over an existing non-core name"
92 (let [ns (temp-ns 'clojure.set)
93 replacement (gensym)]
94 (is (thrown? IllegalStateException
95 (intern ns 'subset? replacement)))
96 (is (nil? ('subset? (ns-publics ns))))
97 (is (= #'clojure.set/subset? ('subset? (ns-refers ns))))))
98 (testing "you cannot refer over an existing non-core name"
99 (let [ns (temp-ns 'clojure.set)
100 replacement (gensym)]
101 (is (thrown? IllegalStateException
102 (.refer ns 'subset? #'clojure.set/intersection)))
103 (is (nil? ('subset? (ns-publics ns))))
104 (is (= #'clojure.set/subset? ('subset? (ns-refers ns)))))))
95 (testing "you cannot replace an interned var"
96 (let [ns1 (temp-ns)
97 ns2 (temp-ns)
98 v1 (intern ns1 'foo 1)
99 v2 (intern ns2 'foo 2)
100 e1 (with-err-string-writer (.refer ns1 'foo v2))]
101 (is (string/starts-with? e1 "REJECTED"))
102 (is (= v1 (ns-resolve ns1 'foo))))))
103
780780
781781 (partition 5 [1 2 3]) ()
782782
783 (partition 4 4 [0 0 0] (range 10)) '((0 1 2 3) (4 5 6 7) (8 9 0 0))
784
783785 ; (partition 0 [1 2 3]) (repeat nil) ; infinite sequence of nil
784786 (partition -1 [1 2 3]) ()
785787 (partition -2 [1 2 3]) () )
795797 (is (= (assoc (array-map (repeat 1 :x) :y) '(:x) :z) {'(:x) :z}))
796798 (is (= (assoc (hash-map (repeat 1 :x) :y) '(:x) :z) {'(:x) :z})))
797799
800 (deftest test-partitionv
801 (are [x y] (= x y)
802 (partitionv 2 [1 2 3]) '((1 2))
803 (partitionv 2 [1 2 3 4]) '((1 2) (3 4))
804 (partitionv 2 []) ()
805
806 (partitionv 2 3 [1 2 3 4 5 6 7]) '((1 2) (4 5))
807 (partitionv 2 3 [1 2 3 4 5 6 7 8]) '((1 2) (4 5) (7 8))
808 (partitionv 2 3 []) ()
809
810 (partitionv 1 []) ()
811 (partitionv 1 [1 2 3]) '((1) (2) (3))
812
813 (partitionv 4 4 [0 0 0] (range 10)) '([0 1 2 3] [4 5 6 7] [8 9 0 0])
814
815 (partitionv 5 [1 2 3]) ()
816
817 (partitionv -1 [1 2 3]) ()
818 (partitionv -2 [1 2 3]) () ))
798819
799820 (deftest test-iterate
800821 (are [x y] (= x y)
9851006 () '(1 2)
9861007 [] [1 2]
9871008 {} {:a 1 :b 2}
988 #{} #{1 2} ))
1009 #{} #{1 2})
1010
1011 ; CLJ-2718
1012 (is (= '(:a) (drop 1 (repeat 2 :a))))
1013 (is (= () (drop 2 (repeat 2 :a))))
1014 (is (= () (drop 3 (repeat 2 :a)))))
9891015
9901016 (defspec longrange-equals-range 1000
9911017 (prop/for-all [start gen/int
10561082
10571083 (reduce + (iterator-seq (.iterator (range 100)))) 4950
10581084 (reduce + (iterator-seq (.iterator (range 0.0 100.0 1.0)))) 4950.0 ))
1085
1086 (deftest range-meta
1087 (are [r] (= r (with-meta r {:a 1}))
1088 (range 10)
1089 (range 5 10)
1090 (range 5 10 1)
1091 (range 10.0)
1092 (range 5.0 10.0)
1093 (range 5.0 10.0 1.0)))
10591094
10601095 (deftest range-test
10611096 (let [threads 10
11181153 {}
11191154 #{}
11201155 ""
1121 (into-array []) )
1156 (into-array [])
1157 (transient [])
1158 (transient #{})
1159 (transient {}))
11221160
11231161 (are [x] (not (empty? x))
11241162 '(1 2)
11271165 {:a 1 :b 2}
11281166 #{1 2}
11291167 "abc"
1130 (into-array [1 2]) ))
1168 (into-array [1 2])
1169 (transient [1])
1170 (transient #{1})
1171 (transient {1 2})))
11311172
11321173
11331174 (deftest test-every?
13301371 (is (= (partition-all 4 2 [1 2 3 4 5 6 7 8 9])
13311372 [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]])))
13321373
1374 (deftest test-partitionv-all
1375 (is (= (partitionv-all 4 [1 2 3 4 5 6 7 8 9])
1376 [[1 2 3 4] [5 6 7 8] [9]]))
1377 (is (= (partitionv-all 4 2 [1 2 3 4 5 6 7 8 9])
1378 [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]])))
1379
13331380 (deftest test-shuffle-invariants
13341381 (is (= (count (shuffle [1 2 3 4])) 4))
13351382 (let [shuffled-seq (shuffle [1 2 3 4])]
14571504 :kf #(some-> % :k #{0 1 2})
14581505 :vf :item))))))
14591506
1507 (deftest test-reduce-on-coll-seqs
1508 ;; reduce on seq of coll, both with and without an init
1509 (are [coll expected expected-init]
1510 (and
1511 (= expected-init (reduce conj [:init] (seq coll)))
1512 (= expected (reduce conj (seq coll))))
1513 ;; (seq [ ... ])
1514 [] [] [:init]
1515 [1] 1 [:init 1]
1516 [[1] 2] [1 2] [:init [1] 2]
1517
1518 ;; (seq { ... })
1519 {} [] [:init]
1520 {1 1} [1 1] [:init [1 1]]
1521 {1 1 2 2} [1 1 [2 2]] [:init [1 1] [2 2]]
1522
1523 ;; (seq (hash-map ... ))
1524 (hash-map) [] [:init]
1525 (hash-map 1 1) [1 1] [:init [1 1]]
1526 (hash-map 1 1 2 2) [1 1 [2 2]] [:init [1 1] [2 2]]
1527
1528 ;; (seq (sorted-map ... ))
1529 (sorted-map) [] [:init]
1530 (sorted-map 1 1) [1 1] [:init [1 1]]
1531 (sorted-map 1 1 2 2) [1 1 [2 2]] [:init [1 1] [2 2]])
1532
1533 (are [coll expected expected-init]
1534 (and
1535 (= expected-init (reduce + 100 (seq coll)))
1536 (= expected (reduce + (seq coll))))
1537
1538 ;; (seq (range ...))
1539 (range 0) 0 100
1540 (range 1 2) 1 101
1541 (range 1 3) 3 103))
1542
14601543 (defspec iteration-seq-equals-reduce 1000
14611544 (prop/for-all [initk gen/int
14621545 seed gen/int]