Created
June 11, 2013 10:24
-
-
Save gerritjvv/5755871 to your computer and use it in GitHub Desktop.
Clojure LazySequence From Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lazyseq; | |
import org.junit.Test; | |
import clojure.lang.AFn; | |
import clojure.lang.ASeq; | |
import clojure.lang.ArraySeq; | |
import clojure.lang.Cons; | |
import clojure.lang.IPersistentMap; | |
import clojure.lang.ISeq; | |
import clojure.lang.LazySeq; | |
import clojure.lang.Obj; | |
import java.util.List; | |
public class TestLazySeq { | |
@Test | |
public void testSeq() { | |
final ISeq seq = aseq(0); | |
List list = new LazySeq(new AFn() { | |
public Object invoke(){ | |
return seq; | |
} | |
}); | |
int i = 0; | |
for(Object v : list){ | |
System.out.println(v); | |
if(i++ > 1000) break; | |
} | |
} | |
private static ISeq aseq(final int i){ | |
return new ASeq() { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
public ISeq next() { | |
return aseq(i+1); | |
} | |
public Object first() { | |
return i; | |
} | |
@Override | |
public Obj withMeta(IPersistentMap arg0) { | |
return null; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment