Fork me on GitHub
#beginners
<
2020-05-19
>
Joel Holmes00:05:22

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)

codonnell00:05:57

I think you want (apply comp enabled-filters)

Joel Holmes00:05:26

ah thanks! that worked!

๐Ÿ‘ 1
noisesmith00:05:40

@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

noisesmith00:05:36

so instead of (comp (filter f) (filter g) (filter h)) you'd have (filter (every-pred f g h))

Joel Holmes00:05:20

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?

noisesmith00:05:55

performance-wise I'd expect one filter to be slightly better than n filters, and every-pred works with apply as expected

noisesmith00:05:43

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

Joel Holmes00:05:37

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!

codonnell01:05:25

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.

Kartik Kulkarni04:05:22

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.

๐Ÿ‘ 1
Sundarrajan Santhanam05:05:38

I am in ๐Ÿ™‚, but i am absolutely new to this

Kartik Kulkarni08:05:51

No problem we are all beginner. If you have finished the get started tutorial from the clojure website it's all fine.

Sundarrajan Santhanam16:05:01

can you share me the website, i will work it out that first then is this the one ? https://clojure.org/guides/getting_started

Kartik Kulkarni16:05:36

Yeah that's the one I did and then I am following http://clojure-doc.org/ right now

Gerome09:05:03

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

dpsutton13:05:53

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?

Gerome13:05:29

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.

Gerome13:05:17

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 ๐Ÿ™‚

dpsutton13:05:15

got a link to the repos?

Gerome13:05:27

Nope, everything is still local right now. Why do you ask?

dpsutton13:05:39

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

Gerome14:05:14

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.

lvh20:05:56

> However, when the app.js is processed, I get the following console ouput: What does "processed" mean here?

Gerome12:05:03

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.

Akshay C. Gollapalli18:05:34

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.

Gerome11:05:20

Thanks for the tip. Iโ€™ll have a look at it.

Endre Bakken Stovner12:05:55

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.

Ben Sless12:05:43

In case you don't want anyone to spoil the solution for you, I'll give you a hint - map can get several sequences

๐Ÿ‘ 2
Ben Sless12:05:13

i.e.

(map f xs1 xs2 xs3)
where f is a function of [a b c]

Endre Bakken Stovner12:05:18

(apply mapv vector %) seems to work.

Ben Sless12:05:27

correct ๐Ÿ™‚

Endre Bakken Stovner12:05:59

(partition (count v) (apply interleave v)) also works

Ben Sless13:05:45

lazy vs eager. I think the first might perform better

noisesmith14:05:56

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

Ben Sless17:05:24

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

noisesmith17:05:47

with lists you have O(n) object allocations for storage, vectors have arrays inside to batch allocations for multiple indexes into one new

noisesmith17:05:27

granular calls to new are more expensive than bulk allocations like vectors do (in general, best to measure of course)

Val Baca21:05:58

@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

1