1

haskell-pipes を使用したパイプラインを介してフローを誘導するのに問題があります。基本的に、私はたくさんのファイルを分析してから、次のいずれかを行う必要があります

  1. 人間に優しい方法で結果を端末に出力する
  2. 結果を JSON にエンコードする

選択されるパスは、コマンド ライン オプションによって異なります。
2 番目のケースでは、左角かっこを出力し、次にすべての入力値にコンマ、次に右角かっこを出力する必要があります。現在insertCommasは終了しないため、閉じ括弧は出力されません。

import Pipes
import Data.ByteString.Lazy as B
import Data.Aeson (encode)

insertCommas :: Consumer B.ByteString IO ()
insertCommas = do
    first <- await
    lift $ B.putStr first
    for cat $ \obj -> lift $ do
        putStr ","
        B.putStr obj

jsonExporter :: Consumer (FilePath, AnalysisResult) IO ()
jsonExporter = do
    lift $ putStr "["
    P.map encode >-> insertCommas
    lift $ putStr "]"

exportStream :: Config -> Consumer (FilePath, AnalysisResult) IO ()
exportStream conf =
    case outputMode conf of
      JSON -> jsonExporter
      _    -> P.map (export conf) >-> P.stdoutLn

main :: IO ()
main = do
    -- The first two lines are Docopt stuff, not relevant
    args <- parseArgsOrExit patterns =<< getArgs
    ins  <- allFiles $ args `getAllArgs` argument "paths"
    let conf = readConfig args
    runEffect $ each ins
             >-> P.mapM analyze
             >-> P.map (filterResults conf)
             >-> P.filter filterNulls
             >-> exportStream conf
4

2 に答える 2

1

pipes-group で「commify」する必要があると思います。がありますintercalatesが、散在していませんが、書くのは大したことではありません。この種の問題については、消費者側から離れるべきだと思います。

{-#LANGUAGE OverloadedStrings #-}
import Pipes
import qualified Pipes.Prelude as P
import qualified Data.ByteString.Lazy.Char8 as B
import Pipes.Group
import Lens.Simple  -- or Control.Lens or Lens.Micro or anything with view/^.
import System.Environment

intersperse_ :: Monad m => a -> Producer a m r -> Producer a m r
intersperse_ a producer = intercalates (yield a) (producer ^. chunksOf 1) 

main = do 
  args <- getArgs
  let op prod = case args of 
        "json":_ -> yield "[" *> intersperse_ "," prod <* yield "]"
        _        -> intersperse_ " " prod
  runEffect $ op producer >-> P.mapM_ B.putStr
  putStrLn ""
  where 
    producer = mapM_ yield (B.words "this is a test")

私にこれを与える

    >>> :main json
    [this,is,a,test]
    >>> :main ---
    this is a test
于 2015-11-01T19:49:33.880 に答える