I noticed that Clojure has a BIG incentive to write small functions - you cannot write long imperative walls of text here because every new binding (`let`, loop
, etc.) increases nesting level
Some people get around that by writing all of their imperative code inside the let binding vector 😉
You'll find some examples of "big let" in #babashka source
(let [x 3
;; _ -> discard return
_ (spit "file" x)]
(* x 10))
Clojure core functions are often quite big
Here's one: https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L225-L261
yup last works just fine for this...
(let [files [["src1" "dst1"] ["src2"]]]
(doseq [file files]
(let [from (first file)
to (last file)]
(println "copying" from "to" to))))
copying src1 to dst1
copying src2 to src2
@vale isn't that a perfect case for a macro?
arrays, as Java objects, have no place to put meta
it's weird that the compiler knows it's a java object that can't take meta but still demands type hints for it
you're confusing what's known at compile-time and run-time
what you're really looking for is something that sets the return type on the into-array expression (the code, which is a list) such that the compiler can resolve the surrounding Java interop call at compile time into a non-reflective call
hmm
thanks that was a gloriously nice hint
i got it working
(defmacro ->typed-array
[klass things]
(let [^Class resolved (resolve klass)]
(with-meta
(list 'into-array resolved things)
{:tag (str "[L" (.getName resolved) ";")})))
was just going to post that :)
it would be smooth sailing if this was into-array's built-in behavior
with this i could get rid of all the into-array type hint monsters
❤️
well it wouldn't be a function then
so that's not a thing we could change
but could be a new wrapper macro
@vale a little snippet how to get array classes programmatically
(let [classes (apply list java.lang.String java.lang.Object (map (comp eval symbol second) primitives-classnames))]
(map #(.. (do %) (arrayType) (getName)) classes))
=> ("[Ljava.lang.String;" "[Ljava.lang.Object;" "[F" "[I" "[J" "[Z" "[C" "[D" "[B" "[S")