beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
evocatus 2021-05-16T00:07:48.297900Z

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

simongray 2021-05-16T08:29:42.298500Z

Some people get around that by writing all of their imperative code inside the let binding vector 😉

☝️ 1
teodorlu 2021-05-19T13:42:51.064800Z

You'll find some examples of "big let" in #babashka source

teodorlu 2021-05-19T13:44:00.065100Z

(let [x 3
      ;; _ -> discard return
      _ (spit "file" x)]
  (* x 10))

teodorlu 2021-05-19T13:45:00.065500Z

Clojure core functions are often quite big

valerauko 2021-05-16T03:41:45.298100Z

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

Ivan Koz 2021-05-16T08:39:11.299300Z

@vale isn't that a perfect case for a macro?

alexmiller 2021-05-16T13:02:01.300600Z

arrays, as Java objects, have no place to put meta

valerauko 2021-05-16T13:08:57.301100Z

it's weird that the compiler knows it's a java object that can't take meta but still demands type hints for it

alexmiller 2021-05-16T13:09:28.301700Z

you're confusing what's known at compile-time and run-time

alexmiller 2021-05-16T13:10:59.303300Z

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

valerauko 2021-05-16T13:12:09.303600Z

hmm

valerauko 2021-05-16T13:20:00.304200Z

thanks that was a gloriously nice hint

valerauko 2021-05-16T13:20:04.304400Z

i got it working

valerauko 2021-05-16T13:20:11.304900Z

(defmacro ->typed-array
  [klass things]
  (let [^Class resolved (resolve klass)]
    (with-meta
     (list 'into-array resolved things)
     {:tag (str "[L" (.getName resolved) ";")})))

alexmiller 2021-05-16T13:20:20.305100Z

was just going to post that :)

valerauko 2021-05-16T13:21:35.305400Z

it would be smooth sailing if this was into-array's built-in behavior

valerauko 2021-05-16T13:22:38.305900Z

with this i could get rid of all the into-array type hint monsters

valerauko 2021-05-16T13:22:39.306100Z

❤️

alexmiller 2021-05-16T13:22:53.306200Z

well it wouldn't be a function then

alexmiller 2021-05-16T13:23:18.306600Z

so that's not a thing we could change

alexmiller 2021-05-16T13:29:14.307Z

but could be a new wrapper macro

alexmiller 2021-05-16T13:51:49.307200Z

logged as https://clojure.atlassian.net/browse/CLJ-2631

👍 2
💯 4
🚀 2
Ivan Koz 2021-05-16T14:43:50.308600Z

@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")