Re: where did I read that?

2010-08-29 Thread Robert McIntyre
That's a neat improvement to be able to ignore the value when you
don't need it, thanks Michał Marczyk.

Also, though both proxies happen to work with clojure's (read)  function,
every read operation of the pushback reader would have to be wrapped
in the variable changing code
to guarantee correctness in all cases.  So the proxy needs about 3-5
more methods to be a complete covering.

you wouldn't want me to do
(.skip reader 5)

and have *current-char-number* become wrong!

The perils of mutable state are made of this... :(

>From a code reuse standpoint, isn't this kinda lame?

--Robert McIntyre

On Sat, Aug 28, 2010 at 5:28 PM, Michał Marczyk
 wrote:
> On 28 August 2010 22:14, evins.mi...@gmail.com  wrote:
>> I was actually asking how to avoid doing what you did :-). If it's
>> necessary to do it, that's fine, but I thought I'd ask first, in case
>> there was a way around it that I hadn't noticed.
>
> Well, some level of reimplementation will probably be necessary (if
> there is an implementation of an "indexing reader" in the base class
> libraries, I'd love to learn about it, but I don't think there is!).
>
> Having said that, you could take Robert's approach and have the proxy
> implement a new interface (which you can define through definterface)
> to check current offset:
>
> ;;; untested...
>
> (definterface IReaderOffset
>  (^int readerOffset []))
>
> ;;; and the proxy form:
> (proxy [java.io.PushbackReader IReaderOffset] [...]
>  (read ...)
>  (unread ...)
>  (readerOffset [] @*current-char-number*))
>
> ;;; also, drop the final (fn ...) form from read-with-number --
> ;;; return the proxy itself instead; maybe also rename
> ;;; to e.g. indexing-reader?
>
> Then you could use the regular read function and say (.readerOffset
> reader-instance) to find out what the current index is. This does at
> least have the nice property that you can ignore the extra information
> when it's not needed (as you could with multiple return values in CL).
>
> Sincerely,
> Michał
>
> --
> 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

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


Re: where did I read that?

2010-08-28 Thread Michał Marczyk
On 28 August 2010 22:14, evins.mi...@gmail.com  wrote:
> I was actually asking how to avoid doing what you did :-). If it's
> necessary to do it, that's fine, but I thought I'd ask first, in case
> there was a way around it that I hadn't noticed.

Well, some level of reimplementation will probably be necessary (if
there is an implementation of an "indexing reader" in the base class
libraries, I'd love to learn about it, but I don't think there is!).

Having said that, you could take Robert's approach and have the proxy
implement a new interface (which you can define through definterface)
to check current offset:

;;; untested...

(definterface IReaderOffset
  (^int readerOffset []))

;;; and the proxy form:
(proxy [java.io.PushbackReader IReaderOffset] [...]
  (read ...)
  (unread ...)
  (readerOffset [] @*current-char-number*))

;;; also, drop the final (fn ...) form from read-with-number --
;;; return the proxy itself instead; maybe also rename
;;; to e.g. indexing-reader?

Then you could use the regular read function and say (.readerOffset
reader-instance) to find out what the current index is. This does at
least have the nice property that you can ignore the extra information
when it's not needed (as you could with multiple return values in CL).

Sincerely,
Michał

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


Re: where did I read that?

2010-08-28 Thread evins.mi...@gmail.com


On Aug 28, 1:41 am, Robert McIntyre  wrote:
> I took a stab at it and came up with this:

> is that what you're going for?

I was actually asking how to avoid doing what you did :-). If it's
necessary to do it, that's fine, but I thought I'd ask first, in case
there was a way around it that I hadn't noticed.

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


Re: where did I read that?

2010-08-28 Thread Robert McIntyre
I took a stab at it and came up with this:

(defn make-reader [s]
  (java.io.PushbackReader. (java.io.CharArrayReader.
(into-array Character/TYPE (seq s)

(defn read-with-number
  "like read but takes in a string and returns a function
   of no arguments which will read the string and provide char number
   as in Common Lisp"
  [string]
  (let [*current-char-number* (atom 0)
numbering-pushback-reader
(proxy  [java.io.PushbackReader]
[(make-reader string)]
  (read [] (swap! *current-char-number* inc) (proxy-super read
))
  (unread [stuff] (swap! *current-char-number* dec)
(proxy-super unread stuff))
  )]

(fn [] [(read numbering-pushback-reader) (deref *current-char-number*)])))


Now you can do:

mobius.physics> (def read-oh-yeah! (read-with-number "(vector 1 2 3)
(list 5 6)"))
#'mobius.physics/read-oh-yeah!
mobius.physics> (read-oh-yeah!)
[(vector 1 2 3) 14]
mobius.physics> (read-oh-yeah!)
[(list 5 6) 25]


is that what you're going for?  please tell me how I can improve this!

--Robert McIntyre

On Sat, Aug 28, 2010 at 1:26 AM, evins.mi...@gmail.com
 wrote:
> I'm working on a project in which it would be very useful to be able
> to easily determine how many characters were consumed in the course of
> a read operation, in a similar fashion to the way that Common Lisp's
> read-from-string returns as a second value the index of the next
> character of the input past the end of the object that was read. I
> want to, for example, read Clojure values from a buffer and keep track
> of where in the buffer they were read from.
>
> Anyone have any good ideas of how to accomplish this without some
> level of reimplementation of read?
>
> --
> 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

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


where did I read that?

2010-08-27 Thread evins.mi...@gmail.com
I'm working on a project in which it would be very useful to be able
to easily determine how many characters were consumed in the course of
a read operation, in a similar fashion to the way that Common Lisp's
read-from-string returns as a second value the index of the next
character of the input past the end of the object that was read. I
want to, for example, read Clojure values from a buffer and keep track
of where in the buffer they were read from.

Anyone have any good ideas of how to accomplish this without some
level of reimplementation of read?

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