John D. Ramsdell | 27 Feb 21:38 2013
Picon

How does one create an input handle bound to a string instead of a file?

How does one create a value of type System.IO.Handle for reading that
takes its input from a string instead of a file?  I'm looking for the
equivalent of java.io.StringReader in Java.  Thanks in advance.

John
Gregory Collins | 27 Feb 23:50 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

On Wed, Feb 27, 2013 at 9:38 PM, John D. Ramsdell <ramsdell0 <at> gmail.com> wrote:
How does one create a value of type System.IO.Handle for reading that
takes its input from a string instead of a file?  I'm looking for the
equivalent of java.io.StringReader in Java.  Thanks in advance.

You can't. There are several libraries that purport to provide better interfaces for doing IO in Haskell, like conduit, pipes, enumerator, and my own io-streams library (http://github.com/snapframework/io-streams, soon to be released). You could try one of those.

G
--
Gregory Collins <greg <at> gregorycollins.net>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Don Stewart | 28 Feb 00:15 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

I don't think that's right - Simon's buffer class rewrite should have made this possible, I think.

http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/GHC-IO-BufferedIO.html

On Feb 27, 2013 10:52 PM, "Gregory Collins" <greg <at> gregorycollins.net> wrote:
On Wed, Feb 27, 2013 at 9:38 PM, John D. Ramsdell <ramsdell0 <at> gmail.com> wrote:
How does one create a value of type System.IO.Handle for reading that
takes its input from a string instead of a file?  I'm looking for the
equivalent of java.io.StringReader in Java.  Thanks in advance.

You can't. There are several libraries that purport to provide better interfaces for doing IO in Haskell, like conduit, pipes, enumerator, and my own io-streams library (http://github.com/snapframework/io-streams, soon to be released). You could try one of those.

G
--
Gregory Collins <greg <at> gregorycollins.net>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Gregory Collins | 28 Feb 00:23 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

Hm, perhaps I stand corrected. Then how exactly do you make the bytestring Handle?


On Thu, Feb 28, 2013 at 12:15 AM, Don Stewart <dons00 <at> gmail.com> wrote:

I don't think that's right - Simon's buffer class rewrite should have made this possible, I think.

http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/GHC-IO-BufferedIO.html

On Feb 27, 2013 10:52 PM, "Gregory Collins" <greg <at> gregorycollins.net> wrote:
On Wed, Feb 27, 2013 at 9:38 PM, John D. Ramsdell <ramsdell0 <at> gmail.com> wrote:
How does one create a value of type System.IO.Handle for reading that
takes its input from a string instead of a file?  I'm looking for the
equivalent of java.io.StringReader in Java.  Thanks in advance.

You can't. There are several libraries that purport to provide better interfaces for doing IO in Haskell, like conduit, pipes, enumerator, and my own io-streams library (http://github.com/snapframework/io-streams, soon to be released). You could try one of those.

G
--
Gregory Collins <greg <at> gregorycollins.net>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
Gregory Collins <greg <at> gregorycollins.net>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Bob Ippolito | 28 Feb 04:05 2013

Re: How does one create an input handle bound to a string instead of a file?

I haven't had time to make an example yet but it looks like if you go down to GHC.IO.Handle.Internals there's a mkHandle function that takes a BufferedIO and some other stuff and gives you an IO Handle.


On Wed, Feb 27, 2013 at 3:23 PM, Gregory Collins <greg <at> gregorycollins.net> wrote:
Hm, perhaps I stand corrected. Then how exactly do you make the bytestring Handle?


On Thu, Feb 28, 2013 at 12:15 AM, Don Stewart <dons00 <at> gmail.com> wrote:

I don't think that's right - Simon's buffer class rewrite should have made this possible, I think.

http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/GHC-IO-BufferedIO.html

On Feb 27, 2013 10:52 PM, "Gregory Collins" <greg <at> gregorycollins.net> wrote:
On Wed, Feb 27, 2013 at 9:38 PM, John D. Ramsdell <ramsdell0 <at> gmail.com> wrote:
How does one create a value of type System.IO.Handle for reading that
takes its input from a string instead of a file?  I'm looking for the
equivalent of java.io.StringReader in Java.  Thanks in advance.

You can't. There are several libraries that purport to provide better interfaces for doing IO in Haskell, like conduit, pipes, enumerator, and my own io-streams library (http://github.com/snapframework/io-streams, soon to be released). You could try one of those.

G
--
Gregory Collins <greg <at> gregorycollins.net>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
Gregory Collins <greg <at> gregorycollins.net>

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
John D. Ramsdell | 28 Feb 14:42 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

I couldn't find the mkHandle function in the source linked to the
specified Haddock generated documentation page.

If there is a consensus that others besides myself would like a
function with the signature

stringHandle :: String -> IO (Handle)

I'd be happy to contribute code.  I'd need help as I haven't ever
contributed this kind of code.

John

On Wed, Feb 27, 2013 at 10:05 PM, Bob Ippolito <bob <at> redivi.com> wrote:
> I haven't had time to make an example yet but it looks like if you go down
> to GHC.IO.Handle.Internals there's a mkHandle function that takes a
> BufferedIO and some other stuff and gives you an IO Handle.
>
>
> On Wed, Feb 27, 2013 at 3:23 PM, Gregory Collins <greg <at> gregorycollins.net>
> wrote:
>>
>> Hm, perhaps I stand corrected. Then how exactly do you make the bytestring
>> Handle?
>>
>>
>> On Thu, Feb 28, 2013 at 12:15 AM, Don Stewart <dons00 <at> gmail.com> wrote:
>>>
>>> I don't think that's right - Simon's buffer class rewrite should have
>>> made this possible, I think.
>>>
>>>
>>> http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/GHC-IO-BufferedIO.html
>>>
>>> On Feb 27, 2013 10:52 PM, "Gregory Collins" <greg <at> gregorycollins.net>
>>> wrote:
>>>>
>>>> On Wed, Feb 27, 2013 at 9:38 PM, John D. Ramsdell <ramsdell0 <at> gmail.com>
>>>> wrote:
>>>>>
>>>>> How does one create a value of type System.IO.Handle for reading that
>>>>> takes its input from a string instead of a file?  I'm looking for the
>>>>> equivalent of java.io.StringReader in Java.  Thanks in advance.
>>>>
>>>>
>>>> You can't. There are several libraries that purport to provide better
>>>> interfaces for doing IO in Haskell, like conduit, pipes, enumerator, and my
>>>> own io-streams library (http://github.com/snapframework/io-streams, soon to
>>>> be released). You could try one of those.
>>>>
>>>> G
>>>> --
>>>> Gregory Collins <greg <at> gregorycollins.net>
>>>>
>>>> _______________________________________________
>>>> Haskell-Cafe mailing list
>>>> Haskell-Cafe <at> haskell.org
>>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>>
>>
>>
>>
>> --
>> Gregory Collins <greg <at> gregorycollins.net>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe <at> haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
Ganesh Sittampalam | 28 Feb 09:02 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

Hi,

On 27/02/2013 20:38, John D. Ramsdell wrote:
> How does one create a value of type System.IO.Handle for reading that
> takes its input from a string instead of a file?  I'm looking for the
> equivalent of java.io.StringReader in Java.  Thanks in advance.

http://hackage.haskell.org/package/bytestring-handle can make handles
that read and write to ByteStrings.

Cheers,

Ganesh
John D. Ramsdell | 28 Feb 14:32 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

I think I wasn't clear about my question.  I want something that
creates a value of type System.IO.Handle.  You see, I have a high
performance S-expression parser that I'd like to use in GHCi reading
strings while at the command loop.

Here is more details on my module SExpr that exports the SExpr data
type and the load function.  The desired function is called
stringHandle.

-- An S-expression
data SExpr
    = S String                 -- A symbol
    | Q String                 -- A quoted string
    | N Int                    -- An integer
    | L [SExpr a]              -- A proper list

-- Read one S-expression or return Nothing on EOF
load :: Handle -> IO (Maybe (SExpr Pos))

In GHCi, I want to type something like:

SExpr> let h = stringHandle "()"
SExpr> load h
Just (L [])
SExpr> load h
Nothing
SExpr>

It seems to me right now that I have to implement a duplicate parser
that implements Read.  At least S-expression parsing is easy.

John

On Thu, Feb 28, 2013 at 3:02 AM, Ganesh Sittampalam <ganesh <at> earth.li> wrote:
> Hi,
>
> On 27/02/2013 20:38, John D. Ramsdell wrote:
>> How does one create a value of type System.IO.Handle for reading that
>> takes its input from a string instead of a file?  I'm looking for the
>> equivalent of java.io.StringReader in Java.  Thanks in advance.
>
> http://hackage.haskell.org/package/bytestring-handle can make handles
> that read and write to ByteStrings.
>
> Cheers,
>
> Ganesh
>
>
Ganesh Sittampalam | 28 Feb 14:40 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

Hi John,

Using bytestring-handle, you can get this with something like

stringHandle :: String -> Handle
stringHandle s = readHandle False (Data.ByteString.Char8.pack s)

[note the complete disregard of encoding issues in the use of
Data.ByteString.Char8]

Cheers,

Ganesh

On 28/02/2013 13:32, John D. Ramsdell wrote:
> I think I wasn't clear about my question.  I want something that
> creates a value of type System.IO.Handle.  You see, I have a high
> performance S-expression parser that I'd like to use in GHCi reading
> strings while at the command loop.
> 
> Here is more details on my module SExpr that exports the SExpr data
> type and the load function.  The desired function is called
> stringHandle.
> 
> -- An S-expression
> data SExpr
>     = S String                 -- A symbol
>     | Q String                 -- A quoted string
>     | N Int                    -- An integer
>     | L [SExpr a]              -- A proper list
> 
> -- Read one S-expression or return Nothing on EOF
> load :: Handle -> IO (Maybe (SExpr Pos))
> 
> In GHCi, I want to type something like:
> 
> SExpr> let h = stringHandle "()"
> SExpr> load h
> Just (L [])
> SExpr> load h
> Nothing
> SExpr>
> 
> It seems to me right now that I have to implement a duplicate parser
> that implements Read.  At least S-expression parsing is easy.
> 
> John
> 
> On Thu, Feb 28, 2013 at 3:02 AM, Ganesh Sittampalam <ganesh <at> earth.li> wrote:
>> Hi,
>>
>> On 27/02/2013 20:38, John D. Ramsdell wrote:
>>> How does one create a value of type System.IO.Handle for reading that
>>> takes its input from a string instead of a file?  I'm looking for the
>>> equivalent of java.io.StringReader in Java.  Thanks in advance.
>>
>> http://hackage.haskell.org/package/bytestring-handle can make handles
>> that read and write to ByteStrings.
>>
>> Cheers,
>>
>> Ganesh
>>
>>
> 
John D. Ramsdell | 28 Feb 14:49 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

I see now.  I read the source code incorrectly.  Now I know what to do.

John

On Thu, Feb 28, 2013 at 8:40 AM, Ganesh Sittampalam <ganesh <at> earth.li> wrote:
> Hi John,
>
> Using bytestring-handle, you can get this with something like
>
> stringHandle :: String -> Handle
> stringHandle s = readHandle False (Data.ByteString.Char8.pack s)
>
> [note the complete disregard of encoding issues in the use of
> Data.ByteString.Char8]
>
> Cheers,
>
> Ganesh
>
> On 28/02/2013 13:32, John D. Ramsdell wrote:
>> I think I wasn't clear about my question.  I want something that
>> creates a value of type System.IO.Handle.  You see, I have a high
>> performance S-expression parser that I'd like to use in GHCi reading
>> strings while at the command loop.
>>
>> Here is more details on my module SExpr that exports the SExpr data
>> type and the load function.  The desired function is called
>> stringHandle.
>>
>> -- An S-expression
>> data SExpr
>>     = S String                 -- A symbol
>>     | Q String                 -- A quoted string
>>     | N Int                    -- An integer
>>     | L [SExpr a]              -- A proper list
>>
>> -- Read one S-expression or return Nothing on EOF
>> load :: Handle -> IO (Maybe (SExpr Pos))
>>
>> In GHCi, I want to type something like:
>>
>> SExpr> let h = stringHandle "()"
>> SExpr> load h
>> Just (L [])
>> SExpr> load h
>> Nothing
>> SExpr>
>>
>> It seems to me right now that I have to implement a duplicate parser
>> that implements Read.  At least S-expression parsing is easy.
>>
>> John
>>
>> On Thu, Feb 28, 2013 at 3:02 AM, Ganesh Sittampalam <ganesh <at> earth.li> wrote:
>>> Hi,
>>>
>>> On 27/02/2013 20:38, John D. Ramsdell wrote:
>>>> How does one create a value of type System.IO.Handle for reading that
>>>> takes its input from a string instead of a file?  I'm looking for the
>>>> equivalent of java.io.StringReader in Java.  Thanks in advance.
>>>
>>> http://hackage.haskell.org/package/bytestring-handle can make handles
>>> that read and write to ByteStrings.
>>>
>>> Cheers,
>>>
>>> Ganesh
>>>
>>>
>>
>
Erik Hesselink | 28 Feb 15:00 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

Is your parser impure? I would expect a function from
String/Text/ByteString to Maybe (SExpr Pos).
 Then you have no need for a Handle.

Regards,

Erik

On Thu, Feb 28, 2013 at 2:32 PM, John D. Ramsdell <ramsdell0 <at> gmail.com> wrote:
> I think I wasn't clear about my question.  I want something that
> creates a value of type System.IO.Handle.  You see, I have a high
> performance S-expression parser that I'd like to use in GHCi reading
> strings while at the command loop.
>
> Here is more details on my module SExpr that exports the SExpr data
> type and the load function.  The desired function is called
> stringHandle.
>
> -- An S-expression
> data SExpr
>     = S String                 -- A symbol
>     | Q String                 -- A quoted string
>     | N Int                    -- An integer
>     | L [SExpr a]              -- A proper list
>
> -- Read one S-expression or return Nothing on EOF
> load :: Handle -> IO (Maybe (SExpr Pos))
>
> In GHCi, I want to type something like:
>
> SExpr> let h = stringHandle "()"
> SExpr> load h
> Just (L [])
> SExpr> load h
> Nothing
> SExpr>
>
> It seems to me right now that I have to implement a duplicate parser
> that implements Read.  At least S-expression parsing is easy.
>
> John
>
> On Thu, Feb 28, 2013 at 3:02 AM, Ganesh Sittampalam <ganesh <at> earth.li> wrote:
>> Hi,
>>
>> On 27/02/2013 20:38, John D. Ramsdell wrote:
>>> How does one create a value of type System.IO.Handle for reading that
>>> takes its input from a string instead of a file?  I'm looking for the
>>> equivalent of java.io.StringReader in Java.  Thanks in advance.
>>
>> http://hackage.haskell.org/package/bytestring-handle can make handles
>> that read and write to ByteStrings.
>>
>> Cheers,
>>
>> Ganesh
>>
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
John D. Ramsdell | 28 Feb 16:10 2013
Picon

Re: How does one create an input handle bound to a string instead of a file?

The actual parser is a bit more complicated than I let on.  First,
it's important that not all of a file be read at the same time as the
files can be huge.  Second, it keeps track of column row position
information as an IORef, which makes sense because the ref is bundled
in a structure with the handle and manipulated only in the IO Monad in
conjunction with operations on the handle.

John

On Thu, Feb 28, 2013 at 9:00 AM, Erik Hesselink <hesselink <at> gmail.com> wrote:
> Is your parser impure? I would expect a function from
> String/Text/ByteString to Maybe (SExpr Pos).
>  Then you have no need for a Handle.
>
> Regards,
>
> Erik
>
> On Thu, Feb 28, 2013 at 2:32 PM, John D. Ramsdell <ramsdell0 <at> gmail.com> wrote:
>> I think I wasn't clear about my question.  I want something that
>> creates a value of type System.IO.Handle.  You see, I have a high
>> performance S-expression parser that I'd like to use in GHCi reading
>> strings while at the command loop.
>>
>> Here is more details on my module SExpr that exports the SExpr data
>> type and the load function.  The desired function is called
>> stringHandle.
>>
>> -- An S-expression
>> data SExpr
>>     = S String                 -- A symbol
>>     | Q String                 -- A quoted string
>>     | N Int                    -- An integer
>>     | L [SExpr a]              -- A proper list
>>
>> -- Read one S-expression or return Nothing on EOF
>> load :: Handle -> IO (Maybe (SExpr Pos))
>>
>> In GHCi, I want to type something like:
>>
>> SExpr> let h = stringHandle "()"
>> SExpr> load h
>> Just (L [])
>> SExpr> load h
>> Nothing
>> SExpr>
>>
>> It seems to me right now that I have to implement a duplicate parser
>> that implements Read.  At least S-expression parsing is easy.
>>
>> John
>>
>> On Thu, Feb 28, 2013 at 3:02 AM, Ganesh Sittampalam <ganesh <at> earth.li> wrote:
>>> Hi,
>>>
>>> On 27/02/2013 20:38, John D. Ramsdell wrote:
>>>> How does one create a value of type System.IO.Handle for reading that
>>>> takes its input from a string instead of a file?  I'm looking for the
>>>> equivalent of java.io.StringReader in Java.  Thanks in advance.
>>>
>>> http://hackage.haskell.org/package/bytestring-handle can make handles
>>> that read and write to ByteStrings.
>>>
>>> Cheers,
>>>
>>> Ganesh
>>>
>>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe <at> haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane