[Haskell-cafe] foreach

Jeremy Shaw jeremy.shaw at linspireinc.com
Wed Sep 13 17:18:41 EDT 2006


At Wed, 13 Sep 2006 15:24:39 +0400,
Bulat Ziganshin wrote:

> because REAL code is somewhat larger than examples. try to rewrite the
> following:
> 
>   directory_blocks  <-  (`mapM` splitBy (opt_group_dir command) files_to_archive)
>     ( \filesInOneDirectory -> do
>       datablocks  <-  (`mapM` splitToSolidBlocks filesInOneDirectory)
>         ( \filesInOneDataBlock -> do     
>           let compressor = map (freearcLimitDictionary$ clipToMaxInt totalBytes)
>                                (data_compressor filesInOneDataBlock)
>               totalBytes = sum$ map (fiSize.cfFileInfo) filesInOneDataBlock
>               copy_solid_block = isWholeSolidBlock filesInOneDataBlock
>           writeBlock pipe DATA_BLOCK compressor copy_solid_block $ do
>             dir <- if copy_solid_block then do
>                      sendP pipe (CopySolidBlock filesInOneDataBlock)
>                      return$ map fileWithCRC filesInOneDataBlock   
>                    else if (compressor==[aFAKE_COMPRESSION]) then do
>                      sendP pipe (FakeFiles filesInOneDataBlock)
>                      return$ map (FileWithCRC 0 . cfFileInfo) filesInOneDataBlock
>                    else do
>                      mapMaybeM (read_file command bufOps decompress_pipe) filesInOneDataBlock
>             processDir dir   
>             return dir
>         )
>       blocks_info  <-  replicateM (length datablocks) (getP backdoor)
>       arcpos <- archiveGetPos archive
>       
>       writeControlBlock DIR_BLOCK dir_compressor $ do
>         archiveWriteDir blocks_info arcpos receiveBuf sendBuf
>     )

One transformation might be to get rid of the, if..then..else if..
like this:

             do dir <- case () of
                           _ | copy_solid_block -> 
                                do sendP pipe (CopySolidBlock filesInOneDataBlock)
                                   return$ map fileWithCRC filesInOneDataBlock   
                            | (compressor==[aFAKE_COMPRESSION]) ->
                                do sendP pipe (FakeFiles filesInOneDataBlock)
                                   return$ map (FileWithCRC 0 . cfFileInfo) filesInOneDataBlock
                            | otherwise ->
                                mapMaybeM (read_file command bufOps decompress_pipe) filesInOneDataBlock

Not sure if that is actually better or not :)

j.


More information about the Haskell-Cafe mailing list