Advertisement
Guest User

For `:once true`

a guest
Apr 29th, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package primes;
  2.  
  3. import clojure.lang.Compiler;
  4. import clojure.lang.*;
  5.  
  6. public final class MainW {
  7.     public static final Var primes_primes_var = RT.var("primes", "primes");
  8.     public static final IFn iterate = (IFn) RT.var("clojure.core", "iterate").getRawRoot();
  9.     public static final IFn inc = (IFn) RT.var("clojure.core", "inc").getRawRoot();
  10.     public static final IFn not = (IFn) RT.var("clojure.core", "not").getRawRoot();
  11.     public static final IFn some = (IFn) RT.var("clojure.core", "some").getRawRoot();
  12.     public static final IFn mod = (IFn) RT.var("clojure.core", "mod").getRawRoot();
  13.     public static final IFn cons = (IFn) RT.var("clojure.core", "cons").getRawRoot();
  14.  
  15.     static class Divides extends AFunction {
  16.         Object x;
  17.  
  18.         public Divides(Object x) {
  19.             this.x = x;
  20.         }
  21.  
  22.         public Object invoke(Object v) {
  23.             return Numbers.isZero(mod.invoke(this.x, v));
  24.         }
  25.     }
  26.  
  27.     static Object checkIfPrime(Object x) {
  28.         return not.invoke(some.invoke(new Divides(x), primes_primes_var.getRawRoot()));
  29.     }
  30.  
  31.     static class FilterLazySeqFn extends AFunction {
  32.         Object coll;
  33.  
  34.         public FilterLazySeqFn(Object coll) {
  35.             this.coll = coll;
  36.         }
  37.  
  38.         public Object invoke() {
  39.             Object coll = this.coll;
  40.             this.coll = null;
  41.             ISeq seq_coll = RT.seq(coll);
  42.             if (seq_coll != null) {
  43.                 Object f = seq_coll.first();
  44.                 Object r = seq_coll.more();
  45.                 Object pred_result = checkIfPrime(f);
  46.                 if (pred_result != null && pred_result != Boolean.FALSE) {
  47.                     return cons.invoke(f, filter(r));
  48.                 }
  49.  
  50.                 return filter(r);
  51.             }
  52.  
  53.             return null;
  54.         }
  55.     }
  56.  
  57.     public static Object filter(Object coll) {
  58.         return new LazySeq(new FilterLazySeqFn(coll));
  59.     }
  60.  
  61.     public static Object get() {
  62.         Compiler.pushNSandLoader(RT.classForName("primes.MainW").getClassLoader());
  63.  
  64.         Object result;
  65.         try {
  66.             Object coll = iterate.invoke(inc, 2L);
  67.             primes_primes_var.bindRoot(filter(coll));
  68.             result = RT.nth(primes_primes_var.getRawRoot(), 2);
  69.         } catch (Throwable var1) {
  70.             Var.popThreadBindings();
  71.             throw var1;
  72.         }
  73.  
  74.         Var.popThreadBindings();
  75.         return result;
  76.     }
  77.  
  78.     public static void main(String[] args) {
  79.         System.out.println(get());
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement