1

I have this:

import Database.HDBC.Sqlite3
import Database.HDBC

For converting fetched rows from a database I use this:

convertFromSql :: [SqlValue] -> [String]
convertFromSql [name, address, number, postal, city, country] = 
  [cName, cAddress, cNumber, cPostal, cCity, cCountry] where 
      cName     = (fromSql name) 
      cAddress  = (fromSql address) :: String 
      cNumber   = (fromSql number) :: String 
      cPostal   = (fromSql postal) :: String 
      cCity     = (fromSql city) :: String 
      cCountry  = (fromSql country) :: String 

All works, but can I for example map the fromSql over the list of entries fetched from sql? And maybe a longshot: zip the mapped list with a list of types, so that code like above is more terse?

Madderote
  • 1,107
  • 10
  • 19
  • 5
    As simple as `convertFromSql = map fromSql`. – arrowd Apr 09 '19 at 19:20
  • Although `fromSql :: Convertible SqlValue a => SqlValue -> a`, the fact that you have explicitly typed `convertFromSql` as `[SqlValue] -> [String]` means that `fromSql` will be specialized to `SqlValue -> String` when you apply it to the input list. – chepner Apr 09 '19 at 20:04
  • @Chepner: true, my bad considering (in this case) it is all `String`s – Madderote Apr 10 '19 at 09:23
  • @arrowd: does not work, since the fetched rows are presented as a list of lists. Maybe with a double mapping. Will try. – Madderote Apr 10 '19 at 09:37
  • @Madderote Yes, `map convertFromSql :: [[SqlValue]] -> [[String]]`. – chepner Apr 10 '19 at 11:43
  • @Chepner, yes, sorry. Thanks for adding that. Case closed :) – Madderote Apr 15 '19 at 10:27

0 Answers0