I'm trying to figure out a way to save the result of a function to a
file. I'm using a code similar to this:
(with-open-file (s "/home/hom/foo" :direction :output)
(write-sequence (foo) s))
The problem is (foo) might return either a character sequence or an
octet sequence. The code gets an error if (foo) returns a sequence of
octets. If I add :element-type '(unsigned-byte 8) I get an error
message when (foo) returns a sequence of characters. Can anyone please
suggest a way to correct the code so that it works with any kind of
sequence? It seems to me that it should be trivial. Maybe I'm missing
something.
I'm using SBCL 1.0.16 if it matters to the problem.
Homayoon <···········@gmail.com> writes:
> I'm trying to figure out a way to save the result of a function to a
> file. I'm using a code similar to this:
>
> (with-open-file (s "/home/hom/foo" :direction :output)
> (write-sequence (foo) s))
>
> The problem is (foo) might return either a character sequence or an
> octet sequence. The code gets an error if (foo) returns a sequence of
> octets. If I add :element-type '(unsigned-byte 8) I get an error
> message when (foo) returns a sequence of characters. Can anyone please
> suggest a way to correct the code so that it works with any kind of
> sequence? It seems to me that it should be trivial. Maybe I'm missing
> something.
>
> I'm using SBCL 1.0.16 if it matters to the problem.
(let ((result (foo)))
(with-open-file (s "/home/hom/foo"
:direction :output
:element-type (typecase (elt result 0)
(character 'character)
(t '(unsigned-byte 8))))
(write-sequence result s)))
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Indentation! -- I will show you how to indent when I indent your skull!"
On Sep 20, 12:07 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Homayoon <···········@gmail.com> writes:
> > I'm trying to figure out a way to save the result of a function to a
> > file. I'm using a code similar to this:
>
> > (with-open-file (s "/home/hom/foo" :direction :output)
> > (write-sequence (foo) s))
>
> > The problem is (foo) might return either a character sequence or an
> > octet sequence. The code gets an error if (foo) returns a sequence of
> > octets. If I add :element-type '(unsigned-byte 8) I get an error
> > message when (foo) returns a sequence of characters. Can anyone please
> > suggest a way to correct the code so that it works with any kind of
> > sequence? It seems to me that it should be trivial. Maybe I'm missing
> > something.
>
> > I'm using SBCL 1.0.16 if it matters to the problem.
>
> (let ((result (foo)))
> (with-open-file (s "/home/hom/foo"
> :direction :output
> :element-type (typecase (elt result 0)
> (character 'character)
> (t '(unsigned-byte 8))))
> (write-sequence result s)))
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> "Indentation! -- I will show you how to indent when I indent your skull!"
Thanks. I should have thought of that myself, although admittedly I
expected there should be some way without enumerating all possible
result types.
On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
> I'm trying to figure out a way to save the result of a function to a
> file. I'm using a code similar to this:
>
> (with-open-file (s "/home/hom/foo" :direction :output)
> (write-sequence (foo) s))
>
> The problem is (foo) might return either a character sequence or an
> octet sequence. The code gets an error if (foo) returns a sequence of
> octets. If I add :element-type '(unsigned-byte 8) I get an error message
> when (foo) returns a sequence of characters. Can anyone please suggest a
> way to correct the code so that it works with any kind of sequence? It
> seems to me that it should be trivial. Maybe I'm missing something.
>
> I'm using SBCL 1.0.16 if it matters to the problem.
(with-open-file (s "/tmp/foo" :direction :output :element-type :default)
(write-sequence "Sto'Vo'Kor" s))
On Sep 20, 4:33 am, alien_guy <····@l.org> wrote:
> On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
> > I'm trying to figure out a way to save the result of a function to a
> > file. I'm using a code similar to this:
>
> > (with-open-file (s "/home/hom/foo" :direction :output)
> > (write-sequence (foo) s))
>
> > The problem is (foo) might return either a character sequence or an
> > octet sequence. The code gets an error if (foo) returns a sequence of
> > octets. If I add :element-type '(unsigned-byte 8) I get an error message
> > when (foo) returns a sequence of characters. Can anyone please suggest a
> > way to correct the code so that it works with any kind of sequence? It
> > seems to me that it should be trivial. Maybe I'm missing something.
>
> > I'm using SBCL 1.0.16 if it matters to the problem.
>
> (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
> (write-sequence "Sto'Vo'Kor" s))
This is exactly what I was looking for. Thanks a lot. Incidentally,
where can I find all possible values for element-type? I looked up the
CLHS but it wasn't there. Isn't element-type part of the ANSI standard?
In article
<····································@k13g2000hse.googlegroups.com>,
Homayoon <···········@gmail.com> wrote:
> On Sep 20, 4:33�am, alien_guy <····@l.org> wrote:
> > On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
> > > I'm trying to figure out a way to save the result of a function to a
> > > file. I'm using a code similar to this:
> >
> > > (with-open-file (s "/home/hom/foo" :direction :output)
> > > � (write-sequence (foo) s))
> >
> > > The problem is (foo) might return either a character sequence or an
> > > octet sequence. The code gets an error if (foo) returns a sequence of
> > > octets. If I add :element-type '(unsigned-byte 8) I get an error message
> > > when (foo) returns a sequence of characters. Can anyone please suggest a
> > > way to correct the code so that it works with any kind of sequence? It
> > > seems to me that it should be trivial. Maybe I'm missing something.
> >
> > > I'm using SBCL 1.0.16 if it matters to the problem.
> >
> > (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
> > � (write-sequence "Sto'Vo'Kor" s))
>
> This is exactly what I was looking for. Thanks a lot. Incidentally,
> where can I find all possible values for element-type? I looked up the
> CLHS but it wasn't there. Isn't element-type part of the ANSI standard?
It's just a type specifier, and the allowable types depend on the
implementation.
I think the above use of :DEFAULT is an implementation extension.
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Homayoon <···········@gmail.com> writes:
> On Sep 20, 4:33�am, alien_guy <····@l.org> wrote:
>> On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
>> > I'm trying to figure out a way to save the result of a function to a
>> > file. I'm using a code similar to this:
>>
>> > (with-open-file (s "/home/hom/foo" :direction :output)
>> > � (write-sequence (foo) s))
>>
>> > The problem is (foo) might return either a character sequence or an
>> > octet sequence. The code gets an error if (foo) returns a sequence of
>> > octets. If I add :element-type '(unsigned-byte 8) I get an error message
>> > when (foo) returns a sequence of characters. Can anyone please suggest a
>> > way to correct the code so that it works with any kind of sequence? It
>> > seems to me that it should be trivial. Maybe I'm missing something.
>>
>> > I'm using SBCL 1.0.16 if it matters to the problem.
>>
>> (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
>> � (write-sequence "Sto'Vo'Kor" s))
>
> This is exactly what I was looking for. Thanks a lot. Incidentally,
I doubt it.
clhs open:
element-type---a type specifier for recognizable subtype of
character; or a type specifier for a finite recognizable subtype
of integer; por one of the symbols signed-byte, unsigned-byte, or
:default. The default is character.
^^^^^^^^^^^^^^^^^^^^^^^^^
C/USER[23]> (with-open-file (s "/tmp/foo" :direction :output
:element-type :default)
(write-sequence #(1 2 3) s))
*** - EXT:WRITE-CHAR-SEQUENCE: cannot output 1 into
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"/tmp/foo">,
not of type CHARACTER
> where can I find all possible values for element-type? I looked up the
> CLHS but it wasn't there. Isn't element-type part of the ANSI standard?
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Debugging? Klingons do not debug! Our software does not coddle the
weak."
···@informatimago.com (Pascal J. Bourguignon) writes:
> Homayoon <···········@gmail.com> writes:
>
> > On Sep 20, 4:33�am, alien_guy <····@l.org> wrote:
> >> On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
> >> > I'm trying to figure out a way to save the result of a function to a
> >> > file. I'm using a code similar to this:
> >>
> >> > (with-open-file (s "/home/hom/foo" :direction :output)
> >> > � (write-sequence (foo) s))
> >>
> >> > The problem is (foo) might return either a character sequence or an
> >> > octet sequence. The code gets an error if (foo) returns a sequence of
> >> > octets. If I add :element-type '(unsigned-byte 8) I get an error message
> >> > when (foo) returns a sequence of characters. Can anyone please suggest a
> >> > way to correct the code so that it works with any kind of sequence? It
> >> > seems to me that it should be trivial. Maybe I'm missing something.
> >>
> >> > I'm using SBCL 1.0.16 if it matters to the problem.
> >>
> >> (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
> >> � (write-sequence "Sto'Vo'Kor" s))
> >
> > This is exactly what I was looking for. Thanks a lot. Incidentally,
>
> I doubt it.
> clhs open:
> element-type---a type specifier for recognizable subtype of
> character; or a type specifier for a finite recognizable subtype
> of integer; por one of the symbols signed-byte, unsigned-byte, or
> :default. The default is character.
The default is normally the elected element-type when no type is
specified. But SBCL has an extension here - bivalent streams, which
are requested in open using :element-type :default.
Use of keyword :default is quite confusing here.
From the SBCL manual:
9.1 Bivalent Streams
A bivalent stream can be used to read and write both character and
(unsigned-byte 8) values. A bivalent stream is created by calling open
with the argument :element-type :default. On such a stream, both
binary and character data can be read and written with the usual input
and output functions.
Streams are not created bivalent by default for performance
reasons. Bivalent streams are incompatible with fast-read-char, an
internal optimization in sbcl's stream machinery that
bulk-converts octets to characters and implements a fast path
through read-char.