On Oct 31, 1:20 pm, André Thieme <[EMAIL PROTECTED]> wrote:
> In #Clojure Rich mentioned today that we can define our own data
> structures on top of the existing ones.
> Maybe someone wants/needs his own version of vectors that rearrange
> automatically in some specific order or whatever.
>
> If I now want to add something that works nearly like lists, but a
> little bit different, and for which I can use all functions that work
> for lists, I will have to use a proxy.
> Chris Houser implemented a little helper 
> tool:http://paste.lisp.org/display/67122#1
>
> Now what I would like to see are some minimal examples on how to do
> that. Lists are a good candidate as they only have 6 methods which we
> need to implement:
>
> user> (show clojure.lang.IPersistentList)
> ===  #=clojure.lang.IPersistentList  ===
> [ 0] cons : interface clojure.lang.IPersistentCollection (1)
> [ 1] count : int (0)
> [ 2] empty : interface clojure.lang.IPersistentCollection (0)
> [ 3] peek : class java.lang.Object (0)
> [ 4] pop : interface clojure.lang.IPersistentStack (0)
> [ 5] seq : interface clojure.lang.ISeq (0)
> nil
>
> The numbers in parens at the end tell us how many parameters the
> methods should have. They have a hidden one more. We can magically
> use “this”.
>
> 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, e.g. you don't use items
in your implementation, count as written is an infinite loop etc.

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.

Rich

--~--~---------~--~----~------------~-------~--~----~
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