Fork me on GitHub
#clojure
<
2020-11-01
>
practicalli-john20:11:55

Without reading through 7892 lines of code that makes up the clojure.core namespace, how can I tell which of those function would take key/value pairs? I am trying to find out which clojure.core functions (or anything from the Clojure standard library) I can call on the command line using Clojure exec. e.g. clojure -X clojure.pprint/pprint :functions 569 :macros 62 :execfn 2

seancorfield21:11:46

"take key/value pairs" -- you mean "take a map" for this use case, right?

👍 3
seancorfield21:11:38

And, specifically, that will accept a single argument -- a hash map. So that narrows it down to 1-arity or varargs.

Alex Miller (Clojure team)21:11:48

I’m sure you could use the new @borkdude utility to find all functions take a map :)

👍 3
seancorfield21:11:53

(I think it's a much smaller list of functions than you might imagine since most things that operate on a map, tend to take at least one other argument)

Alex Miller (Clojure team)21:11:14

Yeah, there are very few useful functions I can think of

practicalli-john21:11:18

I havent had a chance to 'grasp' that tool yet 🙂 but seems I have just found a use for it...

seancorfield21:11:35

But things like count work, because it takes a single argument that can have seq called on it 🙂

practicalli-john21:11:30

the site is down, did you do an update in the last half hour. I was just looking at the clj articles.. will try again.

practicalli-john21:11:39

Hmm, it seems the server isnt serving..

phronmophobic21:11:07

this link works for me 🤷

practicalli-john21:11:49

Maybe its just the uk...

🤷 3
practicalli-john21:11:44

It was working earlier for me, as I was reading the clj exec articles...

practicalli-john21:11:52

its working again now 🙂

Alex Miller (Clojure team)03:11:31

served off of github so ... ¯\(ツ)

borkdude21:11:03

hmm, how can you, by looking at the form, infer that a function takes a map? I guess you can look at keys destructuring. but for example prn also takes a map

borkdude21:11:44

I only find two functions in all of Clojure's source with keys destructuring in the first argument and those are in the tests:

$ grasp ~/git/clojure -e "(g/seq 'defn symbol? (g/vec (s/keys :req-un [::keys]) (s/* any?)) (s/* any?))"
file:/Users/borkdude/git/clojure/test/clojure/test_clojure/special.clj:105:5
    (defn foo
      [{:keys [^String s]}]
      (.indexOf s "boo"))))

file:/Users/borkdude/git/clojure/test/clojure/test_clojure/transducers.clj:149:1
(defn result-good?
  [{:keys [s xs xi xe xt]}]
  (= s xs xi xe xt))

borkdude21:11:47

Forgot to account for docstrings etc, but even then I find only a few and only in the tests

borkdude21:11:45

if we're looking for keys destructuring in any position:

$ grasp ~/git/clojure/src/clj/clojure -e "(s/def ::keys-arg  (s/keys :req-un [::keys])) (g/seq 'defn (s/* any?) (g/vec (s/* any?) ::keys-arg (s/* any?)) (s/* any?))" | grep file:
file:/Users/borkdude/git/clojure/src/clj/clojure/core/server.clj:191:1
file:/Users/borkdude/git/clojure/src/clj/clojure/core/server.clj:272:1
file:/Users/borkdude/git/clojure/src/clj/clojure/core/server.clj:295:1
file:/Users/borkdude/git/clojure/src/clj/clojure/main.clj:584:1
This is only for single-arity fns, but you get the gist