Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a lot of good answers already; I have very little to offer performance-wise but a few style points.</p> <p>First, I would write it this way:</p> <pre><code>import Prelude as P import Data.Iteratee import qualified Data.Iteratee as I import qualified Data.Iteratee.IO as I import qualified Data.ByteString as B import Data.Char import System.Environment -- numLines has a concrete stream type so it's not necessary to provide an -- annotation later. It could have a more general type. numLines :: Monad m =&gt; I.Iteratee B.ByteString m Int numLines = I.foldl' step 0 where --step :: Int -&gt; Word8 -&gt; Int step acc el = if el == (fromIntegral $ ord '\n') then acc + 1 else acc main = do f:_ &lt;- getArgs words &lt;- run =&lt;&lt; I.enumFile 65536 f numLines print words </code></pre> <p>The biggest difference is that this uses <code>Data.Iteratee.ListLike.foldl'</code>. Note that only the individual stream elements matter to the step function, not the stream type. It's exactly the same function as you would use with e.g. <code>Data.ByteString.Lazy.foldl'</code>.</p> <p>Using <code>foldl'</code> also means that you don't need to manually write iteratees with <code>liftI</code>. I would discourage users from doing so unless absolutely necessary. The result is usually longer and harder to maintain with little to no benefit.</p> <p>Finally, I've increased the buffer size significantly. On my system this is marginally faster than <code>enumerator</code>s default of 4096, which is again marginally faster (with iteratee) than your choice of 1024. YMMV with this setting of course.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload