[Haskell-cafe] Fast number parsing with strict bytestrings [Was:
Re: Seemingly subtle change causes large performance variation]
Conal Elliott
conal at conal.net
Fri Jun 8 13:47:16 EDT 2007
Lovely!
Perhaps a stylistic shift would encourage writing this sort of elegant,
fusion-friendly code.
-- Generalized version of "interact". Encapsulates data getter &
putter.
genInteract :: IO i -> (o -> IO ()) -> ((i -> o) -> IO ())
genInteract get put = \ f -> get >>= put . f
The intention here is that i and o are pure value types (no IO). Solutions
2 and three use the following specialization.
-- Lazy ByteString in and showable out
lGetPrint :: Show o => (L.ByteString -> o) -> IO ()
lGetPrint = genInteract L.getContents print
Then rewrite solution 2 as follows:
main = lGetPrint f
where
f contents = foldl' (test k) 0 . map int . take n $ ls
where
(l:ls) = L.lines contents
[n,k] = map int (L.split ' ' l)
And solution 3 similarly.
Plug: for additional examples and a more general approach to separating out
IO, my blog post "separating IO from logic -- example
<http://conal-elliott.blogspot.com/2007/02/separating-io-from-logic-example.html>",
which uses the TV <http://www.haskell.org/haskellwiki/TV> library.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070608/3de70980/attachment.htm
More information about the Haskell-Cafe
mailing list