view src/clojure/lang/Range.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 /**
2 * Copyright (c) Rich Hickey. All rights reserved.
3 * The use and distribution terms for this software are covered by the
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5 * which can be found in the file epl-v10.html at the root of this distribution.
6 * By using this software in any fashion, you are agreeing to be bound by
7 * the terms of this license.
8 * You must not remove this notice, or any other, from this software.
9 **/
11 /* rich Apr 1, 2008 */
13 package clojure.lang;
15 public class Range extends ASeq implements IReduce, Counted{
16 final int end;
17 final int n;
19 public Range(int start, int end){
20 this.end = end;
21 this.n = start;
22 }
24 public Range(IPersistentMap meta, int start, int end){
25 super(meta);
26 this.end = end;
27 this.n = start;
28 }
30 public Obj withMeta(IPersistentMap meta){
31 if(meta == meta())
32 return this;
33 return new Range(meta(), end, n);
34 }
36 public Object first(){
37 return n;
38 }
40 public ISeq next(){
41 if(n < end-1)
42 return new Range(_meta, n + 1, end);
43 return null;
44 }
46 public Object reduce(IFn f) throws Exception{
47 Object ret = n;
48 for(int x = n+1;x < end;x++)
49 ret = f.invoke(ret, x);
50 return ret;
51 }
53 public Object reduce(IFn f, Object start) throws Exception{
54 Object ret = f.invoke(start,n);
55 for(int x = n+1;x < end;x++)
56 ret = f.invoke(ret, x);
57 return ret;
58 }
60 public int count() {
61 return end - n;
62 }
64 }