Fork me on GitHub
#beginners
<
2020-02-03
>
jwoods03:02:03

This might have already been mentioned here but I can't seem to find anything that is like the standard. I'm looking for the standard lib to connect to a db like postgres. I'm coming from C# that has Entity Framework and Elixir that has Ecto what is the Clojure equivalent?

seancorfield03:02:39

Clojure doesn't do "ORMs" because it doesn't bother with "objects", just raw data. So you read a vector of hash maps from a database, and you insert hash maps into a database. Happy to follow up in #sql for a deep dive @jdwoodsy1

👍 4
hindol05:02:08

Hi, what would be an easy way to conditionally memoize-ify a fn? I am implementing a fn int -> int where I only want prime inputs to be memoized. Do I have to roll my own with an atom?

seancorfield05:02:37

@hindol.adhya That seems a pretty strange requirement but, yeah, I think you'd have to implement your own memoization function. But clojure.core.cache might be worth looking at since you can build custom caches with it.

hindol05:02:59

To be more specific, I am defining the kempner function which for a non-prime is the max of (kempner of each prime factor and its power). Memoizing composites as well will consume too much memory.

hindol05:02:03

Here is where I am,

(def kempner
  #_(memoize)
  (fn
    ([^long x]
     (apply max
            (for [[p e] (frequencies (prime-factorize x))]
              (kempner p e))))
    ([p e]
     (let [xf (comp
               (map second)
               (map prime-factorize)
               (map count))]
       (* p
          (inc
           (count
            (take-while #(< % e)
                        (->> (multiples-of p)
                             (eduction xf)
                             (reductions +))))))))))

andy.fingerhut05:02:37

You could copy the implementation of memoize from Clojure, probably give it a different name, and modify it by adding your custom condition.

jysandy05:02:26

@hindol.adhya you could also: 1. decompose your function into calling a separate function with only prime inputs, and take another code path for the rest 2. memoize the new function that now accepts only prime inputs.

👍 12
hindol05:02:00

That's what I was thinking but I am not finding a good name for the inner function, :man-shrugging:

andy.fingerhut06:02:55

same name as the outer function with "-prime-only" appended?

Piotr Brzeziński21:02:10

(Noob question incoming). How often in day-to-day coding do you run into situation where you forget to use (`into …` ) to convert your data structure back into what you need (map/vector/whatever)? I’m curious because it looks like a thing I’ll be forgetting to do all the time.

noisesmith21:02:31

@peb.brzezinski it happens rarely for me, but occasionally as I was learning the language. I think one insight that helps prevent the problem is that most clojure functions aren't generic (conj and into which is effectively (partial reduce conj) in vanilla usage being exceptions)

Piotr Brzeziński21:02:58

Ok cool, thank you 🙂

mloughlin21:02:26

in the :args part of an fdef, how do I apply a spec to only the second argument (of two)?

mloughlin21:02:24

I thought using (s/cat :key ::key-spec) would map it based on keywording the arg name, but my understanding is wrong

seancorfield21:02:59

@michael.e.loughlin (s/cat :arg1 any? :arg2 ::some-spec)

Piotr Brzeziński21:02:10

I’m continuing to work through a clojure book and I just created a project using lein new app fwdp and in core.clj I’ve got

(ns fwdp.core)

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "Hello world!"))

(def filename "suspects.csv")
now when I try to M-x cider-jack-in-clj I get an error
Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
Exception in thread "main" Syntax error compiling at (/private/var/folders/sx/qt3tl5w958xgjdp7yt458h500000gn/T/form-init12838498358722132617.clj:4353:33).
....
did I make some wierd typo?

Piotr Brzeziński21:02:20

^ nevermind my question. For some reason it worked after I removed main definition ¯\(ツ)

noisesmith21:02:31

there's nothing wrong with that main definition, and that warning isn't an error

noisesmith21:02:52

are you sure it didn't just take much longer to start than you expected?

Piotr Brzeziński21:02:27

Ohhhh yeah, could be, since it’s a new project. facepalm

noisesmith21:02:55

also I seem to recall recent complaints about cider startup time, there might be an issue right now(?)

Piotr Brzeziński21:02:57

I don’t think my problem is related. It works fine after the initial compilation of project. I just forgot that I don’t need to try and restart it 28 times if it takes longer than 10 seconds.