Fork me on GitHub

reify might have the worst doc-string of them all: > reify is a macro with the following structure: > (reify options* specs*)



Clojure 1.10.3
user=> (doc reify)
([& opts+specs])
  reify is a macro with the following structure:

 (reify options* specs*)
  Currently there are no options.

  Each spec consists of the protocol or interface name followed by zero
  or more method bodies:

  (methodName [args+] body)*

  Methods should be supplied for all methods of the desired
  protocol(s) and interface(s). You can also define overrides for
  methods of Object. Note that the first parameter must be supplied to
  correspond to the target object ('this' in Java parlance). Thus
  methods for interfaces will take one more argument than do the
  interface declarations.  Note also that recur calls to the method
  head should *not* pass the target object, it will be supplied
  automatically and can not be substituted.

  The return type can be indicated by a type hint on the method name,
  and arg types can be indicated by a type hint on arg names. If you
  leave out all hints, reify will try to match on same name/arity
  method in the protocol(s)/interface(s) - this is preferred. If you
  supply any hints at all, no inference is done, so all hints (or
  default of Object) must be correct, for both arguments and return
  type. If a method is overloaded in a protocol/interface, multiple
  independent method definitions must be supplied.  If overloaded with
  same arity in an interface you must specify complete hints to
  disambiguate - a missing hint implies Object.

  recur works to method heads The method bodies of reify are lexical
  closures, and can refer to the surrounding local scope:
  (str (let [f "foo"] 
       (reify Object 
         (toString [this] f))))
  == "foo"

  (seq (let [f "foo"] 
       (reify clojure.lang.Seqable 
         (seq [this] (seq f)))))
  == (\f \o \o))
  reify always implements clojure.lang.IObj and transfers meta
  data of the form to the created object.
  (meta ^{:k :v} (reify Object (toString [this] "foo")))
  == {:k :v}


Ya, it goes into some details, but the summary is: > reify is a macro with the following structure:


It never really explains what its for


Right. Time to create an question with a suggested edit? :)

✔️ 3
Alex Miller (Clojure team)12:10:25

There is an active ticket for this already

Alex Miller (Clojure team)12:10:52

CLJ-2488 is screened for Rich to look at in 1.11

🎉 4
Ben Wiz10:10:20

I am debugging something. I have a TransientHashSet I must inspect. I cannot call persistent! because the library exposing the TransientHashSet expects to continue modifying it. What can I do? If It were a TransientVector I could call nth on it but since it is a set I cannot

Timur Latypoff10:10:50

Do exposed Java methods work for inspection?

Ed10:10:54 It looks like it extends ATransientSet, so you can call things like .count and .contains ...


but I don't see an easy way to get an iterator without reflection ...


I might well not be looking properly 😜

Ben Wiz10:10:52

Hacky solution

(into []
      (keep-indexed (fn [idx v]
                      (when (even? idx)
      (.-arr ^TransientArrayMap (.-transient_map ^TransientHashSet s)))


No reason you can't persist, inspect, then turn it back into a transient

👍 2

assuming your inspection is somewhere in the middle of a pipeline or something


Hi! Is it intended that sorted-map-by freaks out if the comparator is weird? I'd expect the output to be unstable, but not for it to be downright missing data from the input.

user=> (defn kinda> [a b] (> (max a 2) (max b 2)))
user=> (sorted-map-by kinda> 1 "a", 2 "b", 3 "c")
{3 "c", 1 "b"}


ah! thank you 🙂


that isn't even a comparator issue


it is a comparator issue, I just get confused about sorted-map-by, because the -by means something different from the by in sort-by


what is the complexity of count on a vector? O(n)? Or is there something like the length of the vector stored somewhere that allows O(1) ?


you can use counted? to determine if coll implements count in constant time


perfect thank you !


> (counted? [1 2 3])