Zip of streams/pipes in right way

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Baa
Reply | Threaded
Open this post in threaded view
|

Zip of streams/pipes in right way

Baa
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

--
Reply | Threaded
Open this post in threaded view
|

Re: Zip of streams/pipes in right way

David McBride
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))

On Thu, Jun 8, 2017 at 10:57 AM,  <[hidden email]> wrote:

> 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
>
> --
>
> "Haskell Pipes" group.
>
> email to [hidden email].
>

--



Baa
Reply | Threaded
Open this post in threaded view
|

Re: Zip of streams/pipes in right way

Baa
Thank you David!

Have a nice weekand

четверг, 8 июня 2017 г., 18:07:47 UTC+3 пользователь David M написал:
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))

On Thu, Jun 8, 2017 at 10:57 AM,  <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZwUynYgQCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">aqu...@...> wrote:

> 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
>
> --
>
> "Haskell Pipes" group.
>
> email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZwUynYgQCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">haskell-pipe...@googlegroups.com.
>

--