Yes, your example looks ok. About performance of reify vs proxy, http://clojure.org/datatypes explains this better than I could:
The method bodies of reify are lexical closures, and can refer to the > surrounding local scope. *reify* differs from *proxy* in that: > > > - Only protocols or interfaces are supported, no concrete superclass. > - The method bodies are true methods of the resulting class, not > external fns. > - Invocation of methods on the instance is direct, not using map > lookup. > - No support for dynamic swapping of methods in the method map. > > > The result is better performance than proxy, both in construction and > invocation. *reify* is preferable to proxy in all cases where its > constraints are not prohibitive. - Max On Thursday, February 14, 2013 8:08:37 PM UTC+1, Joachim De Beule wrote: > > > Thanks for the tip Max, so you mean like this? > > (defn cases->event-stream [cases features-extractor labeler] > (let [remaining (atom cases)] > (reify opennlp.model.EventStream > (next [this] (let [current (first @remaining)] > (swap! remaining rest) > (case->event current features-extractor labeler))) > (hasNext [this] (not (empty? @remaining)))))) > > May I ask why that would be faster? And when proxy is preferred over reify? > > Joachim. > > 2013/2/14 Max Penet <[email protected] <javascript:>> > >> >> Also It looks like you could use reify instead of proxy here, it would >> improve performance. >> >> http://clojuredocs.org/clojure_core/clojure.core/reify >> >> - Max >> >> >> On Thursday, February 14, 2013 3:26:02 PM UTC+1, Ulises wrote: >> >>> Without testing or anything that looks reasonable enough. I'm sure that >>> there are plenty caveats around infinite seqs, etc. though. >>> >>> >>> On 14 February 2013 14:22, Joachim De Beule <[email protected]>wrote: >>> >>>> Thanks! So you mean like this (assuming some function elt->event): >>>> >>>> (defn seq->event-stream [input-seq] >>>> (let [remaining (atom input-seq)] >>>> (proxy [EventStream] [] >>>> (next [] (let [current (first @remaining)] >>>> (swap! remaining rest) >>>> (elt->event current))) >>>> (hasNext [] (not (empty? @remaining)))))) >>>> >>>> >>>> >>>> 2013/2/14 Ulises <[email protected]> >>>> >>>>> How about having your methods access an atom provided in a closure >>>>> like it's done here: http://kotka.de/blog/**2010/03/proxy_gen-class_** >>>>> little_brother.html<http://kotka.de/blog/2010/03/proxy_gen-class_little_brother.html> >>>>> >>>>> U >>>>> >>>>> >>>>> On 14 February 2013 13:58, Joachim De Beule <[email protected]>wrote: >>>>> >>>>>> Hi All, >>>>>> >>>>>> I want to turn a clojure sequence into an 'EventStream' java >>>>>> interface in clojure (see http://opennlp.apache.** >>>>>> org/documentation/1.5.2-**incubating/apidocs/opennlp-** >>>>>> maxent/index.html<http://opennlp.apache.org/documentation/1.5.2-incubating/apidocs/opennlp-maxent/index.html>). >>>>>> >>>>>> Basically this is an object that implements next() and hasNext() methods. >>>>>> >>>>>> I know this can be done with proxy: >>>>>> >>>>>> (proxy [EventStream] [] >>>>>> (hasNext [] ...) >>>>>> (next [] (Event. ...) ...)) >>>>>> >>>>>> What I am not sure about is how to deal with state. More precisely, >>>>>> the object returned by the above call to proxy obviously must somehow >>>>>> keep >>>>>> a pointer to the current position in the input sequence and increment >>>>>> the >>>>>> index after a call to next() etc. >>>>>> >>>>>> Any ideas on how to best do something like this? >>>>>> >>>>>> Thanks a lot! >>>>>> Joachim. >>>>>> >>>>>> >>>>>> -- >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "Clojure" group. >>>>>> To post to this group, send email to [email protected] >>>>>> >>>>>> Note that posts from new members are moderated - please be patient >>>>>> with your first post. >>>>>> To unsubscribe from this group, send email to >>>>>> clojure+u...@**googlegroups.com >>>>>> >>>>>> For more options, visit this group at >>>>>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en> >>>>>> --- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "Clojure" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to clojure+u...@**googlegroups.com. >>>>>> >>>>>> For more options, visit >>>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>>>>> . >>>>>> >>>>>> >>>>>> >>>>> >>>>> -- >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Clojure" group. >>>>> To post to this group, send email to [email protected] >>>>> >>>>> Note that posts from new members are moderated - please be patient >>>>> with your first post. >>>>> To unsubscribe from this group, send email to >>>>> clojure+u...@**googlegroups.com >>>>> >>>>> For more options, visit this group at >>>>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en> >>>>> --- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Clojure" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to clojure+u...@**googlegroups.com. >>>>> >>>>> For more options, visit >>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>>>> . >>>>> >>>>> >>>>> >>>> >>>> -- >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To post to this group, send email to [email protected] >>>> >>>> Note that posts from new members are moderated - please be patient with >>>> your first post. >>>> To unsubscribe from this group, send email to >>>> clojure+u...@**googlegroups.com >>>> >>>> For more options, visit this group at >>>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en> >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to clojure+u...@**googlegroups.com. >>>> >>>> For more options, visit >>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>>> . >>>> >>>> >>>> >>> >>> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to [email protected]<javascript:> >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> [email protected] <javascript:> >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
