Fork me on GitHub
#beginners
<
2018-08-16
>
stardiviner02:08:35

Is the library clojure.tools.trace unmaintained and deprecated? I need an library like this. What suggestion about trace library? Thanks .

Alex Miller (Clojure team)03:08:23

it’s unmaintained but still works just fine afaik

Alex Miller (Clojure team)03:08:37

I just tried the examples from the README and they worked:

Alex Miller (Clojure team)03:08:41

$ clj -Sdeps '{:deps {org.clojure/tools.trace {:mvn/version "0.7.9"}}}'
Clojure 1.9.0
user=> (use 'clojure.tools.trace)
nil
user=> (trace (* 2 3))
TRACE: 6
6
user=> (trace "tag" (* 2 3))
TRACE tag: 6
6
user=> (deftrace fubar [x v] (+ x v))
#'user/fubar
user=> (fubar 2 3)
TRACE t415: (fubar 2 3)
TRACE t415: => 5
5
user=> (do (+ 1 3) (* 5 6) (/ 1 0))
ArithmeticException Divide by zero  clojure.lang.Numbers.divide (Numbers.java:163)
user=> (trace-forms (+ 1 3) (* 5 6) (/ 1 0))
ArithmeticException Divide by zero
  Form failed: (/ 1 0)
  clojure.lang.Numbers.divide (Numbers.java:163)

stardiviner04:08:01

Do you know any other similar libraries can trace too?

Alex Miller (Clojure team)05:08:50

I googled “clojure trace” and the first stackoverflow hit seems to have a pretty good range of options on it https://stackoverflow.com/questions/41946753/how-can-i-trace-code-execution-in-clojure

Michael Stokley05:08:58

I have a string I would like to evaluate. should i prefer (load-string my-string) or (resolve (symbol my-string))?

Alex Miller (Clojure team)11:08:20

read-string and eval? Depends what constraints you have on the string

Michael Stokley04:08:35

thanks for the reply. some of the strings will resolve to functions i've defined, some to data. the top level goal is to take json, massage it so it looks more like lists and function calls, then evaluate the resulting expressions

Michael Stokley05:08:32

my sense is that load-string is too powerful

stardiviner05:08:41

@alexmiller Hmm, my bad, I should Google this too. I though experienced Clojurians might know this.

Alex Miller (Clojure team)11:08:52

I tend to mostly work in small pieces evaling into a repl, or if really needed, with a debugger

ben14:08:20

Hi everyone, I’m trying to make a recursive function to find all possible routes through a graph (encoded as a coll of ‘edges’). So far I’ve got :

(defn extend-path
  [path-so-far graph]
  (let [leading-edge   (last path-so-far)
        candidates     (next-edges leading-edge graph)
        extended-paths (map #(conj path-so-far %) candidates)]
    (if (empty? candidates)
      (vector path-so-far)
      (mapcat #(extend-path % graph) extended-paths))))
Or in pseudocode:
1. Find the possible next edges from path
2. For each edge:
    i.  Append edge to path
    ii. goto 1
which works for small graphs but will explode for bigger ones (no recur). All my efforts to make it work so far have come unstuck due to the fact that each loop requires another iteration over candidates, therefore putting recur out of the tail position. Does anyone have any pointers on where I should look to get around this? Is such a thing possible with nexted loops like this? Thanks

ben14:08:41

One such failed attempt:

(defn- extend-path
  [init-path graph]
  (loop [path init-path]
    (let [next (next-edges (last path) graph)]
      (if (empty? next)
        (vector path)
        (for [edge next]
          (recur (conj path edge)))))))

moxaj14:08:37

@torvaney lots of specifics missing (directed? contains cycles?), but here's a rough idea:

(defn extend-paths [initial-edge graph]
  (loop [paths [initial-edge]]
    (let [paths' (mapcat #(next-paths % graph) paths)]
      (if (= paths paths')
        paths
        (recur paths')))))

moxaj14:08:59

next-paths would return either a list of continuations, or if there aren't any, a single element list containing the input

noisesmith15:08:22

you need a much different approach if it's cyclic or undirected

ben15:08:09

Thanks @moxaj, apologies for the lack of information. The graph is directed, and each edge is forwards-only, so I think you’re solution will get me on the right track.

ben15:08:13

Thank you very much

bartuka15:08:59

hello ppl, I am trying to run the clojurescript quickstart tutorial

bartuka15:08:10

but when I run the `clj --main cljs.main --compile hello-world.core --repl’

bartuka15:08:16

it hangs and nothing happens

Mario C.16:08:51

When I try to get from postgres db I get an error thrown by clojure.java.jdbc/get-connection which states db-spec ... is missing a required parameter

Mario C.16:08:13

I am using (sql/get-by-id @url <table-name> <the rest of the stuff>)

Mario C.16:08:38

my url is a string containing the database name, user and password.

Mario C.16:08:16

It worked fine when my url was

dadair16:08:55

Is url an atom/etc? Why the @?

Mario C.16:08:07

It is an atom

dadair16:08:56

Can you capture/print the value of @url and make sure the string matches what you expect?

Mario C.16:08:25

Yup its what I expect

Mario C.16:08:43

It prints out the db-spec with {:subname <the url> :subprotocol nil :user "myuser" :password "hunter2"}

Mario C.16:08:33

Looking at the get-connection code it needs some :dbname :dbtype

Mario C.16:08:54

But I thought I am using this

Raw:
    :connection-uri (required) a String
                 Passed directly to DriverManager/getConnection
                 (both :user and :password may be specified as well, rather
                  than passing them as part of the connection string)

dadair16:08:01

Sorry, you said url was a string; is it a map then?

Mario C.16:08:15

No the error prints out the map, sorry for the confusion

Mario C.16:08:27

url is indeed a string

dadair16:08:05

can you show what the actual value of url is?

dadair16:08:14

subprotocol should be postgresql in that map I think

Mario C.16:08:14

db-spec {:subname <the url> :subprotocol nil :user "myuser" :password "hunter2"} is missing a required parameter

dadair16:08:22

if it’s nil then your url may not have that prefix

Mario C.16:08:32

I think thats what it is

dadair16:08:33

(since it’s failing to parse it)

Mario C.16:08:52

weird because coworker doesn't have that prefix and it works for him

dadair16:08:44

raw string should match the following format, according to the docs:

java.lang.String:
  
               An optional prefix of jdbc: is allowed.

JanisOlex16:08:24

hey I am confused on results here is what I do (defn p1 [] (for [x ["1" "2" "3"]] (println x))) (defn p2 [] (for [x ["a" "b" "c"]] (println x))) (do (println "l1") (p1) (println "l2") (p2)) Result is l1 l2 a b c => (nil nil nil) So p1 prints are lost.. why?

Mario C.16:08:05

@dadair Thanks! :thumbsup:

dadair16:08:41

@olekss.janis you can format the code by placing it between lines with “```”

dadair16:08:32

The problem may be lazyness; you probably want to wrap each (for ..) sexp with (doall (for ..))

JanisOlex16:08:14

Hmmm can you explain this lazines, why the first one is not evaluated? Or... what am I missing?

JanisOlex16:08:42

And is there might be better cycle construct which lets me print vector of stuff to the screen?

JanisOlex16:08:06

and yes, that was the problem

dadair16:08:25

Each item individually, on their own line, I assume?

dadair16:08:41

maybe (run! println coll)?

noisesmith16:08:01

you can replace for with doseq - it uses the same syntax, isn't lazy

noisesmith16:08:29

run! also works if your case is really that simple though

JanisOlex16:08:44

writing simple console based game as my first meaningful Clojure program, haven't got much far and learned a ton already

noisesmith16:08:45

#(run! print-factory %) does the same thing

noisesmith16:08:24

usually in clojure using recur on the rest of a collection means your code could be much simpler

noisesmith16:08:35

(because something else already does that)

Mario C.18:08:41

Is anyone familiar with VisualVM?

samuel lough19:08:46

Beginner questions on strings vs strings stuck in PersistentLists: I am trying to reverse a list which is easily accomplished by (reverse "hello") however, this returns a type PersistentList (\o \l \l \e \h) The problem is i want to be able to do this: (= "olleh" (reverse "hello")) What is going on here?

manutter5119:08:12

If you want it as a string again, you can do (apply str (reverse "hello"))

sundarj20:08:24

reverse takes a seqable (which a string is) and returns a seq (which a string is not)

=> (seqable? "hello")
true
=> (seq? "hello")
false
=> (seq? (reverse "hello"))
true
=> (= (seq "olleh") (reverse "hello"))
true

😀 4
sundarj20:08:52

seq is used to turn a seqable into a seq (which is like a list)

sundarj20:08:45

no problem!

Mario C.23:08:44

Is there a way I can have Cheshire decode values with certain symbols in them differently?

Mario C.23:08:54

"{\"b\":\":sample\"}"

noisesmith23:08:24

there's a global config iirc

Mario C.23:08:42

Have it look at that ^ and say "okay we have a colon prefixed. Lets make that into a keyword and not a string on decode"

noisesmith23:08:35

it's easier to use clojure.walk to update your values, but there is a config in jackson to add custom decoders - I know that it exists but not how to use it

noisesmith23:08:35

user=> (require '[clojure.walk :as walk])
nil
user=> (walk/postwalk #(if (and (string? %) (= (first %) \:)) (read-string %) %) {:a 0 :b "e" :c {:f ":d"}})
{:a 0, :b "e", :c {:f :d}}

Mario C.23:08:58

That would work but I am trying to see if i can do it on a decode

noisesmith23:08:18

that's going to involve interop with the jackson java library, and a global config

Mario C.23:08:31

If I am already traversing the string why go over it again

Mario C.23:08:56

cheshire doesn't provide anything with values only with keys

noisesmith23:08:44

cheshire uses jackson, you'd use interop to replicate something like this https://www.baeldung.com/jackson-deserialization

Mario C.23:08:41

Thanks :thumbsup:

noisesmith23:08:01

also depending on your needs, transit can embed in json, and can use first class local decoders (and of course supports basic clojure stuff like symbols, keywords, sets, etc. out of the box)

noisesmith23:08:42

I've even used transit to encode edn to json, then put that json body directly into mongo