How to separate the stream `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `

Imagine I have a very long file:

1
1
1
1
2
2
2
2
1
1
1
1
2
2
1
1
2
2
...

I am interested in dividing and performing the calculation in each group of consecutive 1along with the consecutive 2that appears immediately after it.

. -, 1 2, , , Record s, , , Record 1 2; -, , , , get1 :: IO (Maybe Record). Just record Nothing, ; , , ( ), .

, , : (1 2 - Record, get1, (,) , )

1
1
1
2
2
1
(["1","1","1"],["2","2"])
2
1
(["1"],["2"])
1
1
1
2
2
2
2
1
(["1","1","1","1"],["2","2","2","2"])
+1
2

, , . , - , , .

-, . pipes-group, ​​ . ( pipes-bytestring pipes-text.)

pipe-group , FreeT. FreeT " " .

import Control.Lens
import Pipes
import Pipes.Group
import qualified Pipes.Prelude as P

main = runEffect $ (concats . view groups) P.stdinLn >-> P.stdoutLn

( (==)), , . , , intercalates:

import Control.Lens
import Pipes
import Pipes.Group
import qualified Pipes.Prelude as P

main = runEffect $
         (intercalates (yield "!") . view groups) P.stdinLn >-> P.stdoutLn

"!" , , , , . , foldl:

import Control.Lens
import Pipes
import Pipes.Group
import qualified Pipes.Prelude as P

main = runEffect $
         (folds (++) [] id . view groups) P.stdinLn >-> P.stdoutLn

, stdin , , , , .

. foldl .

+4

-, - , , , , , , . . , , , , , , , .

, stdin , ? Haskell:

import Data.List
import Data.Function

main :: IO ()
main = do
  do input <- getContents
     print $ map computation (groupBy ((==) `on` grouper) (lines input))

grouper = id

computation = ("I see a block of stuff of length: " ++) . show . length

/:

1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
1
["I see a block of stuff of length: 6","I see a block of stuff of length: 9","I see a block of stuff of length: 1"]
+1

Source: https://habr.com/ru/post/1612094/


All Articles