Fork me on GitHub
#beginners
<
2017-09-01
>
Lucas Barbosa01:09:00

Is there anybody here that is familiar with the buddy auth library? I am trying to create access rules for some endpoints in my restful api, but I am having some troubles with regex matching

bschrag01:09:12

@noisesmith Finally got Netica running with lein by copying .dll and .lib files to C:\Windows\System32. For some reason, the direct java call we were using also stopped working, started giving the same error as the lein approach. (I did reboot with a BIOS update, but...) Clojure kept reporting No <file>.dll in java.library.path, even when using the -XshowSettings:properties option clearly showed java.library.path was set to <file>.dll's directory. Seems like it shouldn't be this hard. 🙂 Anyway, I'm now happily bumbling through translating Netica's Java demo into Clojure. Question (for anyone): How should I translate an expression like tuberculosis.setCPTable("visit", 0.05, 0.95), where tuberculosis is a successfully created instance of type norsys.netica.Node? I have tried...

edit-server.core> (.setCPTable tuberculosis '("visit",    0.05, 0.95))
ClassCastException clojure.lang.PersistentList cannot be cast to [F  edit-server.core/eval10884 (form-init256107752130466048.clj:53)
I gather .setCPTable is a method on Node, and it seems like that part is working. It looks like the error is about the list. Tried changing that to vector, got vector version of same error. Thanks for any insight.

chris_johnson01:09:59

I’m 99% sure that the right form for that is simpler: (.setCPTable tuberculosis "visit" 0.05 0.95)

juanjo.lenero02:09:27

Does anyone know how to get clojure.java.jdbc to accept a string in the parameter value and cast it to postgres timestamp with time zone?

juanjo.lenero02:09:15

So far I have this:

juanjo.lenero02:09:28

clojure
(extend-type java.lang.String
  jdbc/ISQLParameter
  (set-parameter [v ^java.sql.PreparedStatement stmt ^long idx]
    (let [conn      (.getConnection stmt)
          meta      (.getParameterMetaData stmt)
          type-name (.getParameterTypeName meta idx)]
      (prn type-name)
      (prn stmt))
    (.setObject stmt idx v)))

juanjo.lenero02:09:30

whenever the string parameter is meant for a timestamptz column the type-name variable will contain timestamptz.

juanjo.lenero02:09:14

So I would need to do something like (if (= type-name "timestamptz") (.setObject stmt idx (str v "::timestamp with time zone")) (else…)

juanjo.lenero02:09:23

But that doesn’t work

juanjo.lenero02:09:58

What do I replace (.setObject stmt idx v) with so v is cast to timestamptz

juanjo.lenero02:09:28

For the time being I resorted to jusing datetimes from clj-time.

chris_johnson03:09:42

well if you’re already using clj-time don’t forget to require clj-time.jdbc

chris_johnson03:09:19

it wraps some of the protocols for JDBC timestamp interop

bschrag03:09:47

@chris_johnson Looks like I was wrong about the method definition being ok:

edit-server.core> (let [tuberculosis (norsys.netica.Node. "VisitAsia" "visit,no_visit" (norsys.netica.Net.))]
                    (.setCPTable tuberculosis "visit" 0.05 0.95))
IllegalArgumentException No matching method found: setCPTable for class norsys.netica.Node  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)
I see byte code for setCPTable in Node.class, in NeticaJ.jar (and the Java demo I am trying to translate works). Am I missing something in the translation?

deg06:09:56

@eggsyntax @chris_johnson - Thanks again. As predicted, I slept, went back to debugging, and found the answer in two minutes. And, of coures, it was not where I had guessed. The real problem was that I had somehow put a dependency on re-frame/trace into all my builds. Duh! I'm still fighting other issues now, but they look straightforward. Gotta love the power of sleep, at least in hindsight!

eggsyntax11:09:24

Been there for for sure 😜

grierson09:09:52

Hey, are there any data validation lib with just predicates? Example (v/email?) So that I can just use the predicates with Clojure spec when defining my types.

eggsyntax12:09:06

Not that I’ve seen, but in my experience it’s not too bad to put them together on the fly. Although email, of course, can be a bit tricky 😜 I bet someone’s done a thorough job of that one — I’d try searching for (s/def email on one of the code search websites.

eggsyntax12:09:42

Or if you don’t find anything that way, just (defn email?

eggsyntax12:09:31

^ The latter at least claims to follow RFC-2822 😉

slipset10:09:35

So, given a function like

(defn maybe-change [p val] (if p (assoc val :foo :bar) val))

slipset10:09:58

is there a neater way to write this in Clojure?

emilaasa11:09:57

Seems pretty neat to me! (But I'm a noob)

danm11:09:26

Well there are the conditional thread macros

danm11:09:21

(cond-> val
  p (assoc :foo :bar))

danm11:09:38

MIght need a (some? p) or similar as the condition, not sure

danm11:09:55

But then you can add a load like that

sundarj11:09:23

if it's just checking for nil you can use some->

danm11:09:43

(cond-> val
  p (assoc :foo :bar)
  q (assoc :baz :gzork))

danm11:09:47

True, also that

danm11:09:06

I was assuming some boolean evaluation not just nil/not nil

slipset11:09:48

ah, so my example was unclear.

sundarj11:09:56

oh p is a predicate

sundarj11:09:08

you haven't called it in that function you posted

sundarj11:09:30

but yeah i think the other guys have it covered

kevinbheda12:09:14

how to do with-redef for java instance methods ?

didibus05:09:51

You can use (proxy) to mock Java instances.

didibus05:09:41

You proxy the instance where the methods you call are overridden with a mock implementation, then you pass in the proxied instance instead

didibus05:09:06

For interfaces, you can also use reify

danm12:09:35

I don't believe you can

danm12:09:55

We always wrote very thin wrapper-shims around the Java, and then redef'd those

danm12:09:09

Or reworked the code so we didn't have to redef to test

sb14:09:58

How could I define global variable from this let loop (like atom?)

update (fn [comp]
                (let [{:keys [smth]} (reagent/props comp)]                    
                      (reset! gchart smth)
                     ))

sb14:09:18

the gchart variable nil outside the loop

sb14:09:34

any idea how to define a var what i can use outside the loop?

danm14:09:04

I assume you're defining the atom gchart somewhere further up in the real code?

sb14:09:26

I created, smth not nil, coming the data.

sb14:09:50

When I reset! the atom.. nothing happens.. outside.. inside the loop I can read the value.

sb14:09:16

I defined the atom outside.

sb14:09:34

(def gchart (atom nil))

danm14:09:03

Hmm. I would have expected that to work

danm14:09:14

Although you keep saying loop, and I don't see a loop there?

sb14:09:31

re-frame reagent chart with subscription to a counter which come from a wrapper

sb14:09:10

‘(prn “outer” @gchart)’ shows nil

danm14:09:20

Frontend control flow always confuses me... The first time that prn is called, update will not have been executed, so I would expect it to be nil, yesno? But you're saying even after the update, when the function should have run, it still displays nil? Are you sure the function is actually being called?

danm14:09:39

Also why are you defining the gchart atom at the top in a def and then within the let as well?

sb14:09:38

Yes, I tested therefore I have two definition. I use one, of course.

danm14:09:42

Actually, surely that prn only runs during first initialisation, so it would always be nil. On update the update function is called, but the inside of the let with the create-class etc is not re-called is it?

sb14:09:41

Ok, sorry!

danm14:09:22

No worries, I could have no answered 😉

sb14:09:03

I know, that is a little bit difficult in first view therefore I shared in first time just a little part from this code.

sundarj14:09:35

@sb shadowing the top-level atom with the let is probably the cause of the problem

sundarj14:09:49

and it would never be updated by line 15, because the component hasn't been mounted at that point (and thus update hasn't been called)

sb14:09:35

thanks the infos!!!

sundarj14:09:55

hope that helps 😛

sb15:09:02

Yes, that was the problem..

danm15:09:50

Well, nice to know I was right but explaining myself badly 🙂

danm15:09:04

Ta @sundarj for doing a better job 🙂

sb15:09:46

No-no, both was useful.. just when @sundarj said.. I try to use the atom in the wrong place.. I got the “aha” moment.. true. 🙂

juanjo.lenero23:09:11

I usually write my functions in order [most-general -> most-specific]

juanjo.lenero23:09:24

The dependencies usually also run in that order

juanjo.lenero23:09:57

e.g. [parse-response-item parse-item-price….]

juanjo.lenero23:09:18

I come from javascript where we have function hoisting

juanjo.lenero23:09:26

In clojure this causes an exception.

juanjo.lenero23:09:35

Should I use declare all around

juanjo.lenero23:09:46

Or should I bite the bullet and reverse my ordering?

didibus02:09:34

Reverse ordering. Don't need to write specific first, but get in the habit of reading files bottom up and writing them bottom up.

johnj23:09:44

What's the reason cljs doesn't improves js types? ex: (+ "5" 5)