Created
September 13, 2021 08:00
-
-
Save gyk/0ea2501bb291577c45c1f35cebae949b to your computer and use it in GitHub Desktop.
Generate random file name in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
List files using Babashka:
or