Permalink
Browse files

optimization: Use tail-recursion in stepStmt

  • Loading branch information...
1 parent d0b84fb commit d2b6f6a50e116e1bad28395eac4646c1e6b34c4b @nurpax committed Dec 6, 2012
Showing with 12 additions and 12 deletions.
  1. +12 −12 Database/SQLite/Simple/Internal.hs
@@ -23,6 +23,7 @@ module Database.SQLite.Simple.Internal where
import Prelude hiding (catch)
import Control.Applicative
+import Control.Monad.Fix (fix)
import Control.Exception
import Data.ByteString (ByteString)
import Data.ByteString.Char8()
@@ -65,16 +66,15 @@ exec :: Connection -> T.Text -> IO Result
exec (Connection conn) q =
bracket (Base.prepare conn q) Base.finalize stepStmt
--- Run a query a prepared statement
+
+-- Run a query on a prepared statement
stepStmt :: Base.Statement -> IO Result
-stepStmt = go
- where
- go stmt = do
- res <- Base.step stmt
- case res of
- Base.Row -> do
- cols <- Base.columns stmt
- next <- go stmt
- return $ cols : next
- Base.Done ->
- return []
+stepStmt stmt =
+ flip fix [] $ \loop acc -> do
+ res <- Base.step stmt
+ case res of
+ Base.Done ->
+ return (reverse acc)
+ Base.Row -> do
+ !cols <- Base.columns stmt
+ loop (cols : acc)

0 comments on commit d2b6f6a

Please sign in to comment.