This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-27
Channels
- # adventofcode (3)
- # aws (1)
- # beginners (79)
- # boot-dev (1)
- # clara (50)
- # cljs-dev (7)
- # clojure (60)
- # clojure-austin (1)
- # clojure-germany (1)
- # clojure-greece (1)
- # clojure-russia (3)
- # clojure-spec (43)
- # clojure-uk (1)
- # clojurescript (76)
- # data-science (1)
- # datomic (26)
- # docs (2)
- # emacs (7)
- # fulcro (10)
- # hoplon (1)
- # instaparse (1)
- # lumo (3)
- # off-topic (16)
- # om (3)
- # onyx (2)
- # re-frame (3)
- # reagent (24)
- # ring-swagger (10)
- # shadow-cljs (15)
- # spacemacs (1)
- # sql (16)
- # uncomplicate (12)
- # unrepl (47)
also you can recur to a function - loop is just a convenience if the loop bindings shouldn't be your function args
The key thing is that recur
may only appear in a tail position -- and therefore it is always optimized. The compiler won't allow you to recur
in a position that couldn't be optimized.
Hi, just wondering, how do you guys use spec for data validation?
Using :pre
in defn
helps in a way to ensure that the function can't be called with unsuitable arguments. But the resulting AssertionError is not really helpful for composing error message to the user. I mean, I don't want to return "Assert failed: (spec/valid? ::email email)" as the error message to the user. Something like: "Invalid format for email" is good enough for me as of now.
I came across this https://groups.google.com/forum/#!topic/clojure/H9tk04sSTWE It gave me some clarity, but any thoughts here will still be appreciated 🙂
the spec guide https://clojure.org/guides/spec#_testing has a snippet on how to test all spec'ed functions from an ns (-> (stest/enumerate-namespace 'user) stest/check)
. Where should I add this in a classic clojure.test file? Adding it just like that produces 0 tests. Wrapping it in a deftest produces 1 tests with 0 assertions..
(counted? (float-array [1 2 3]))
(comment
false)
what is the constant time way to get the size of a float-array ?it dousn't implement clojure.lang.Counted because it's not a clojure type
it still supports count in constant time
user=> (source counted?)
(defn counted?
"Returns true if coll implements count in constant time"
{:added "1.0"
:static true}
[coll] (instance? clojure.lang.Counted coll))
nil
@seancorfield does the compiler similarly optimize non-`recur` recursion when it's in the tail position ?
no, this feature is intentionally missing
explicit is better
@noisesmith actually, shouldn't recur
be called iterate
(I know iterate
has been used) ? it's not recursion when it's in the tail-position
it's still recursion, a tail recursion is trivially translated into a goto
but that's an implementation detail - aside from stack usage the behavior is the same as direct recursion
👋 I recently converted a clojurescript project to use mostly cljc (to possibly move towards clojure), but now i'm getting a ⛵ load of errors when I boot my cljs project:
java.lang.RuntimeException: Unable to resolve symbol: defproject in this context, compiling:(project.clj:1:1)
usually starting with that ⬅️ . Strangely enough everything still works. Sound familiar with anyone?
is your source path for cljs set up so it would find project.clj?
I'm currently using the lein checkouts feature ... and I also have the third party lib cljc thing as a dependency...
ie. for my shared cljc lib I have run "lein install", and am now referencing that as a dependency as well as using the lein checkouts sym link features to make use of it. Everything appears to work, but I am seeing these errors on boot.
the errors look like so:
Reloading Clojure file "/src/myproject/myfile.cljc" failed.
clojure.lang.Compiler$CompilerException: java.lang.IllegalAccessError: MY-FUNCTION does not exist, compiling:(src/myproject/myfile.cljc:1:1)
Looking for a working proxy middleware for ring. Have tried tailrecursion/ring-proxy but running into errors. Any recommendations?
More specifically looking for a proxy configuration that can work with figwheel: https://github.com/bhauman/lein-figwheel/issues/634
I don’t have any help for you but just want to say thank you for working on this. This feature would be very helpful.
Hi all, can you tell me why tree-seq
is flattening my tree? My code is like this:
(defn to-tree [root]
(let [name (:name root)
reg @registry]
(tree-seq
#(some? (:childs (get reg %)))
#(:childs (get reg %))
name)))
registry
is a giant atom map with {"xxx" {:name "aaa" :childs ["yyy" "zzz"]}}
> Returns a lazy sequence of the nodes in a tree, via a depth-first walk.
that seems to be what it promises? perhaps you want something in clojure.walk
?
yeah, you want clojure.walk/post-walk for that likely
tree-seq is useful if you have no assumptions about the shape of the data, but you know what the thing you want looks like (so you can filter and find it)
post-walk / pre-walk / walk are for doing a transformation in the shape of the original collection
Justin and I have talked about it a couple times but haven’t buckled down to do it. It’s on my list for January
@alexmiller ^ ? Last year it was opened in early-ish December (12th) and the results came out in early February (7th).
Folks, what is the better/more idiomatic/shorter way to sort+print a vector of elements than
(doall (map #(-> %
format-value
println)
(sort-by some-fn some-vector)))
?also consider (doseq [value (sort-by some-fn some-vector)] (println (format-value value)))
also, if using map instead of run! (eg. for clojure version reasons or to take extra args like map does) use dorun instead of doall, since the value returned is not something you want or need (just a sequence of nils)
(run! #(println (format-value %)) (sort-by some-fn some-vector))
looks better, thanks.
for fancy fp-gangsta points you can replace that function with the equivalent (comp println format-value)
run! is much better than mapv here
(defn get-data []
( (str (mnist-config :data-dir) "foo"))
(doseq [[k url] (select-keys mnist-config [:train-images :train-labels :t10k-images :t10k-labels])]
(with-open [in (java.util.zip.GZIPInputStream. ( url))]
(spit (str (mnist-config :data-dir) (name k)) (slurp in)))))
(get-data)
is there a better way to write this? in particular, the (spit ... (slurp ...)) seems redundant
We're downloading a bunch of *.gz files, gunziping thej,m, and writing it out to file
perhaps?
Oh wait, you're slurping an input stream... not sure if copy will do that...
you can write an inputstream directly though
that is, you can write it to an outputstream, and create the output stream in the with-open call as a second binding
something vaguely like (with-open [in …. out (io/output-stream (io/file (str …)))] (.write out in))
but probably also requiring a loop or a byte count on the write
it’s just a needless creation of a string, not a huge deal
also just to be clear making a string out of a 60 meg file that’s encoded with utf-8 is likely 120megs in memory because java strings are utf-16
and definitely don’t use slurp/spit for anything that isn’t text
(I don’t know what those “images” in the name refer to just be aware that if they are image data as in bmp or jpeg slurp/spit will turn them into garbage)
is anyone else able to watch https://www.youtube.com/watch?v=5VAEXYYdfWY (“Zippers - Episode 2”)? it’s not working for me.
oh, i get a better error message embedded here in slack, sorry! (“This video requires payment to watch”)
is there an easy way to list the requires for a given namespace? I was looking for something like ns-refers but for requires
I have a lot of .cljc code I'd like to make into a library. Is there a way to make cross platform libraries without rewriting my code twice?