Fork me on GitHub
Kanishk Kumar08:05:34

(time ((fn prime-numbers?
     (prime-numbers? 2 (range 2 (inc num))))
    ([p, coll]
     (let [multiples-of-prime (drop 2 (range 0 (inc (last coll)) p))
           remaining-coll (remove (into #{} multiples-of-prime) coll)
           next-num (nth remaining-coll (inc (.indexOf remaining-coll p)) nil)]
       (if (nil? next-num)
         (recur next-num remaining-coll))))) 9927))
Please give feedbacks on the implementation of Sieve_of_Eratosthenes for generating primes

Kanishk Kumar15:05:17

How is defn- different from defn


(defn- foo [x] ,,,) is the same as (defn ^:private foo [x] ,,,)


It means that if you are in a different namespace than where foo was defined, you cannot use the normal way to refer to it, e.g. other.ns/foo will give an error, because it is private.


You can still refer to it using #'other.ns/foo as a workaround for it being private, if you wish.


*defn in your first snippet Andy


So private in Clojure does not absolutely prevent you from using it from outside the namespace, it simply means you have to make a small explicit jump-through-a-hoop to use it.


For those reading this later, I edited my earlier message that dominicm pointed out a typo in, to correct it. Thanks for noticing that.

Kanishk Kumar15:05:12

Is something similar to a private function in OOPs


Is there a good way to match a namespaced keyword key without knowing the namespace? e.g. when I have a keyword :employee/last-name as a key in a map, and I want to get the value without knowing the keyword namespace.


like this?

(let [the-map {:foo/last-name 2}
      [v]  (keep (fn [[k v]]
                   (when (-> k name #{"last-name"})
  (println v))


Two issues: it creates an extra lazy collection and it traverses the entire map even though you just need the very first found key (at least, as far as I understand). It's possible to use reduce + reduced or just loop if the points above matter.


What is the use case for this need?


Does anyone have a suggestion for a parser generator that also produces serializers? My ideal case is something that takes an ABNF, parses source, lets me modify the resulting AST, and goes back to source.

👀 4

I'm not aware of one (aside from tools.anaylzer if this is a clojure dialect) but I suppose your other option is to use antlr and a custom visitor to go back to source? I imagine the difficulty depends on how complex your language is and how much you care about preserving comments / formatting. Are there particular things about the serialization process you're thinking about that would make writing a visitor difficult?


my immediate application is terraform hcl


so my main concern is that it's not particularly consistent or well-documented


got it. yeah i don't think there's even an antlr grammar for hcl yet... i'm not even sure if hcl is a cfg or not


somewhat related, I ran across recently and it seemed interesting