haskell尝试获取带有标题(类型字符串)的int的csv文件以将第2行输出到第n行到列表列表中的新功能。例如[[row,1],[row,2] ..] 到目前为止,使用Pipes库,让它输出每一行作为列表,数字为字符串,并出于某种原因打印“正确”?例如输出:'右[“1”,“3”,“5”..]'新行'右[“32”,“38”,“45”]'。任何建议,将不胜感激。代码:
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
import qualified Data.Vector as V
import Pipes
import qualified Pipes.Prelude as P
import qualified System.IO as IO
import qualified Pipes.ByteString as PB
import qualified Data.Text as Text
import qualified Pipes.Csv as PCsv
import Control.Monad (forever)
showPipe :: Proxy () (Either String (V.Vector Text.Text)) () String IO b
showPipe = forever $ do
x::(Either String (V.Vector Text.Text)) <- await
yield $ show x
main :: IO ()
main = do
putStrLn "Filename? "
fName <- getLine
IO.withFile fName
IO.ReadMode
(\handle -> do
let producer = (PCsv.decode PCsv.NoHeader
(PB.fromHandle handle))
runEffect ( (producer)>->
(showPipe) >->
P.stdoutLn)
)
答案 0 :(得分:1)
我不熟悉Pipe
,但您似乎正在将数据提取到Either
,Right
通常使用Left
构造函数来处理数据和错误消息的data Either a b = Left a | Right b
构造函数。
Either String (V.Vector Text.Text)
在这种情况下,您要构建:: Left String
,因此您的结果将是:
:: Right (V.Vector Text.Text)
或
show
在这种情况下,您获得后者,并希望Either
从either
monad中取出的值。您可以使用maybe
一次处理这两种可能性(与您在Maybe
monad中使用showPipe :: Proxy () (Either String (V.Vector Text.Text)) () String IO b
showPipe = forever $ do
x::(Either String (V.Vector Text.Text)) <- await
let v = either id show x
yield v
的方式大致相同。
x
这会检查Left a
是Right b
还是id a
,并根据show b
或either
返回。 either :: (a -> c) -> (b -> c) -> Either a b -> c
-- or, specialized for this case:
either :: (String -> String)
-> ((V.Vector Text.Text) -> String)
-> Either String (V.Vector Text.Text)
-> String
是类型
String
要么(a hyuk)你应该让main
回来打印create table Data (n int, name char(3), val int)
insert into data values (1, 'ABC',8)
insert into data values (1, 'DEF', 7)
insert into data values (2 , 'ABC' , 9)
insert into data values (2 , 'XYZ', 6)
SELECT COALESCE(a.Name, b.Name) as Name, a.Val as A, b.Val as B
FROM Data a
FULL OUTER JOIN Data b on a.N = b.N - 1 and a.Name = b.Name