Fork me on GitHub
#clojure
<
2020-08-24
>
Risetto10:08:12

How could I find all subdirectories of a certain name? Is using methods the way to go?

plexus10:08:00

file-seq + filter should get you there

Risetto10:08:03

Thank you!

Risetto10:08:59

I'm looking for several subdirectories, in quite a large directory. Would this be more or less efficient than any other option?

Ben Sless10:08:01

you can convert the search to a BFS and parallelize it with a core.async pipeline, which maintains order

Risetto10:08:03

Will take a look into it

Ben Sless11:08:59

feel free to ping if you need

Joe Lane14:08:15

@UFLJN302F You could also follow this example. https://clojuredocs.org/clojure.core/file-seq#example-59f3948ee4b0a08026c48c79 You would need to change a few things like change (filter #(.isFile %)) to (remove #(.isFile %)) since you're looking for directories. The grammar-matcher is likely what you're looking for. I wouldn't jump to core async just yet, see if an adaptation of the above example is sufficient for your needs.

jumar04:08:25

Using java interop might be faster: https://docs.oracle.com/javase/tutorial/essential/io/walk.html It depends on your use case but here I was trying to discover git repositories in a directory and it was significantly faster than naive list files and filter (although uglier):

(defn discover-no-subdirs [path]
  (let [git-repos (atom [])]
    (java.nio.file.Files/walkFileTree (java.nio.file.Paths/get path (make-array String 0))
                                      (proxy [java.nio.file.SimpleFileVisitor] []
                                        (preVisitDirectory [dir attrs]
                                          (if (-> (.resolve dir ".git") .toFile .exists)
                                            (do
                                              (swap! git-repos conj (str dir))
                                              java.nio.file.FileVisitResult/SKIP_SUBTREE)
                                            java.nio.file.FileVisitResult/CONTINUE))))
    @git-repos))

👍 1
souenzzo14:08:07

Now that we have extend-with-meta, should I prefer pass "functions as parameter" with protocols

(my/lib (with-meta {::data 42}
                   `{my/operation (fn [])}))
Over pass "as data"
(my/lib {::my/data      42
         ::my/operation (fn [])})
In a shot think: :thumbsup: Better errors: No implementation of method is way clear that NPE print: "Dumb" print methods, like prn or str may "hidden"/confuse developers in debugging tasks. But "richer" debuggers like REBL/cljs-devtools have dedicated "metadata" area :thumbsdown: A bit worse do write. :thumbsup: Serialize: if you need to write a map on wire (json/transit/edn), it will not include functions by default. harder do compose: should we always use alter-meta assoc over with-meta to not unintentionally remove a implementation? What you think about that?

alexmiller15:08:45

as usual, it probably depends on the rest of the context. :) one downside of the latter is fn's dont have good equality semantics

zilti16:08:35

Can someone tell me what's wrong with this namespace declaration? Clojure complains that there's "extra input":

(ns io.the-network.lib.segment
  (:require
   #[email protected](:clj
       []
       :cljs
       [[io.the-network.lib.form :as lib-form]])))
I can't for the life of me see what's wrong. I also manually re-typed this twice now.

dpsutton16:08:43

your clj branch will end up just (:require) i think

👍 1
zilti16:08:16

I tried putting an empty vector in there, so it is :clj [[]] but that gives the same error

zilti16:08:25

And I am pretty sure (:require []) is valid, no?

zilti16:08:52

At least I have other namespaces with an empty require vector and it works fine there. Though that is in the :cljs part there

dpsutton16:08:56

tmp ❯❯❯ clj
Clojure 1.10.1
user=> (ns bob (:require []))
Syntax error macroexpanding clojure.core/ns at (REPL:1:1).
((:require [])) - failed: Extra input spec: :clojure.core.specs.alpha/ns-form
user=>

zilti16:08:59

Well, thanks! I at first ruled something like that out, because at first, clj even complained about the ns declaration without any require whatsoever.

Joel18:08:16

(match [event auth]
      ["login" _] "LoginAttempt"
      ["login" "success"] "LoginSuccess"
      ["login" "failure"] "LoginFailure"
How can I coax clojure to return a list of matching results? I realize I could just make the result a list eg: ("LoginAttempt" "LoginFailure"), but I think for coding/readability, I'd prefer to be able to list each result separately.

MatthewLisp18:08:10

Hi Clojurists! I'm having a problem here with reading files on a project.

(defn custom-gen []  
  (fn []
    (s/gen #{x}) 
     (-> (slurp "test/resources/some-file.edn")    
     edn/read-string    
     :response    
     :body)))
the problem is that this project is a lib of another one, so we have a problem regarding the file path. when the main project tries to compile the lib, it cannot find the file. what should i use instead of slurp here? I know that slurp uses a reader that reads files relative to the project root, but the project root depends on running context right?

ghadi18:08:51

looks like there is a discarded form in your example (s/gen #{x}) seems to be thrown away

MatthewLisp18:08:39

i've redacted the code to show here, dunno if i've balanced parenthesis correctly and so on but the main thing here is the slurp argument file path

phronmophobic18:08:14

so if you move your edn file into resources directory (usually <project dir>/resources) you can do (slurp ( "test/some-file.edn"))

MatthewLisp18:08:07

thanks, I'll try that

schmidt7320:08:54

I'm writing a simple parse for different types of input and it can fail.

schmidt7320:08:22

(defmethod parse-query :text
  [text]
  (->> text
       (clojure.string/split-lines)
       (map #(rest (re-find #"(chr.*):(\d+)-(\d+)" %)))
       (map parse-row)))

schmidt7320:08:47

And I was wondering as to what is the idiomatic way to go about handling errors in Clojure? In Haskell I'd do something with Monads obviously, and the cats library looks like a nice solution, but is this the way to go?

phronmophobic21:08:38

instaparse is clojure parsing library which has a pretty idiomatic way to handle errors: https://github.com/engelberg/instaparse#parse-errors

lread21:08:36

Hi folks! I am trying to describe namespaced keywords and maps for rewrite-cljc with simple terms. I might have missed it but I don’t see definitive terminology in Clojure docs. Here’s my current shot at it: • current-namespaced for ::foo • alias-namespaced for ::alias/foo • literal-namespaced for prefix-ns/foo I think the first 2 are easy to grok but am not crazy about literal-namespaced maybe prefix-namespaced? Thoughts?

alexmiller21:08:07

the first two are "auto-resolved keywords", with current namespace and alias namespace, the latter is just a qualified keyword

alexmiller21:08:29

:foo is an unqualified keyword

alexmiller21:08:18

and important that auto-resolved keywords are syntactic sugar resolved by the reader, just shorthand for writing qualified keywords

lread21:08:22

Thanks so much @alexmiller! I should probably be more wordy for the first 2 then. • “auto-resolved current namespace” for ::foo • “auto-resolved alias namespace” for ::alias/foo • “qualified” for :prefix-ns/foo • “unqualified” for :foo And I’ll add your important note about syntactic sugar.