Skip to content

Instantly share code, notes, and snippets.

@gyk
Created September 13, 2021 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyk/0ea2501bb291577c45c1f35cebae949b to your computer and use it in GitHub Desktop.
Save gyk/0ea2501bb291577c45c1f35cebae949b to your computer and use it in GitHub Desktop.
Generate random file name in Clojure
; Better to use test.check
(defn random-name
[]
(let [char-ranges [[\A \Z]
[\a \z]
[\0 \9]]
chars (->> char-ranges
(mapcat (fn [[from-char to-char]]
(map char (range (int from-char) (inc (int to-char))))))
(concat [\_ \space \-])
vec)]
(let [base-len (rand-nth (range 1 (inc 10)))
ext-len (rand-nth (range 0 (inc 3)))
basename (repeatedly base-len #(rand-nth chars))
ext (repeatedly ext-len #(rand-nth chars))]
(apply str (concat basename
(when-not (empty? ext)
[\.])
ext)))))
@gyk
Copy link
Author

gyk commented Sep 14, 2021

List files using Babashka:

(defn list-files
  [root]
  (->> (fs/file root)
       file-seq
       (map #(fs/relativize root %))
       (map str)
       (filter #(not (str/starts-with? % ".")))
       (remove empty?)))

or

(defn list-files
  [root]
  (->> (fs/glob root "[!.]**")
       (map #(fs/relativize root %))
       (map str)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment