• Haskell语言学习笔记(75)Conduit


    安装 conduit

    $ cabal install conduit
    Installed conduit-1.3.0.3
    Prelude> import Conduit
    Prelude Conduit> 
    

    Conduit

    Conduit 是一个处理流的库。

    Prelude Conduit> :{
    Prelude Conduit|   print $ runConduitPure
            $ yieldMany [1..10]
           .| mapC (+ 1)
           .| sinkList
    Prelude Conduit| :}
    [2,3,4,5,6,7,8,9,10,11]
    

    应用实例

    {-# LANGUAGE ExtendedDefaultRules #-}
    import Conduit
    
    magic :: Int -> IO Int
    magic x = do
        putStrLn $ "I'm doing magic with " ++ show x
        return $ x * 2
    
    main :: IO ()
    main = do
        putStrLn "List version:"
        mapM magic (take 10 [1..]) >>= mapM_ print . takeWhile (< 18)
        putStrLn ""
        putStrLn "Conduit version:"
        runConduit
              $ yieldMany [1..]
             .| takeC 10
             .| mapMC magic
             .| takeWhileC (< 18)
             .| mapM_C print
    
    List version:
    I'm doing magic with 1
    I'm doing magic with 2
    I'm doing magic with 3
    I'm doing magic with 4
    I'm doing magic with 5
    I'm doing magic with 6
    I'm doing magic with 7
    I'm doing magic with 8
    I'm doing magic with 9
    I'm doing magic with 10
    2
    4
    6
    8
    10
    12
    14
    16
    
    Conduit version:
    I'm doing magic with 1
    2
    I'm doing magic with 2
    4
    I'm doing magic with 3
    6
    I'm doing magic with 4
    8
    I'm doing magic with 5
    10
    I'm doing magic with 6
    12
    I'm doing magic with 7
    14
    I'm doing magic with 8
    16
    I'm doing magic with 9
    
  • 相关阅读:
    Codeforces Round #364
    HDU5727 Necklace
    bzoj4578: [Usaco2016 OPen]Splitting the Field
    Codeforces Round #363 (Div. 1) C. LRU
    BestCoder Round #84
    2014 Multi-University Training Contest 2
    php中 new self 和 new static的区别
    浏览器带着cookie去访问服务器,取出的数据为空
    左边用0填充补齐
    centos7修改密码
  • 原文地址:https://www.cnblogs.com/zwvista/p/9235659.html
Copyright © 2020-2023  润新知