<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Thanks to the lazy IO, deforestration and fusion framework in `Vector`, <div class=""><br class=""></div><div class=""> readLines h = fromList . lines <$> hGetContents h</div><div class=""><br class=""></div><div class="">shouldn’t create intermediate lists or/and read the whole file at once into memory.</div><div class=""><br class=""></div><div class="">- Oleg</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 11 Aug 2016, at 10:03, Jake <<a href="mailto:jake.waksbaum@gmail.com" class="">jake.waksbaum@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><p dir="ltr" class="">I want to write a function that will read the contents of a file and return a Vector of its lines:</p><p dir="ltr" class="">readLines :: Handle -> IO (Vector String)</p><p dir="ltr" class="">I have an implementation that works but seems to naive and inefficient to me because it reads the entire file contents, <i class="">then </i>splits it into lines, and <i class="">then</i> converts it to a Vector from a list. </p><p dir="ltr" class="">readLines h = fromList . lines <$> hGetContents h</p><p dir="ltr" class="">I would like to a) use hGetLine and continue until I get an isEOFError and b) read directly into a Vector instead of a list. I started something using Data.Vector.unfoldr but I the presence of the IO monad around my String was causing issues I couldn't figure out how to solve. </p><p dir="ltr" class="">I'd also be curious to see how to do either one of these things separately and I assume they'd each help make my program more efficient on their own. Or, maybe my implementation is not as terrible as I thought because some laziness/fusion/optimization magic is happening behind the scenes? </p><p dir="ltr" class="">Thanks,<br class="">
Jake Waksbaum</p>
_______________________________________________<br class="">Haskell-Cafe mailing list<br class="">To (un)subscribe, modify options or view archives go to:<br class=""><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br class="">Only members subscribed via the mailman list are allowed to post.</div></blockquote></div><br class=""></div></body></html>