This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-04-27
Channels
- # architecture (6)
- # beginners (36)
- # boot (4)
- # cider (74)
- # cljsrn (5)
- # clojure (87)
- # clojure-denver (2)
- # clojure-finland (2)
- # clojure-gamedev (5)
- # clojure-italy (10)
- # clojure-nl (1)
- # clojure-uk (45)
- # clojurescript (33)
- # code-reviews (5)
- # core-async (12)
- # cursive (17)
- # datascript (2)
- # datomic (71)
- # duct (4)
- # emacs (19)
- # figwheel (1)
- # fulcro (4)
- # garden (1)
- # hoplon (18)
- # jobs (5)
- # leiningen (2)
- # off-topic (73)
- # onyx (14)
- # overtone (2)
- # portkey (32)
- # re-frame (62)
- # reagent (46)
- # shadow-cljs (76)
- # spacemacs (2)
- # sql (14)
- # tools-deps (5)
- # yada (3)
Is there a common pattern for composing transducers at runtime, where some may not be needed?
I'd like to filter on several different things, but some of the filters may not be active
I could put them all in a sequence, remove the nils, and apply
to comp
but wondered if there was a cleaner pattern for this
That seems like a reasonable approach. I wonder if this alternative reads and performs well, in the special case that the transducers are statically ordered, with some disabled at runtime:
(let [x? true
y? false]
(comp
(if x? (filter pos?) identity)
(if y? (filter neg?) identity)))
I suspect just (apply comp active-xforms)
reads better and is more flexible.I have a WebGL / CLJS app that is very laggy. What tool is recommended for profiling CLJS code ?
@qqq wrong channel? But to answer: There is nothing CLJS specific, all JS profiling tools apply here. I just use the Chrome dev tools
Does lein test run tests in order? I see test 1 setting something and then testing it in test 2. I know I should be using fixtures but just curious
@xtreak29 I dont know about lein exactly but tests in general should be independent of each other. In a best case they should even be able to run parallel. So you should never depend in one test on another.
Yes, I just ran with some print statements and you are correct. I need to refactor to use fixtures.
I was testing setting 'foo' to 1 and then doing listing the values in the other test and they seem to run out of order causing failure. Thanks
Is there a bug with reader conditionals and namespaced keyword aliases? It looks like the aliases have to resolve at read time (and therefore can’t be required). Actually thinking about it this might not be a bug but be an implementation detail / limitation…
e.g. #?(:cljs ::foo/bar)
doesn’t work in a :clj
environment… even if above it there is a (ns blah.blah #?(:cljs (:require [foo.bar :as foo]))
I’m guessing keyword aliases have to be resolved at the time the reader conditional is read
@rickmoynihan yes, that is all correct
cool. Makes sense, just surprised me at first
I do frequently find myself wishing for a decomplected way to alias keywords without loading a namespace… though I know this isn’t a new conversation 🙂
(Not that that would solve the limitation above)
If you write
#?(:clj #"some-re-1" :cljs #"some-re-2")
The :clj
branch has to be a valid literal in ClojureScript (for self-hosted), and I believe the converse is true where the :cljs
branch has to have a valid literal in Clojure.We encountered this when porting test.check
to self-hosted ClojureScript and solved it by not using literals in a few places:
https://github.com/clojure/test.check/commit/ebcc57dd631e25ee39bc059899e3bbd9ddf24ee9#diff-8e9504630b7d3914b74409188fc969e8L78
I've only encountered it once (in the above). It might be nice to address from a "completeness / correctness" standpoint, if that aligns with the language semantics, etc. But, as far as I can tell, it is not a pressing issue, per se.
@borkdude Interesting thoughts here https://stackoverflow.com/questions/28844647/why-are-disj-and-dissoc-distinct-functions-in-clojure
hmyeah. btw, I met the author last weekend again, it’s a small small clojure world 😛
As a "meta" point sometimes I worry that our discussions in here are trapped in a world that is not as visible as something like SO.
I’d love if more questions like this end up in Clojureverse. Esp. with timezone differences it’s hard to stay on top of slack...
Agreed. Also, if you find a good Q&A exchange on this Slack, you can [ask those involved for permission and] summarize the exchange on SO, like I did here: https://stackoverflow.com/q/47358416/202292 It takes some work, but SO provides good exposure for worthy topics.
@borkdude @mfikes I don’t know anything special but disj
== remove element, dissoc
== remove association and these are the opposite of conj
and assoc
. conj
means add element and that has natural “fast” impls for all Clojure collections. But disj
does not have “fast” implementations on sequential collections like lists or vectors. It could theoretically be implemented on maps (disj {:a 1} [:a 1])
but it seems unlikely that you would ever prefer to do that over (dissoc {:a 1} :a)
. So it’s only on sets. Maps are the only associative collection that allows removal (vectors can’t be removed in the middle).
a good place for short entries like this is https://clojure.org/guides/faq and I’m happy to consider PRs adding q’s there
most of the entries on that page are questions that came up either here or on the reddit “new user” topics that I answered on the faq page and linked to
Well, dissoc
is remove association, that’s why I expected it to work on sets
since they are keyed by their elements
sets are not conceptually associations (even though they are implemented that way)
Why is it that I can use my Java class directly i.e. (com.my.Class. params), but if I try to (require '[http://com.my]), I get "could not locate com/my__init.class"?
Hey all! What would be the most efficient way to grow an ordered sequence (any - list, vector, ...) while simultaneously limiting its length? I'm okay with adding values in the front or in the back of said sequence... cons + take seems a bit wasteful for me, is there anything better I could do?
okay, I seem to overestimate wastefulness 🙂 cons/take executes 1m times in 200 msec, I guess I could live with that 😄
the simplest way to keep a ring buffer is something like {:items [] :read -1 :write 0}
insert is something like (fn [{:keys [items write} :as buffer} value] (assoc buffer :items (assoc items write value) :write (mod (inc write) 10)))
you an use assoc to change items in a vector and an assoc at the index of size+1 becomes a conj on to the end
it is also pretty easy to write a custom persistent (tree and path copying) ring buffer, because you can just preallocate a full balanced binary tree and never have to worry about balancing it
if it's small enough just use an array and clone it
although I'm not sure I've ever needed a immutable ring buffer, so perhaps just mutating an array is good enough?
depends on what the use-case is here
like, if you were implementing a core.cache cache, which the model is the cache is a value and you wrap it in an atom, and whatever cache algorithm you were implementing was some complicated thing based on layers of ring buffers, you would want a ring buffer as a value
well, I get some updates coming in once a second and I need to store them, but I want to limit them by 100 or 1000 items
"ring buffer" is something I need to remember for next time though, I found some right now on github to study code 🙂
heh, that's an interesting way to do it, but I'm not sure I want to bring core.async into that codebase yet
https://github.com/amalloy/ring-buffer - this seems like a good enough solution
Hey, are we aware that hash
isn't consistent for regular expressions? e.g.
dev=> (hash #"foo")
738015301
dev=> (hash #"foo")
793194086
@eraserhd regex's in clojure are instances of java.util.regex.Pattern
, and hash
just uses the java hashCode for the java instance. So yes (not= #"foo" #"foo")
, and also the hashes don't match, since java regex objects don't support equality based off the values of the regex
Yesyes, but we have hash
specifically to make instances of Long, Integer, Short, and clojure persistent collections equal... So, do we know that regexp literals aren't equal - is that an oversight or intentional?
(It came up for me today, in expecting that two code forms were equal and had the same hash code.)
@eraserhd looks like there was some discussion on this a few years ago https://dev.clojure.org/jira/browse/CLJ-1182
i really wish clojure had a ->
that threaded to the second argument for when I chain a number of reductions together.
just made up, may have problems, still should get you what you want
lovely thankyou @joelsanchez!
(and please update the docstring.)
@triss I'm curious... what sort of code needs to thread the second argument? I can imagine threading the first argument (data) or last argument (collection).
(second-> (reduce f graph coll-a)
(reduce g graph coll-b)
(reduce h graph coll-c)
(reduce i graph coll-d))
(second-> (reduce f graph coll-a)
(reduce g coll-b)
(reduce h coll-c)
(reduce i coll-d))
(as-> graph %
(reduce f % coll-a)
(reduce g % coll-b)
(reduce h % coll-c)
(reduce i % coll-d))
lovely! thanks again @joelsanchez