Fork me on GitHub
hipster coder17:03:30

@noisesmith like the CLR (common language runtime)?

hipster coder17:03:14

Ahhh, that says it is written on .net


and clojurescript on js yeah

hipster coder17:03:35

I am reading about Graal for native machine code compilations

hipster coder17:03:32

I should try writing a performance test to see how much faster Graal could be?


Graal will be slower at peak, but faster at startup


It also has a ton of restrictions


Can’t reflect, some parts of the JDK won’t work, etc.


It’s a weird non-Java environment

hipster coder17:03:31

It sounds like a bad idea? I thought Java Virtual Machine only ran bytecode?


I would call it frozen dairy dessert, not ice cream


Java already JITs to machine code, quite well


I've got a bit of code that produces a map from a list of maps, where each of those original maps has a value i'm using as the key in the produced map. e.g, turning [{:id 1 :name "foo" :tag "wee"} {:id 2 :name "bar" :tag "splat"}] into {1 {:id 1 :name "foo" :tag "wee"} 2 {:id 2 :name "bar" :tag "splat"}}


right now I'm using a reduce, is there a more idiomatic option?


e.g. (reduce #(assoc %1 (:id %2) %2) {} my-coll)


that's good @robertfrederickwarner. You can generalize it, too:

(defn into-map
  [keyf valf coll]
  (reduce (fn [m v]
            (assoc m (keyf v) (valf v)))


(def d [{:id 1 :name "foo" :tag "wee"} {:id 2 :name "bar" :tag "splat"}])

(group-by :id d)
=> {1 [{:id 1, :name "foo", :tag "wee"}], 2 [{:id 2, :name "bar", :tag "splat"}]}
This works, but might not be what you want. Group-by assumes you might have duplicates, so you get a vector of maps back instead of one


Thanks! Yeah group-by isn't quite the right fit here (I'm converting results from a db query into a lookup, so I know there are no dupes), but another handy function to know about


or even:

(defn index-by
  ([keyf coll]
   (index-by keyf identity coll))
  ([keyf valf coll]
   (reduce (fn [m v]
             (let [k (keyf v)]
               (if (find m k)
                 (throw (ex-info "Duplicate key" {:k k}))
                 (assoc m k (valf v)))))
  ([keyf valf mergef coll]
   (reduce (fn [m v]
             (let [k (keyf v)]
               (if-let [entry (find m k)]
                 (assoc m k (mergef (val entry) (valf v)))
                 (assoc m k (valf v)))))


sorry, no docstring


(index-by :id collection) (it would throw an exception on a duplicate key)


dang, none of those impls in the reddit link throw if you get non-unique values


i ran across this tweet this morning: "A Clojure love story: Presses "p" in a specific place in the file. Now the program runs faster. The end." What would he be referring to here? Is this a clojure way to make something "parallel" or something?


cool, thanks! I'm far from needing to learn optimizations and such at this point but it's still fun to explore

Lennart Buit20:03:45

there are no magic bullets in programming. (Implicit) parallelisation is no exception to the rule

Lennart Buit20:03:08

I think I linked it here a dozen of times, but is quite insightful about performance

Lennart Buit20:03:53

Maybe a bit of a stupid question, I am using a wrapper around java.time that wraps java.time’s exception into an ExceptionInfo. Can I catch warpped exceptions more specifically then just catching ExceptionInfo (or Exception, or …)


@lennart.buit (try ... (catch ExceptionInfo ....) (catch Exception ...)) works


I forget if it goes with first match or most precise, but I always order them narrow to wide regardless

Lennart Buit20:03:46

right, catching Exception works in my case, I was just wondering whether there was a way to be more specific about what wrapped exception I was expecting. Having been taught to always catch the most specific one ^^


you can't even catch a wrapped exception - you catch something and then check if it wraps something


you could write a macro that makes it look like you are catching a wrapped exception I guess

Lennart Buit20:03:06

I think I’ll check the cause, and if it doesn’t make sense, just re-raise it

Lennart Buit20:03:14

for the error reporting to pick it up ^^


yeah, that's how I always do a selective catch that can't just go on the type


try / catch / maybe throw

Jorge Hamilton21:03:05

I have some scripts that I need to run. I am on osx. What command do I need to run? I know a super beginner questions


@bmaddy adding a solution for the nested group-by above:

(defn nest-by
  [ks coll]
   (fn [m x] (update-in m ((apply juxt ks) x) (fnil conj []) x))
this avoids the recursion. Feels right to use update-in for this and I find this solution beautiful (from s.o. so not my creation)

👍 4

@undothis When you say "scripts", do you mean Clojure or something else?

Mario C.21:03:44

Perhaps he is referring to the shebang? #!/usr/bin/env bash

Jorge Hamilton22:03:52

I basically got src, results, project.cjl, lib folders and I have no idea how to run it. hopefully that makes sense

Jorge Hamilton22:03:42

I am using an Apple by the way. So I CD into the folder and type that in?


@undothis That sounds like a Leiningen project (because of project.clj). Do you have Leiningen installed?


It sounds like you need to work through some intro-to-Clojure material starting from the basics (getting Leiningen installed, running a REPL, creating a new project from scratch or with Leiningen). Have you looked at Clojure for the Brave and True? Both a book and an online tutorial.