On 1 Nov., 14:12, Rich Hickey <[EMAIL PROTECTED]> wrote:
> On Oct 31, 1:20 pm, André Thieme <[EMAIL PROTECTED]> wrote:
> > Here is what I tried:
>
> > user> (defn make-lyst [& items]
> >         (proxy [clojure.lang.IPersistentList] []
> >                (cons [obj] (cons obj this))
> >                (count []   (count this))
> >                (empty []   ())
> >                (peek  []   5)
> >                (pop   []   6)
> >                (seq   []   this)))
>
> > As I found no examples yet, I was hoping this would return an instance
> > of my own list type.
> > (make-lyst 1 2 3) would print like normal lists:  (1 2 3)
> > And it would use the cons function of lists, dito for counting.
> > (empty lyst) would return an empty IPersistentList, peek always a 5,
> > pop
> > always a 6 and seq the list object itself.
>
> > But unfortunately (make-lyst 1 2 3) ==>
> > java.lang.ClassCastException: clojure.lang.Proxy__5307 cannot be cast
> > to clojure.lang.ISeq
>
> > Any ideas how this should be done so that it will work?
>
> It's still unclear to me what you want to do

What I want to have is a basic working example.
And it seems I have it now, since:

> e.g. you don't use items

Ah okay, I see.
When I copy my example and replace “this” with “items” it works.
I tried this with IPersistentVector before, and there got some
exceptions,
but I don’t fully remember.

So, this is basically working:
user> (defn make-lyst [& items]
        (proxy [clojure.lang.IPersistentList] []
                (cons [obj] (cons obj items))
                (count []   (count items))
                (empty []   ())
                (peek  []   5)
                (pop   []   (rest this))
                (seq   []   items)))
#=(var user/make-lyst)
user> (make-lyst 1 2 3)
(1 2 3)
user> (peek (make-lyst 1 2 3))
5
user> (count (make-lyst 1 2 3))
3

Although I suppose it still has some errors.
For example: I don’t understand why these functions count, empty,
peek and so on don’t take an argument.
Also when I do (class (cons 100 (make-lyst 1 2 3))) ==>
#=clojure.lang.Cons
and not #=clojure.lang.Proxy__5678


> in your implementation, count as written is an infinite loop etc.

I see. It is not clear to me how to call the method count which is
working correctly for lists.


> But the cause of your error is that you are saying that this proxy is
> its own seq (by returning this from seq). In order to do that, the
> proxy has to implement ISeq as well. If you want seq to pass over
> items it might be better to implement it as
>
> (seq [] (seq items))
>
> It seems to me in all places in which you use 'this you really meant
> to say items, and should.

That did the trick.


Although this is working now I think if I really want to add a new
list type the way Meikel is going is the better one, instead of using
a proxy.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to