So, the current version, after the suggestion by Alan Malloy, is the 
following:

(defmacro buffer-reduce [b f val]
  `(let [b# (.duplicate ~b)
         f# ~f]
     (loop [remaining# (.remaining b#)
            result# ~val]
       (if (zero? remaining#)
         result#
         (recur (dec remaining#) (f# result# (.get b#)))))))

(extend-protocol clojure.core.protocols/CollReduce
  java.nio.ByteBuffer
  (coll-reduce [b f] (coll-reduce b f (f)))
  (coll-reduce [b f val] (buffer-reduce b f val))

  java.nio.LongBuffer
  (coll-reduce [b f] (coll-reduce b f (f)))
  (coll-reduce [b f val] (buffer-reduce b f val))

  ; ... other kinds of buffer ...
)


About the other suggestions:

 - Herwig Holchleitner: how would that solution with protocols be? I write 
a protocol that defines methods like `get` and `duplicate` and extend it 
for each buffer, delegating the methods to their specific implementations? 
ie, (defprotocol UnifiedBuffers (get [buff] "get data from buf") (duplicate 
[buff] "duplicates buff")), (extend-protocol UnifiedBuffers ....ByteBuffer 
(get [buff] (.get buff)) ....LongBuffer (get [buff] (.get buff))....)?

 - David: about the solution you propose, is it somehow considered 
"cleaner" or "faster" or "more idiomatic" or, in any way, "better" than the 
solution I'm proposing? The advantages might be way more subtle than my 
small knowledge of Clojure allow me to understand -- I'm new in the Clojure 
community, I might be missing some common idioms. I'd be glad if you could 
expand on that!


Thanks!


On Thursday, October 18, 2012 8:44:41 PM UTC-3, David Nolen wrote:
>
> On Thu, Oct 18, 2012 at 7:15 PM, Herwig Hochleitner 
> <hhochl...@gmail.com<javascript:>
> > wrote:
>
>> 2012/10/19 Bruno França dos Reis <bfr...@gmail.com <javascript:>>
>>
>>> The reason why I wrote a macro instead of a function is because 'get' 
>>> and 'duplicate' are not declared in any common superclass of the different 
>>> buffers, so I was getting lots of reflection!
>>>
>>> Are there alternatives to the macro that avoid reflection?
>>>
>>  You could use a protocol for that, though it might not be quite as fast 
>> as the directly type hinted version, but certainly faster than reflection.
>>
>> kind regards
>>
>
> Or write a macro that can take a type and create a type specialized 
> version of buffer-reduce - long-buffer-reduce, byte-buffer-reduce etc.
>
> David 
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to