<meta charset="utf-8"><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Hello, beginner here, looking for some ideas on how to simplify how I use the Takusen database library to query a database and return types.<br>
<br>In the working code below, I&#39;m able to query the database and through several functions get a list of simple types. I&#39;ve tried playing around with the query2Iteratee and getAccounts&#39; functions to simplify the code and reduce it to a single function, but keep getting hung up on compiler errors due to type issues.<br>
<br>Is there a better way to write this, or is it best to keep the various functions quite separate? (in my real project, I have the database access in a separate module from the list of custom types and the code to convert to the Account type).<br>
<br>Thanks in advance,<br>Neil<br><br>import Database.Sqlite.Enumerator<br>import Database.Enumerator<br><br>data Account = Account {account_id :: Int}<br>            deriving (Eq, Show)<br><br><br>query2Iteratee :: (Monad m) =&gt; Int -&gt; [Int] -&gt; m (IterResult [Int])<br>
--query2Iteratee :: (Monad m) =&gt; Int -&gt; IterAct m [Int]<br>query2Iteratee a accum = result&#39; (a:accum)<br><br><br>getAccounts&#39; :: IO [Int]<br>getAccounts&#39; = do<br>           let dbh = connect<br>&quot;performance_data.sqlite&quot;<br>
           withSession dbh (do<br>                  r &lt;- doQuery (sql &quot;SELECT account_id FROM<br>ACCOUNT;&quot;) query2Iteratee []<br>                  return r<br>            )<br><br><br>getAccounts :: IO [Account]<br>
getAccounts  = do<br>              accountsList &lt;- getAccounts&#39;<br>              let r = map conv accountsList<br>              return r<br><br>conv :: Int -&gt; Account<br>conv a = Account {account_id=a}<br><br><br>
main = do<br>    accounts &lt;- getAccounts&#39;<br>    putStrLn (show accounts)<br><br></span>