Fork me on GitHub
#clojure
<
2017-09-30
>
didibus02:09:11

QUESTION: Anyway I can try catch a let binding, and have access to the bindings in my catch?

noisesmith02:09:04

nothing simple, no

didibus02:09:42

try(
  (let [a 100
         b (throw (ex-info "bla" {})]
   a)
(catch Exception e a))

noisesmith02:09:08

right, that's not even going to compile

noisesmith02:09:46

in some weird cases I've had positive results with binding delays in a let block, forcing the delays in a try, and then checking if they are realized and conditionally accessing them in the catch

noisesmith02:09:08

but you want to be careful you aren't forcing delays that didn't run yet in the catch block of course so it's clumsy

didibus02:09:32

I'm thinking to just let a bunch of atoms, and inside the let do some weird side effect Java style assignments

noisesmith02:09:53

(let [a (delay (foo))
     b (delay (bar @a))]
  (try @b     
     (catch Exception e
        (when (realized? b)
          ...))))

noisesmith02:09:22

you don't need atoms if you are only assigning once

didibus02:09:11

I hate both ways honestly 😛

didibus02:09:43

Just because I want to log some of the bound vars inside the let in the case something throws an exception lol

didibus02:09:32

Is the delay going to properly throw?

didibus02:09:38

When derefed?

noisesmith02:09:01

a delay with a body that throws an error when forced throws the exception when forced - the reason to have the delay is so that a and b are in scope in the catch

didibus02:09:02

Which is why in my catch I need to check for realized? or it would rethrow

noisesmith02:09:53

oh, wait, sadly it doesn't work out that way

+user=> (let [d (delay (/ 1 0))]
          (try @d
               (catch Exception _)))
nil
+user=> (let [d (delay (/ 1 0))]
          (try @d
               (catch Exception _
                  (when (realized? d)
                    @d))))
ArithmeticException Divide by zero  clojure.lang.Numbers.divide (Numbers.java:158)

didibus03:09:13

I guess I have to use atoms then

noisesmith03:09:36

or promise/deliver

noisesmith03:09:30

+user=> (let [p (promise)]
          (try (deliver p (/ 1 0))
               (catch Exception _
                  (when (realized? p)
                    @p))))
nil

noisesmith03:09:03

so you can guard usage of p in the catch block based on whether it's realized

noisesmith03:09:22

it's too bad you can't do that with delays though

didibus03:09:59

Hum, that's almost the same as making p an atom, but its a bit more explicit in the semantics I guess

noisesmith03:09:25

it's a less powerful construct (and performs better)

didibus03:09:53

How come delay was throwing on the deref but not promise though

didibus03:09:05

I mean on the realized?

didibus03:09:24

sounds like a bug

noisesmith03:09:27

it's throwing on deref, but it shows as realized

noisesmith03:09:48

similar to the behavior of future - if it sees an exception it throws on deref

didibus03:09:01

oh ya, I remember that quirk now

didibus03:09:07

so does promise just return nil or the exception, but does not throw it?

noisesmith03:09:48

the exception means that deliver never gets called

noisesmith03:09:22

similar to (println "error" (/ 1 0)) - "error" won't print, the exception prevents println from being called

didibus03:09:32

oh right, ok I see

didibus03:09:34

The downside, if I'm not careful in my catch, I can just forever block my thread

didibus03:09:49

where-as atom would just be nil

bbss05:09:29

I'm having an issue loading my core.clj. It depends on a .clj file in the same directory but it says it's not found on the classpath. The directory is on the classpath though. The weird thing is if I load-file on the depended upon file that runs fine, and after that I can also load-file on the core.clj without issues.

bbss05:09:17

I suspect I messed up namespacing/directory structure or something in the project.clj. https://github.com/bbss/cljsc2/tree/master/src/cljsc2/clj

hkjels08:09:19

https://gist.github.com/hkjels/5637e59933d4c8cebf73d4052d75b2f8 Hi, I wrote this a while back and now I’m having a hard time understanding my own code 😒 Do any of you see where this short-circuits? What I would like is for all of the matches to be returned

hkjels08:09:19

I’m not certain yet, but I think there might be a performance-issue here as well, so if there’s a more idiomatic way of doing this, that would be great!

hkjels08:09:39

still learning

jaymartin09:09:21

@bbss lein run returns No :main namespace specified in project.clj. Perhaps adding the :main key and value will do the trick for you. You can run lein sample for an example.

jaymartin09:09:21

Oh, I don’t see a main fn in core. My suggestion probably won’t help. :thinking_face:

bbss09:09:21

@jaymartin thanks, good suggestion to try anyway.

jaymartin09:09:45

The other that might help is to use a tool like lein-ns-dep-graph to autogenerate a pic of your internal namespaces, and compare the generated image with your understanding of what should be. https://github.com/hilverd/lein-ns-dep-graph

jaymartin09:09:44

Here’s what that program outputs for your proj

jaymartin10:09:05

@bbss how are you actually trying to to load the file? From the REPL? etc.

jaymartin10:09:56

@bbss FWIW, i’m getting the same error msg when running (load "cljsc2.clj.core") from user ns at repl: FileNotFoundException Could not locate cljsc2.clj.core__init.class or cljsc2.clj.core.clj on classpath. clojure.lang.RT.load (RT.java:458)

bbss10:09:08

I'm running from Cider, but lein repl has the same issue.

jaymartin10:09:38

Yep, I was in lein. Now I’m just curious!

bbss10:09:18

That graph looks good, I guess. I'm not near my computer right now to debug.

bbss10:09:54

Thanks for looking at it. Will let you know when I figure it out.

qqq10:09:34

I'm willing to sacrifice pixel equivalency across platforms. Is there something faster than Graphics2D/bufferedImage for drawing text / rectangles / ovals ?

jaymartin10:09:07

@qqq not sure, but you might poke around http://thi.ng/ because I know the author is into performance and well versed in such things.

qqq10:09:32

@jaymartin: I have lost track of the # of times I have tried (but failed) to get into that librry

jaymartin10:09:21

interesting. I’ll keep that in mind.

qqq10:09:03

it's clearly brilliant; I just can't seem to find a tutorial/docs for "how to create an image+ render text with http://thi.ng"

pbaille10:09:04

Is there a straightforward way to turn a simple namespace (containing only defs and defns) into a map?

jaymartin10:09:57

@pbaille Would dir do the trick? Or do you need something more?

jaymartin10:09:52

I think namespaces are maps under the hood

pbaille10:09:52

@jaymartin you mean clojure.tools.namespace/dir ?

jaymartin10:09:25

clojure.repl/dir

pbaille10:09:34

I will try

jaymartin10:09:07

I thought it was in core, thanks for asking.

pbaille10:09:44

Ok just tried it, it just print all the vars names

pbaille10:09:19

I would like a map of kind {var-name var-value}

jaymartin10:09:34

Interesting. I recall namespaces themselves being maps. It might be useful to dig into Clojure source for this one and see if its somethings that is or can be coerced to a map.

pbaille10:09:14

@jaymartin thank you, I was hoping for a simple function but if I have to dive, I will 🙂

jaymartin10:09:46

Hopefully some more experienced folks than I will wake up soon ; )

