Advertisement
Guest User

For `:once false`

a guest
Apr 29th, 2022
9
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package primes;
  2.  
  3. import clojure.lang.Compiler;
  4. import clojure.lang.*;
  5.  
  6. public final class MainWO {
  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.             ISeq seq_coll = RT.seq(this.coll);
  40.             if (seq_coll != null) {
  41.                 Object f = seq_coll.first();
  42.                 Object r = seq_coll.more();
  43.                 Object pred_f_val = checkIfPrime(f);
  44.                 if (pred_f_val != null && pred_f_val != Boolean.FALSE) {
  45.                     return cons.invoke(f, filter(r));
  46.                 }
  47.  
  48.                 return filter(r);
  49.             }
  50.  
  51.             return null;
  52.         }
  53.     }
  54.  
  55.     public static Object filter(Object coll) {
  56.         return new LazySeq(new FilterLazySeqFn(coll));
  57.     }
  58.  
  59.     public static Object get() {
  60.         Compiler.pushNSandLoader(RT.classForName("primes.MainWO").getClassLoader());
  61.  
  62.         Object result;
  63.         try {
  64.             Object coll = iterate.invoke(inc, 2L);
  65.             primes_primes_var.bindRoot(filter(coll));
  66.             result = RT.nth(primes_primes_var.getRawRoot(), 2);
  67.         } catch (Throwable var1) {
  68.             Var.popThreadBindings();
  69.             throw var1;
  70.         }
  71.  
  72.         Var.popThreadBindings();
  73.         return result;
  74.     }
  75.  
  76.     public static void main(String[] args) {
  77.         System.out.println(get());
  78.     }
  79. }
  80.  
Advertisement
RAW Paste Data Copied
Advertisement