Bis*_*943 -1 maps haskell type-conversion
有人将如何将的地图转换ByteString:ByteString
为地图String:String
?我尝试了以下方法:
import qualified Data.Map as Map
import Data.ByteString.UTF8 as BSU
type Key = ByteString
type Valye = ByteString
type DB = Map.Map Key Valye
printMap :: IO ()
printMap = do
-- db exists to this point and is of type DB
mapM_ putStrLn $ Map.map BSU.toString db
Run Code Online (Sandbox Code Playgroud)
这将导致可打印的值,但仅打印我的值而不打印我的键...我知道也Map.mapKeys
可以给我键,但是我如何同时获得它们和打印它们呢?
k0: v0
k1: v1
k2: v2
Run Code Online (Sandbox Code Playgroud)
Map
本身不是函子,仅Map k
对于某些键类型k
,这就是为什么mapM_
仅对您的值进行运算的原因。而是使用由返回的键/值对的列表Map.toList
。
mapM_ (\(k, v) -> ...) (Map.toList db)
Run Code Online (Sandbox Code Playgroud)