1{-# LANGUAGE FlexibleContexts #-}
3module Streamly.Examples.MergeSortedStreams where
6import System.Random (getStdGen, randoms)
9import Streamly.Prelude (once)
10import qualified Streamly.Prelude as A
12getSorted :: Stream Word16
15 let ls = take 100000 (randoms g) :: [Word16]
16 foldMap return (sort ls)
18-- | merge two streams generating the elements from each in parallel
19mergeAsync :: Ord a => Stream a -> Stream a -> Stream a
25merge :: Ord a => Stream a -> Stream a -> Stream a
27 a1 <- once $ A.uncons a
31 b1 <- once $ A.uncons b
33 Nothing -> return x <> ma
36 then (return y) <> merge (return x <> ma) mb
37 else (return x) <> merge ma (return y <> mb)
39mergeSortedStreams :: IO ()
40mergeSortedStreams = do
41 xs <- A.toList $ mergeAsync getSorted getSorted
42 putStrLn $ show $ length xs
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list