odie10:09:36

@pbaille You probably want to look at clojure.core/ns-interns or clojure.core/ns-publics

odie10:09:26

something like (ns-publics 'clojure.core), for example, should get you all the public vars in that namespace

pbaille10:09:36

@odie great! thank you

odie10:09:48

you’re very welcome 😃

lsenjov11:09:22

Along those lines, don't forget that meta is a thing

lsenjov11:09:30

With a little bit of convolution you can get all the docstrings and other fun stuff out of a namespace

pbaille11:09:57

nice 🙂 thank you

ttx16:09:24

Never mind. I think the source of starvation/suppression is not clojure.core.async/mix itself. Rather, how the data producers compete for another shared resource and wait for some fixed interval before trying again. Randomizing the interval seems to improve the situation.

gonewest81815:09:51

@qqq I’ll be interested to hear what you decide on. Google turns up the usual mix of OpenGL bindings, game engines, and some stackoverflow commentary on how to optimize use of the standard java 2d libs. Are you rendering offscreen? Or is this onscreen visualization of some sort? Again, I’m just curious.

chalcidfly16:09:48

Need some macro help. I’m trying to walk a form and get meta data for each symbol.

chalcidfly16:09:56

(defmacro meta-sym [sym] `(if (symbol? ‘sym) (-&gt; ‘sym resolve meta) {}))

chalcidfly16:09:54

But (clojure.walk/postwalk #(println (ex/meta-sym %)) (read-string (clojure.repl/source-fn ‘max))) just prints out a bunch of nils

chalcidfly16:09:26

However, I can do (meta-sym defn) and get a full meta map

chalcidfly16:09:18

Think I need @noisesmith and @bronsafor this one

chrisjd17:09:31

@chalcidfly postwalk and friends replace the nodes with the return values of your fn.

chalcidfly17:09:05

So would (clojure.walk/postwalk #(do (println (ex/meta-sym %)) (identity %)) (read-string (clojure.repl/source-fn 'max))) work?

chalcidfly17:09:14

Note the do block that returns the identity

chrisjd17:09:51

Should do, although you don’t need to call identity. Just have % as the last form.

chalcidfly17:09:23

Now it’s printing a bunch of {}. (symbol? ‘~sym) is return false on all of them.

chrisjd17:09:32

This seems to work (I only have my iPhone with Replete to do this on though, sorry): (def a {:foo ^{:test1 123} [] :bar (quote ^{:test2 456} baz)}) (clojure.walk/postwalk #(do (when (symbol? %) (println (meta %))) %) a)

chalcidfly17:09:58

@chrisjd thanks a bunch, looks like the issue was with the data structure I was trying to walk

chalcidfly17:09:38

I was trying to do it with (def a (read-string (clojure.repl/source-fn ‘max)))

eggsyntax20:09:38

Some months ago, I think that @alexmiller gave me a super-useful pointer on how to list all clojure data structures which implement some interface (eg clojure.lang.Associative). I thought I made a note of it at the time, but damned if I can find it, and of course it's long since fallen off the cliff at the end of Clojurians' history. Anyone happen to know offhand how you could do that?

eggsyntax22:09:26

Dang, @lgouger, nice find! Turns out it's exactly backward from what I remembered -- not a way to show all data structures which implement some interface, but a way to show all interfaces implemented by a particular data structure (namely (supers (class entity))). So doesn't solve my immediate problem, but definitely goes into my notes this time.