Fork me on GitHub
#clojure
<
2023-05-29
>
Colin (fosskers)08:05:43

Am I right that there is no analog to first and last in the stdlib Transducer reducing functions? Like:

(defn rfirst
  "Grab the first item that makes it through the transduction."
  ([] nil)
  ([acc] acc)
  ([_ input] (reduced input)))

tomd08:05:05

You are right, but you can do (halt-when any?) to make a first transducer.

tomd08:05:40

I'm not sure a last transducer is particularly meaningful in the context of transducers :thinking_face:

Colin (fosskers)08:05:52

Ah I see that that produces a transducer whereas the function I wrote above is a reducing function (called reducers elsewhere, but in Clojure that name seems to mean something else)

👍 2
Colin (fosskers)08:05:34

My intent was (transduce (filter pred) rfirst items)

Colin (fosskers)08:05:17

For instance. And of course with comp there can be many steps before or after that filtering.

tomd08:05:55

Yeah that works. See the example I did for any? on clojuredocs

Colin (fosskers)08:05:53

Ah does any? function like a reducer?

tomd08:05:48

No. It's just the same as (constantly true) which is what halt-when needs to become produce a first transducer

Colin (fosskers)08:05:13

ahhh I see, I had assumed it behaved like any seen elsewhere

tomd08:05:57

Tbh if you're writing the reducing function anyway, you may not need transducers. They just manipulate the reducing function after all.

tomd08:05:25

It's more about readability at this point I suppose

Colin (fosskers)08:05:08

For more complex transduction chains, first can be handy, but there are other ways to achieve it depending on the scenario, as you've pointed out

👍 2
Colin (fosskers)08:05:45

Thanks! And if I need such an rfirst I can always hand-write it quickly enough anyway

🙂 2
Can19:05:14

(defn regex-file-seq
  "Lazily filter a directory based on a regex."
  [re dir]
  (filter #(re-find re (.getPath %)) (file-seq dir)))

(mapv
  (fn [file] {:name (.getName file), :content (slurp file)})
  (filter
    (fn [file] (not (.isDirectory file)))
    (regex-file-seq #".*\.clj" ( "/Users/bariscanates/prj/electric"))))
Hello! I have a question 🙂 The problem is this file seq brings all of json, js, map, md, clj, cljc and other types of files. I just need to filter them with absolute clj, cljc and edn. I tried to solve it with the "regex-file-seq" method but it still returns some other files types like ---> "/Users/bariscanates/prj/electric/.shadow-cljs/builds/dev/dev/ana/hyperfiddle/rcf/queue.cljs.cache.transit.json"] some other files. Where am I doing a mistake?

seancorfield19:05:59

The $ will anchor the match at the end of the string.

Can19:05:30

ah awesome! Thanks for the explanation.

Can19:05:59

is there any chance that without using extra codes or fors to filter clj and cljc at the same time?

seancorfield19:05:39

#".*\.(cljc?|edn)$" .clj, .cljc, and .edn

Can19:05:29

thank you so much, have a good day/evening 🙂

seancorfield19:05:56

You might find https://regex101.com/ helpful if you are just learning about regex.

1
👀 1
skylize20:05:19

Probably good to accept cljs files too, right?

#".*\.(clj[cs]?|edn)$"

seancorfield20:05:05

He said "I just need to filter them with absolute clj, cljc and edn." in his original post so I assumed he wasn't interested in cljs flies...

skylize20:05:40

Of course. I was just thinking forward. Use of cljc instead of just clj implies intent to use ClojureScript at some point.

1
skylize20:05:07

Or maybe Babashka?

#".*\.(clj[cs]?|edn|bb)$"

Can06:05:39

Oh thanks for the more information, I didn't see the messages sorry for late answer.

simongray07:05:13

There’s also str/ends-with? BTW since regex maybe isn’t totally necessary here.

👍 2