Oz and Mozart Users Mailing List

Re: "Read" procedure?


From: Grzegorz Chrupała (grzegorz@pithekos.net)
Date: Fri Sep 26 2003 - 18:05:07 CEST


On Sat 27 Sep 2003 14:19, Denys Duchier wrote:

At this stage I just wanted something really simple, like read some
expressions from a file or socket, without having to understand the
complexities involved in instantiating a compiler engine or using Gump.

> - I assume you know about the Open module for IO, so I'll let you
> figure the IO part for yourself if it's needed :-)

Right. I figured out how to sequentially read characters (with the read
method) or lines (with the getS method). I just don't see any
straightforward way to read a file expression by expression.

> - {Compiler.virtualStringToValue +VS ?X}
> takes a virtual string as input (arg 1) and returns the
> corresponding Oz value as output (arg 2). Example:
>
> {Inspect {Compiler.virtualStringToValue "foo(a b:c)"}}

This works if you already have a string that represents a single
expression. What if you just have a file or stream? As an example, in
Scheme I can use something like the following to processes a file
expression-by-expression:

 (call-with-input-file
       "myfile"
       (lambda (file)
         (let loop ((expression (read file)))
                    (unless (eof-object? expression)
                      (do-something-with-expression expression)
                      (loop (read file))))))

Being able to do it can be very convenient in many circumstances.

> - if your target application is for reading persistent data, then you
> should consider using pickling instead. Example:
>
> {Save foo(a b:c) '/tmp/PickledData'}
> {Inspect {Load '/tmp/PickledData'}}
>
> This is a lot simpler and MUCH more efficient.

Yes I knew about this, very useful.
But sometimes I just want to write some records to a file in a
human-readable way, maybe modify them manually, and read them back later.
I could probably wrap all the records in a list or something and write it
out. Then to read them back, I could read the whole file into a string and
call Compliler.virtualStringToValue on it. But it seems less flexible than
being able to read single expressions as needed, as in Scheme.

I guess I'll have to have a closer look at Compiler engines and Gump after
all.

Thanks for your help!

-- 
Grzegorz Chrupała
http://pithekos.net

- Please send submissions to users@mozart-oz.org and administriva mail to users-request@mozart-oz.org. The Mozart Oz web site is at http://www.mozart-oz.org/. Please send bug reports to bugs@mozart-oz.org.



This archive was generated by hypermail 2b29.