Fork me on GitHub

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]
    (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 [ :as foo]))


I’m guessing keyword aliases have to be resolved at the time the reader conditional is read


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)


FWIW, the same is true with regex literals.


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.


ah yeah sure


We encountered this when porting test.check to self-hosted ClojureScript and solved it by not using literals in a few places:


if this is an issue we can work around it in t.r btw


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.


Why don’t disj and dissoc share the same name?


Perhaps a corollary question: Why is disj not polymorphic like conj?


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...

👍 4

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: It takes some work, but SO provides good exposure for worthy topics.

Alex Miller (Clojure team)13:04:38

@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).

Alex Miller (Clojure team)13:04:19

a good place for short entries like this is and I’m happy to consider PRs adding q’s there

Alex Miller (Clojure team)13:04:56

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


but on the other hand assoc doesn’t work on it either, just a blurb of my mind

Alex Miller (Clojure team)14:04:47

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. ( params), but if I try to (require '[]), I get "could not locate com/my__init.class"?


require is for clojure namespaces, use import for java classes


It works. Thnks!


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}


(using the existing persistent collections)


I'm not sure how do I make this work though 🙂


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


hm, cool 🙂


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


but the core.cache model is just kind of odd

☝️ 4

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


so really simple


"ring buffer" is something I need to remember for next time though, I found some right now on github to study code 🙂


you may just want to use a core.async channel with a sliding or dropping buffer


heh, that's an interesting way to do it, but I'm not sure I want to bring core.async into that codebase yet

asolovyov19:04:55 - this seems like a good enough solution


Hey, are we aware that hash isn't consistent for regular expressions? e.g.

dev=> (hash #"foo")
dev=> (hash #"foo")


probably hashing the instance of the regex object


@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


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).


I’m building a graph with a few different types of connections.


I iterate over lists of connections and apply add them to the same graph


what I’d like to do is:


(second-> (reduce f graph coll-a)
  (reduce g graph coll-b)
  (reduce h graph coll-c)
  (reduce i graph coll-d))


well without saying graph!!!! so:


(second-> (reduce f graph coll-a)
  (reduce g  coll-b)
  (reduce h  coll-c)
  (reduce i  coll-d))


sorry for the all edits - I’m obviously a bit tired!


ah but you can do that with as->

👍 4

(as-> graph %
      (reduce f % coll-a)
      (reduce g % coll-b)
      (reduce h % coll-c)
      (reduce i % coll-d))


lovely! thanks again @joelsanchez