Fork me on GitHub
#beginners
<
2019-03-26
>
hipster coder17:03:30

@noisesmith like the CLR (common language runtime)?

hipster coder17:03:14

Ahhh, that says it is written on .net

noisesmith17:03:32

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?

ghadi17:03:09

Graal will be slower at peak, but faster at startup

ghadi17:03:20

It also has a ton of restrictions

ghadi17:03:48

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

ghadi17:03:06

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?

ghadi17:03:35

I would call it frozen dairy dessert, not ice cream

ghadi17:03:26

Java already JITs to machine code, quite well

robertfw19:03:33

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"}}

robertfw19:03:59

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

robertfw19:03:17

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

ghadi19:03:33

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)))
          {}
          coll))

butterguns19:03:35

(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

robertfw19:03:13

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

ghadi19:03:03

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)))))
           {}
           coll))
  ([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)))))
           {}
           coll)))

ghadi19:03:08

sorry, no docstring

ghadi19:03:39

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

ghadi19:03:40

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

Chase19:03:53

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?

Chase19:03:10

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 http://clojure-goes-fast.com/blog/ 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 …)

noisesmith20:03:24

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

noisesmith20:03:51

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 ^^

noisesmith20:03:34

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

noisesmith20:03:51

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 ^^

noisesmith20:03:23

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

noisesmith20:03:31

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

mbjarland21:03:55

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

(defn nest-by
  [ks coll]
  (reduce
   (fn [m x] (update-in m ((apply juxt ks) x) (fnil conj []) x))
   {}
   coll))
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)

👍 1
seancorfield21:03:57

@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?

seancorfield00:03:41

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

seancorfield00:03:34

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.