This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-16
Channels
- # announcements (1)
- # beginners (83)
- # boot (10)
- # cider (23)
- # cljdoc (24)
- # cljs-dev (3)
- # clojure (138)
- # clojure-finland (1)
- # clojure-italy (12)
- # clojure-losangeles (2)
- # clojure-mexico (1)
- # clojure-nl (18)
- # clojure-russia (23)
- # clojure-sg (1)
- # clojure-spec (15)
- # clojure-uk (126)
- # clojurescript (94)
- # cloverage (2)
- # core-async (1)
- # cursive (98)
- # datomic (54)
- # figwheel-main (19)
- # hyperfiddle (21)
- # jobs (1)
- # jobs-rus (4)
- # leiningen (4)
- # liberator (4)
- # off-topic (15)
- # parinfer (9)
- # re-frame (23)
- # reagent (19)
- # reitit (2)
- # ring-swagger (3)
- # rum (3)
- # shadow-cljs (244)
- # sql (4)
Is the library clojure.tools.trace
unmaintained and deprecated? I need an library like this. What suggestion about trace library? Thanks .
it’s unmaintained but still works just fine afaik
I just tried the examples from the README and they worked:
$ 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)
Do you know any other similar libraries can trace too?
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
I have a string I would like to evaluate. should i prefer (load-string my-string)
or (resolve (symbol my-string))
?
read-string
and eval
? Depends what constraints you have on the string
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
my sense is that load-string
is too powerful
@alexmiller Hmm, my bad, I should Google this too. I though experienced Clojurians might know this.
I tend to mostly work in small pieces evaling into a repl, or if really needed, with a debugger
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?
ThanksOne 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)))))))
@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')))))
next-paths
would return either a list of continuations, or if there aren't any, a single element list containing the input
you need a much different approach if it's cyclic or undirected
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.
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
Can you capture/print the value of @url
and make sure the string matches what you expect?
It prints out the db-spec
with {:subname <the url> :subprotocol nil :user "myuser" :password "hunter2"}
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)
db-spec {:subname <the url> :subprotocol nil :user "myuser" :password "hunter2"} is missing a required parameter
raw string should match the following format, according to the docs:
java.lang.String:
An optional prefix of jdbc: is allowed.
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?
@olekss.janis you can format the code by placing it between lines with “```”
The problem may be lazyness; you probably want to wrap each (for ..)
sexp with (doall (for ..))
Hmmm can you explain this lazines, why the first one is not evaluated? Or... what am I missing?
And is there might be better cycle construct which lets me print vector of stuff to the screen?
you can replace for
with doseq
- it uses the same syntax, isn't lazy
run! also works if your case is really that simple though
writing simple console based game as my first meaningful Clojure program, haven't got much far and learned a ton already
#(run! print-factory %)
does the same thing
usually in clojure using recur on the rest of a collection means your code could be much simpler
(because something else already does that)
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?
If you want it as a string again, you can do (apply str (reverse "hello"))
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
Thanks!
Is there a way I can have Cheshire decode values with certain symbols in them differently?
there's a global config iirc
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"
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
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}}
that's going to involve interop with the jackson java library, and a global config
cheshire uses jackson, you'd use interop to replicate something like this https://www.baeldung.com/jackson-deserialization
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)
I've even used transit to encode edn to json, then put that json body directly into mongo