Fork me on GitHub
#clojure
<
2020-05-31
>
Kanishk Kumar08:05:34

(time ((fn prime-numbers?
    ([num]
     (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)
         coll
         (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

andy.fingerhut15:05:30

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

andy.fingerhut15:05:25

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.

andy.fingerhut15:05:57

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

dominicm15:05:45

*defn in your first snippet Andy

andy.fingerhut15:05:25

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.

andy.fingerhut15:05:10

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

zilti16:05:47

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.

vemv16:05:06

like this?

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

p-himik19:05:32

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.

kwladyka19:05:10

What is the use case for this need?

lvh20:05:54

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
rutledgepaulv00:06:22

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?

lvh14:06:46

my immediate application is terraform hcl

lvh14:06:55

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

rutledgepaulv18:06:19

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

rutledgepaulv18:06:59

somewhat related, I ran across https://www.pulumi.com/ recently and it seemed interesting