Zip of streams/pipes in right way

23 views
Skip to first unread message

aqu...@gmail.com

unread,
Jun 8, 2017, 10:57:47 AM6/8/17
to Haskell Pipes
Hello, Group!

I implemented a scheme (strN - stream N, a, b - items of stream):

   a -> b -> (a, b)
   a
-> b -> (a, b)
   a -> b -> (a, b)
   
.    .     ...
 str1  str2   str3

i.e. from each item of str1 I get item of str2 (with network call) and then combine item from str1 and item from str2 to a pair - to analize combined item (because I need info from both items: a and b).

In pseudocode:

for a in str1Getter:
  b
= getStr2 a
  analize
(a, b)


I done combining with S.zip from Streaming.Prelude. I suppose it's similar to Pipes and Conduits. And this looks like:

(|>) = flip $
str1Getter
|> S.mapM (liftIO . getStr2 connection) |> S.zip str1Getter

and sure here I got twice execution of str2 retrieving from network (each request happens twice), due this zip.

Would somebody help me: how to make this scheme (in simple way)? I can pass a to getStr2 sure and to get from it not simple b but (a, b) but this looks not cool. Is some other way to do it?


/Best regards, Paul

David McBride

unread,
Jun 8, 2017, 11:07:47 AM6/8/17
to haskel...@googlegroups.com
You would just change it like so.

S.mapM (\conn -> liftIO (getStr2 connection) >>= \b -> return (conn,b))

If you want to make it more brief, perhaps

{-# LANGUAGE TupleSections #-}

S.mapM(\conn = (conn,) <$> liftIO (getStr2 conn))
> --
> You received this message because you are subscribed to the Google Groups
> "Haskell Pipes" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to haskell-pipe...@googlegroups.com.
> To post to this group, send email to haskel...@googlegroups.com.

aqu...@gmail.com

unread,
Jun 9, 2017, 12:06:06 PM6/9/17
to Haskell Pipes
Thank you David!

Have a nice weekand

четверг, 8 июня 2017 г., 18:07:47 UTC+3 пользователь David M написал:
Reply all
Reply to author
Forward
0 new messages