Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package primes;
- import clojure.lang.Compiler;
- import clojure.lang.*;
- public final class MainW {
- public static final Var primes_primes_var = RT.var("primes", "primes");
- public static final IFn iterate = (IFn) RT.var("clojure.core", "iterate").getRawRoot();
- public static final IFn inc = (IFn) RT.var("clojure.core", "inc").getRawRoot();
- public static final IFn not = (IFn) RT.var("clojure.core", "not").getRawRoot();
- public static final IFn some = (IFn) RT.var("clojure.core", "some").getRawRoot();
- public static final IFn mod = (IFn) RT.var("clojure.core", "mod").getRawRoot();
- public static final IFn cons = (IFn) RT.var("clojure.core", "cons").getRawRoot();
- static class Divides extends AFunction {
- Object x;
- public Divides(Object x) {
- this.x = x;
- }
- public Object invoke(Object v) {
- return Numbers.isZero(mod.invoke(this.x, v));
- }
- }
- static Object checkIfPrime(Object x) {
- return not.invoke(some.invoke(new Divides(x), primes_primes_var.getRawRoot()));
- }
- static class FilterLazySeqFn extends AFunction {
- Object coll;
- public FilterLazySeqFn(Object coll) {
- this.coll = coll;
- }
- public Object invoke() {
- Object coll = this.coll;
- this.coll = null;
- ISeq seq_coll = RT.seq(coll);
- if (seq_coll != null) {
- Object f = seq_coll.first();
- Object r = seq_coll.more();
- Object pred_result = checkIfPrime(f);
- if (pred_result != null && pred_result != Boolean.FALSE) {
- return cons.invoke(f, filter(r));
- }
- return filter(r);
- }
- return null;
- }
- }
- public static Object filter(Object coll) {
- return new LazySeq(new FilterLazySeqFn(coll));
- }
- public static Object get() {
- Compiler.pushNSandLoader(RT.classForName("primes.MainW").getClassLoader());
- Object result;
- try {
- Object coll = iterate.invoke(inc, 2L);
- primes_primes_var.bindRoot(filter(coll));
- result = RT.nth(primes_primes_var.getRawRoot(), 2);
- } catch (Throwable var1) {
- Var.popThreadBindings();
- throw var1;
- }
- Var.popThreadBindings();
- return result;
- }
- public static void main(String[] args) {
- System.out.println(get());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement