[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36
John Goerzen
jgoerzen at complete.org
Fri Apr 23 14:42:32 UTC 2010
The following commit has been merged in the master branch:
commit b7c41932cb41b736c33f62e3b38a25ebcf0e2ef6
Author: John Goerzen <jgoerzen at complete.org>
Date: Tue Oct 5 21:49:41 2004 +0100
Added some awesome I/O utilities
Keywords:
(jgoerzen at complete.org--projects/missingh--head--1.0--patch-15)
diff --git a/MissingH/IOutil.hs b/MissingH/IOutil.hs
new file mode 100644
index 0000000..04bb247
--- /dev/null
+++ b/MissingH/IOutil.hs
@@ -0,0 +1,115 @@
+{- arch-tag: I/O utilities main file
+Copyright (C) 2004 John Goerzen <jgoerzen at complete.org>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+-}
+
+{- | This module provides various helpful utilities for dealing with I\/O.
+
+Written by John Goerzen, jgoerzen\@complete.org
+-}
+
+module MissingH.IOutil(-- * Line Processing Utilities
+ hPutStrLns, hGetLines,
+ -- * Lazy Interaction
+ hInteract, lineInteract, hLineInteract
+ ) where
+import IO
+import System.IO.Unsafe
+
+
+{- | Given a list of strings, output a line containing each item, adding
+newlines as appropriate. The list is not expected to have newlines already.
+-}
+
+hPutStrLns :: Handle -> [String] -> IO ()
+hPutStrLns _ [] = return ()
+hPutStrLns h (x:xs) = do
+ hPutStrLn h x
+ hPutStrLns h xs
+
+{- | Given a handle, returns a list of all the lines in that handle.
+Thanks to lazy evaluation, this list does not have to be read all at once.
+
+Combined with 'hPutStrLns', this can make a powerful way to develop
+filters. See the 'lineInteract' function for more on that concept.
+
+Example:
+
+> main = do
+> l <- hGetLines stdin
+> hPutStrLns stdout $ filter (startswith "1") l
+
+-}
+
+hGetLines :: Handle -> IO [String]
+
+hGetLines h = unsafeInterleaveIO (do
+ ieof <- hIsEOF h
+ if (ieof)
+ then return []
+ else do
+ line <- hGetLine h
+ remainder <- hGetLines h
+ return (line : remainder)
+ )
+
+
+{- | This is similar to the built-in 'System.IO.interact', but works
+on any handle, not just stdin and stdout.
+
+In other words:
+
+> interact = hInteract stdin stdout
+-}
+hInteract :: Handle -> Handle -> (String -> String) -> IO ()
+hInteract finput foutput func = do
+ content <- hGetContents finput
+ hPutStr stdout (func content)
+
+{- | Line-based interaction. This is similar to wrapping your
+interact functions with 'lines' and 'unlines'. This equality holds:
+
+> lineInteract = 'hLineInteract' stdin stdout
+
+Here's an example:
+
+> main = lineInteract (filter (startswith "1"))
+-}
+lineInteract :: ([String] -> [String]) -> IO ()
+lineInteract = hLineInteract stdin stdout
+
+{- | Line-based interaction over arbitrary handles. This is similar
+to wrapping hInteract with 'lines' and 'unlines'.
+
+One could view this function like this:
+
+> hLineInteract finput foutput func =
+> let newf = unlines . func . lines in
+> hInteract finput foutput newf
+
+Though the actual implementation is this for efficiency:
+
+> hLineInteract finput foutput func =
+> do
+> lines <- hGetLines finput
+> hPutStrLns foutput (func lines)
+-}
+
+hLineInteract :: Handle -> Handle -> ([String] -> [String]) -> IO ()
+hLineInteract finput foutput func =
+ do
+ lines <- hGetLines finput
+ hPutStrLns foutput (func lines)
diff --git a/MissingH/Listutil.hs b/MissingH/Listutil.hs
index 4dae581..b696115 100644
--- a/MissingH/Listutil.hs
+++ b/MissingH/Listutil.hs
@@ -33,8 +33,6 @@ module MissingH.Listutil(-- * Tests
split, join, trunc,
-- -- * Sub-List Selection
-- sub,
- -- * Processing utilities
- hPutStrLns, hGetLines
) where
import Data.List(intersperse, concat, isPrefixOf, isSuffixOf)
import IO
@@ -122,33 +120,3 @@ delFromAL l key = filter (\a -> (fst a) /= key) l
{- FIXME TODO: sub -}
-{- Given a list of strings, output a line containing each item, adding
-newlines as appropriate. The list is not expected to have newlines already.
--}
-
-hPutStrLns :: Handle -> [String] -> IO ()
-hPutStrLns _ [] = return ()
-hPutStrLns h (x:xs) = do
- hPutStrLn h x
- hPutStrLns h xs
-
-{- Given a handle, returns a list of all the lines in that handle.
-Thanks to lazy evaluation, this list does not have to be read all at once.
-
-Combined with 'hPutStrLns', this can make a powerful way to develop
-filters.
--}
-
-hGetLines :: Handle -> IO [String]
-
-hGetLines h = unsafeInterleaveIO (do
- ieof <- hIsEOF h
- if (ieof)
- then return []
- else do
- line <- hGetLine h
- remainder <- hGetLines h
- return (line : remainder)
- )
-
-
--
haskell-testpack
More information about the Pkg-haskell-commits
mailing list