Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most standard/generic way to zip a traversable with a list?

Traversable is in a sense the class of containers whose structure has a “path” (that can correspond to a list), the elements on which can be modified without dissolving the structure. Hence

zipTrav :: Traversable t => t a -> [b] -> Maybe (t (a,b))
zipTrav = evalStateT . traverse zp
 where zp a = do
           bs <- get
           case bs of
              [] -> lift Nothing
              (b:bs') -> put bs' >> return (a,b)

However, that list-state traversal seems a bit hackish and likely not the most efficient way to do it. I'd suppose there would be a standard function that accomplished the above or a more general task, but I can't figure out what it would be.

like image 907
leftaroundabout Avatar asked Jan 07 '17 14:01

leftaroundabout


People also ask

How to distinguish the quality of zipper products?

One is the overall quality of zipper products, such as the qualified rate of the whole batch of products or the stability of zipper quality in a quite long period of time. The other refers to the specific zipper quality, such as the physical properties and technical parameters of zippers.

What is the use of Zipzip?

zip is used to compress the files to reduce file size and also used as file package utility. zip is available in many operating systems like unix, linux, windows etc. If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.

How many indexes are there in a zipper?

In Chinese standards, there are 12 indexes for metal zippers and plastic zippers, and 10 indexes for nylon zippers because nylon zipper teeth are continuous. 4 indexes for slider: slider and puller pull-off strength, resistance to twist of puller and slider, slider deflection strength, holding strength of slider lock

What is ZIP command in Linux with example?

ZIP command in Linux with examples. ZIP is a compression and file packaging utility for Unix. Each file is stored in single .zip {.zip-filename} file with the extension .zip. zip is used to compress the files to reduce file size and also used as file package utility.


1 Answers

What about mapAccumL/mapAccumR?

tzipWith :: Traversable t => (a -> b -> c) -> [a] -> t b -> Maybe (t c)
tzipWith f xs = sequenceA . snd . mapAccumL pair xs
    where pair [] y = ([], Nothing)
          pair (x:xs) y = (xs, Just (f x y))

tzip :: Traversable t => [a] -> t b -> Maybe (t (a, b))
tzip = tzipWith (,)

ghci> tzip [1..] [4,5,6]
Just [(1,4),(2,5),(3,6)]

ghci> tzip [1,2] [4,5,6]
Nothing

On the question of efficiency - under the hood the mapAccum functions use the state monad, so all I've really done is capture the imperative part of your code in a higher-order function. I wouldn't expect this code to perform better than yours. But I don't think you can do much better than the State monad (or ST), given only Traversable t.

like image 61
Benjamin Hodgson Avatar answered Sep 29 '22 06:09

Benjamin Hodgson