<p dir="ltr">If I'm not mistaken, lazy lists and lazy IO alone make this a reasonably efficient implementation; even without further optimizations, the intermediate lists would most likely never reside in memory all at once.</p>
<div class="gmail_extra"><br><div class="gmail_quote">On Aug 11, 2016 9:27 AM, "Oleg Grenrus" <<a href="mailto:oleg.grenrus@iki.fi">oleg.grenrus@iki.fi</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Thanks to the lazy IO, deforestration and fusion framework in `Vector`, <div><br></div><div> readLines h = fromList . lines <$> hGetContents h</div><div><br></div><div>shouldn’t create intermediate lists or/and read the whole file at once into memory.</div><div><br></div><div>- Oleg</div><div><br></div><div><br><div><blockquote type="cite"><div>On 11 Aug 2016, at 10:03, Jake <<a href="mailto:jake.waksbaum@gmail.com" target="_blank">jake.waksbaum@gmail.com</a>> wrote:</div><br><div><p dir="ltr">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">readLines :: Handle -> IO (Vector String)</p><p dir="ltr">I have an implementation that works but seems to naive and inefficient to me because it reads the entire file contents, <i>then </i>splits it into lines, and <i>then</i> converts it to a Vector from a list. </p><p dir="ltr">readLines h = fromList . lines <$> hGetContents h</p><p dir="ltr">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">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">Thanks,<br>
Jake Waksbaum</p>
______________________________<wbr>_________________<br>Haskell-Cafe mailing list<br>To (un)subscribe, modify options or view archives go to:<br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>Only members subscribed via the mailman list are allowed to post.</div></blockquote></div><br></div></div><br>______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div></div>