-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some stuff for chunking... Not sure if we'll use it
1 parent
165667d
commit e8410ee
Showing
3 changed files
with
65 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,58 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
module VaporTrail.Codec.Type | ||
( Codec(..) | ||
, toChunks | ||
, fromChunks | ||
, liftProcChunked | ||
, liftCodecChunked | ||
, lowerProcChunked | ||
, lowerCodecChunked | ||
) where | ||
|
||
import Control.Monad | ||
import Data.Machine | ||
import qualified Data.Vector.Generic as Vector | ||
import Data.Vector.Generic (Vector) | ||
|
||
data Codec a b = Codec | ||
{ codecEnc :: Process a b | ||
, codecDec :: Process b a | ||
} | ||
|
||
toChunks :: Vector v a => Int -> Process a (v a) | ||
toChunks n = | ||
construct . forever $ do | ||
xs <- replicateM n await | ||
yield (Vector.fromListN n xs) | ||
|
||
fromChunks :: Vector v a => Process (v a) a | ||
fromChunks = construct . forever $ do | ||
chunk <- await | ||
Vector.forM_ chunk yield | ||
|
||
-- | Lifts a process to operate on chunks. Less efficient than writing | ||
-- it to work with chunks from the start. The second argument specifies | ||
-- the size of the output chunks | ||
liftProcChunked :: (Vector v0 a, Vector v1 b) => Process a b -> Int -> Process (v0 a) (v1 b) | ||
liftProcChunked p n = toChunks n <~ p <~ fromChunks | ||
|
||
-- | Lifts a codec to operate on chunks. Less efficient than writing | ||
-- it to work with chunks from the start. | ||
liftCodecChunked :: (Vector v0 a, Vector v1 b) => Codec a b -> Int -> Codec (v0 a) (v1 b) | ||
liftCodecChunked codec n = | ||
Codec | ||
{ codecEnc = liftProcChunked (codecEnc codec) n | ||
, codecDec = liftProcChunked (codecDec codec) n | ||
} | ||
|
||
-- | Lowers a process to work on individual elements. The second argument | ||
-- specifies the size of chunks provided to the process | ||
lowerProcChunked :: (Vector v0 a, Vector v1 b) => Process (v0 a) (v1 b) -> Int -> Process a b | ||
lowerProcChunked p n = fromChunks <~ p <~ toChunks n | ||
|
||
lowerCodecChunked :: (Vector v0 a, Vector v1 b) => Codec (v0 a) (v1 b) -> Int -> Codec a b | ||
lowerCodecChunked codec n = | ||
Codec | ||
{ codecEnc = lowerProcChunked (codecEnc codec) n | ||
, codecDec = lowerProcChunked (codecDec codec) n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters