This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-05-19
Channels
- # announcements (12)
- # aws (17)
- # babashka (6)
- # beginners (40)
- # cider (14)
- # cljs-dev (14)
- # cljsrn (8)
- # clojure (110)
- # clojure-europe (46)
- # clojure-italy (1)
- # clojure-nl (4)
- # clojure-spec (14)
- # clojure-sweden (3)
- # clojure-uk (29)
- # clojurescript (52)
- # conjure (68)
- # cursive (33)
- # datomic (9)
- # figwheel-main (11)
- # fulcro (97)
- # ghostwheel (1)
- # graalvm (2)
- # helix (53)
- # hoplon (13)
- # joker (6)
- # kaocha (1)
- # leiningen (2)
- # meander (28)
- # mid-cities-meetup (1)
- # observability (1)
- # off-topic (112)
- # pathom (6)
- # pedestal (3)
- # re-frame (16)
- # reagent (16)
- # reitit (2)
- # shadow-cljs (27)
- # spacemacs (2)
- # sql (26)
- # testing (3)
- # utah-clojurians (3)
- # vim (2)
- # xtdb (32)
I'm trying to build something with custom filters that can be toggled through buttons on a UI. For example
(defn red? [item] (= "red" (:color item)))
(defn large? [item] (= "l" (:size item)))
(defn small? [item] (= "s" (:s item)))
(def selected-options #{red? small?})
And then use that to filter through a vector of maps.
I tried using transducers (which seemed like the proper use case for them) but I can't seem to break the set into a series of functions?
(def filter-red (filter red?))
(def filter-large (filter large?))
(def filter-small (filter small?))
(def enabled-filters #{filter-red filter-smal})
When using
(sequence (comp enabled-filters) items)
I get an error, however when I do
(sequence (comp filter-red filter-small) items)
it works. Not sure how to get the items out of my enabled-filters
set and processed by the comp function. (this was all in the REPL)I think you want (apply comp enabled-filters)
@holmes89 depending on what your bigger task looks like, you could also compose the functions as an arg to one filter, instead of composing the filters
so instead of (comp (filter f) (filter g) (filter h))
you'd have (filter (every-pred f g h))
would that be the same thing with the set of functions? (filter (apply every-pred enabled-filters))
? Also is one a better use case than the other? performance?
performance-wise I'd expect one filter to be slightly better than n filters, and every-pred works with apply as expected
I'd choose one or the other based on the larger context - if there are also usages of map / partition etc. it makes sense to compose transducers, if all you need is filtering I think composing a function for a filter is simpler
okay, thanks for answering my questions! My context is just a stupid little app to teach myself. Transducers seemed interesting and I wanted to use it. But this is why I like doing these projects, I now know two different ways based on use cases. Thanks for all the help!
I would recommend you do whatever you find the most readable. The performance difference between these approaches will not be noticeable in almost all cases.
Hello guys, I am a newbie and have basically only understood the syntax and want to start learning more. I wanted to find anyone who's interested in learning this together as a study buddy. This is basically someone who will share resources with each other and meet daily or as much as possible to keep track of the other if they are learning properly. Basically a meetup like partner but in virtual group. If anyone is interested please reply to me in this thread or ping me.
I am in 🙂, but i am absolutely new to this
No problem we are all beginner. If you have finished the get started tutorial from the clojure website it's all fine.
can you share me the website, i will work it out that first then is this the one ? https://clojure.org/guides/getting_started
Yeah that's the one I did and then I am following http://clojure-doc.org/ right now
Good morning, everyone. Could anyone help with this? I have issues importing reagent to my setup. I posted everything here: https://ask.clojure.org/index.php/9311/reagent-undefined-in-clj-cljs-setup
I’m on my phone but I see you have Works! not in a string but as a var. put that in a string maybe?
It’s weird that there are no quotation marks but execution isn’t even getting to that point and I also tried deleting the call to reagent/render
I still get the undefined
for reagent.
But as you can see, I gave up on trying to use both in the same repo. I have two repos now and it works 🙂
i wanted to spot the difference. i don't think there's anything special about them in two repos vs one so i wanted to see what was going on
There is probably no difference but at my level of experience with clojure it’s easier to keep them apart. At one point, I had a setup where I was able to use reagent but it was beyond my abilities to start my server. This way I can use an approach that I’m used to. I start the server with one config and the client with another. I tell the client where to send its requests and now I can focus on the actual issue I was going to solve.
> However, when the app.js
is processed, I get the following console ouput:
What does "processed" mean here?
I think, I might abandon the idea to host clojure and clojurescript in one codebase. Not only are there no resources on this, I constantly run into issues, even if I try different configurations. This has kept me busy for far too long and the added value doesn’t justify that.
Have you looked at the luminus reagent cljs project template? It might be worth generating the template and poking around to see how it works, if you haven't already.
How do I turn this list
[[1 2 3] [4 5 6] [7 8 9]]
into
[[1 4 7] [2 5 8] [3 6 9]]
?
The naive way would be to map first on all the vectors, then second, then third etc. Is there a better way?
Assumptions: all vectors are the same length.In case you don't want anyone to spoil the solution for you, I'll give you a hint - map can get several sequences
(apply mapv vector %)
seems to work.
Thanks 🙂
(partition (count v) (apply interleave v))
also works
it's not just lazy vs. eager - map has less work to do, it's a simpler set of steps to do the same result
That, too, but I think in comparison with (apply map vector %)
it'll still win (need to doall
, ofc)
Wondering if mapping list
will be faster than vector
with lists you have O(n) object allocations for storage, vectors have arrays inside to batch allocations for multiple indexes into one new
granular calls to new are more expensive than bulk allocations like vectors do (in general, best to measure of course)
@endrebak85
(apply mapv vector x)
where x is your list
apply splits out your list so it's like [1 2 3] [4 5 6] [7 8 9]
mapv is like map but puts the result in a vector (use map if you want the output to be a list)
if map is given multiple lists, it takes one from each at a time, so it takes [1 4 7], then [2 5 8]
vector just turns them into vectors