如何将Map ByteString ByteString转换为Map String String并打印

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)

che*_*ner 6

Map本身不是函子,仅Map k对于某些键类型k,这就是为什么mapM_仅对您的值进行运算的原因。而是使用由返回的键/值对的列表Map.toList

mapM_ (\(k, v) -> ...) (Map.toList db)
Run Code Online (Sandbox Code Playgroud)