Fork me on GitHub
#clojure
<
2018-05-13
>
ghaskins02:05:36

Hi All, I’m having a problem related to trying to load/require/compile clojure from clojure that I feel like probably is possible, yet I am doing something slightly wrong to get it working

ghaskins02:05:05

ive written a tool that takes an interface definition language, and compiles it to clojure

ghaskins02:05:12

the tool is written in clojure itself

ghaskins02:05:01

what I would like is to have a unit-test in the tool that runs the code generator, compiles the result, and then runs tests against some of the symbols in the compiled code

ghaskins02:05:35

ive tried all variants of load-file/load-string, requires :reload, and compile I can think of

ghaskins02:05:13

in some forms, I think I am pretty close, but I can never seem to subsequently reference what was compiled and I dont understand why

ghaskins02:05:21

any guidance appreciated

ghaskins02:05:16

in case it helps, here is a sample of one of the approaches I tried: https://gist.github.com/ghaskins/57245da53b66b0ede4b1f745a17015e6

ghaskins02:05:37

particularly confusing is it seems like everything is loading fine

ghaskins02:05:55

e.g.

(pprint (ns-publics 'com.example.tutorial.sample))
returns a sane list of symbols from the expected namespace

andy.fingerhut04:05:40

@ghaskins Are you getting an error like: "... java.lang.ClassNotFoundException: com.example.tutorial.sample ..." ?

ghaskins16:05:56

Yep! Or variants of that. And everything does work if I (ns-resolve) as you suggested. I can probably work with this if necessary, but I would like to try to get the symbols in a way that they can be used naturally if at all possible.

ghaskins02:05:45

@U0CMVHBL2 I figured out the magic….if I put the logic to generate/compile/require outside of the deftest form, it all works fine

ghaskins02:05:48

Thanks for the help, appreciated

andy.fingerhut03:05:56

You are welcome

andy.fingerhut03:05:15

Something to be cautious about -- when you use clojure.test / deftest, and run tests, the order that different deftests run relative to each other is not easily predictable. It is probably some kind of order determined by hashing their names or something equally difficult to predict.

andy.fingerhut03:05:06

I do not know what guarantees the implementation may have about running other top level forms before the contents of the test code inside of deftest's.

andy.fingerhut04:05:56

If so, I believe the reason why is because doing the require followed by trying to use the newly required namespace in a symbol inside of a larger top level form (here 'deftest'), the Clojure compiler is trying to read, then compile, that entire top level form, and the require isn't actually done until after reading the entire top level form is complete. Someone will correct me if I've messed up the precise reason there.

andy.fingerhut04:05:54

If you use (ns-resolve 'com.example.tutorial.sample 'pb->AddressBook) it should return the var, which is as if you had successfully written #'com.example.tutorial.sample/pb->AddressBook. You can use @ or deref on the (ns-resolve ...) expression to get the value of the var.

andy.fingerhut08:05:43

@benzap If you replace the let statement you tried with (let [x (var foo)] (meta x)) it will get the same metadata as (meta (var foo)).

dottedmag09:05:45

How do I write a regexp literal that matches from 3 to 5 *?

dottedmag09:05:00

I'm confused by the amount of escaping that is going on.

dottedmag09:05:49

Oh, it's just #"\*{3,5}", I was overdoing it.

triss09:05:31

ok, so I’ve got a code base with some hard coded values/thresholds etc. I want to make these things options. what’s everyone’s favourite way of handling config?

triss09:05:46

and passing it down to the functions that need it?

triss10:05:52

I’m not sure wether to make the map containing options the last argument or the first…

schmee10:05:58

I like to make it the first because then it’s easy to use partial to create pre-configed versions

👍 1
triss10:05:52

that’s what I was thinking… just means I really do have need to partial them before I can use them cleanly with update or swap! etc…

triss10:05:14

I’ll give this a go…

triss10:05:54

Ok another question… If I want to use namespaced keys in my map when destructuring is their an equivalent to :: in the :keys list I want to unpack?

triss10:05:15

so if i have a map {::kw1 1 ::kw2 2} how do I get them out using destructuring?

triss10:05:06

Can I not use the :keys [kw1 kw2] thing with out specifying the full namespace?

jumar10:05:36

You can do {::keys [kw1 kw2]}

jumar10:05:51

or {:my-ns/keys [kw1 kw2]}

triss10:05:10

lovely @jumar thanks!

sobel11:05:18

yw!

👍 1
hlolli12:05:35

Is there a way to get the :target-path value in clojure runtime?

hlolli12:05:22

I don't think I need that value https://github.com/technomancy/leiningen/commit/e04f1bee9bc7cdfb419b6b188a10e2076d6867eb I have dymanic shared lib files in native, and I need to ensure that the directory is copied from target when useing the library, as the lib files have relative RPATH's from the root directory.

sobel13:05:29

is there a clojure email (javamail) interface that doesn't require i write attachments to disk? i'd like to attach from streams

gon08:05:49

com.draines/postal

sobel13:05:37

or even strings

benzap14:05:41

hmm, I have more hoops to jump through to get the metadata it seems

benzap14:05:29

The docstring i'm trying to get is in a (fn []) which is a value within a map. If i'm not mistaken, this is not a var in any sense of the word. However, if I make a temporary var, is it possible to bind this (fn []) temporarily using set!, and retrieve the (fn []) metadata?

benzap15:05:36

From what I can tell the information is lost

benzap15:05:15

So the only way to really preserve the foo meta data is to store it off to the side explicitly, since there is no way to access the (fn []) metadata after it's use in the map

andy.fingerhut16:05:14

@benzap When defn occurs, it creates a metadata map and puts it on the var being defn'd. It doesn't put metadata on the function value itself.

benzap22:05:18

Thanks for the heads up

benzap22:05:19

Judging by what I want, i'm likely going to develop a separate collection to store metadata on key values. I suppose it's similar to how clojure.spec would store spec data

jfntn17:05:26

I’m writing a deftype for a transient that wraps a map, I’m using ^:unsynchronized-mutable ^ITransientMap m for the backing map, and mutating it with (set! m (.assoc m k v)) but I’m wondering if there are faster options?

val_waeselynck22:05:28

Faster for doing what? What read / write operations in what concurrency scenarios?

jfntn23:05:37

The map is backing a kv index, the goal is to speed up adding entries but lookup is also required

noisesmith02:05:06

why not use one of the built in hash-map types?

noisesmith02:05:42

(by which I mean, the ones provided by the jvm)

devn06:05:53

concurrent hashmap is a finely tuned machine

👍 1