fs2 icon indicating copy to clipboard operation
fs2 copied to clipboard

Combining Streams as expressions

Open cjweigle opened this issue 11 years ago • 6 comments

Hello all, Im currently playing around with doing processing on timeseries data with scalaz-stream. I want to break apart a calculation into multiple streams, which means streams need to be combinable in arbitrary expressions? In other words, currently, is there anyway to just write the expression and have the streams composed automatically? I figure there would need to be some gating mechanism in place, if an expression has 4 terms, you need to wait until all 4 terms have a value. Whats a good way to do this?

To my knowledge there isn't a way to make expressions from streams, but I'm interested in implementing it, whether as a feature or a library built on top. I'm relatively new to scala, so I'm still trying to figure out the best way to do things. I just want to hear some others thoughts. Am I doing something wrong? Does this make sense? Does this seem unnecessary?

Thanks

cjweigle avatar Jun 01 '15 13:06 cjweigle

I'm not sure I understand exactly what you're asking. The external API for scalaz-stream is purely functional, which means that when you're working with the library, you are working with expressions which compose just as easily as mathematical expressions do. That seems to be a bit different from what you're asking though. Perhaps some pseudocode would help?

djspiewak avatar Jun 01 '15 15:06 djspiewak

Sorry for the confusion, I wrote that post before bed, and could have been a bit more coherent haha. I've uploaded some code I've been working on the past few nights. I'm trying to implement the ADX indicator for technical analysis in scalaz streams. I've been using wikipedia as a reference: http://en.wikipedia.org/wiki/Average_directional_movement_index

And my code is at https://gist.github.com/chronweigle/fe0bfbaa518743d82317

I have upmove and downmove working, but the next step in the calculation is the +DM: if UpMove > DownMove and UpMove > 0, then +DM = UpMove, else +DM = 0

+DM takes both upmove and downmove, so I need to wait until both streams have a value before I can run the calculation. The +DM calculation has 2 values in it's calculations, but I have to write all the code to wait for those 2 values. In the future if I try to implement other indicators, I'll have to readd the gating code. It will become boilerplate.

Ideally, I want to make the +DM step in the calculation as a one line expression (with any amount of values. not just 2. if the expression has 20 values, it should wait until all 20 inputs have a value). Currently I have a recvUp function and recvDown, but I think it should be possible to write something thats automatic.

I may also be confused and doing everything verbose for no reason. I'm not sure.

cjweigle avatar Jun 01 '15 22:06 cjweigle

also note the pDM on the gist doesn't work yet. it's it's current state, but I have finished it yet

cjweigle avatar Jun 01 '15 22:06 cjweigle

I've attempted to implement a sort of gate, where you pass in multiple streams, and a function is run once a value from every stream is obtained. For example, to construct a Gate, I pass in a Seq[Process[Task, A]] with length of 7 and a function. I want to wait until all 7 streams have a value waiting to be read, and then call a function with all 7 values passed in. The return of the function should be emitted on an output stream. Unfortunately I can't get it to compile. Can anyone give me a direction to go in to get this working?

https://gist.github.com/chronweigle/6dd2becd6b4d686b3f4f

Thanks

cjweigle avatar Jun 05 '15 13:06 cjweigle

ok, sorry if I've been a bit confusing, Im confused myself which only makes things harder. I think part of the problem is I was trying to do too much with the streams, and making different Sinks and Processes to send things to other destinations, when I should think about modify the object being passed around. I was trying to encode information in the Sinks, (for example having 10 sinks for basically the same thing, just a different label) but I should have a single sink and I should store the label in the object.

Even though I'm going to try something new I still have a generic question that could help me understand whats going on. How can I have a process that receives an object, looks at a property of that object, and than forwards it to a Sink based on the property. I wanted to store the Sinks in a map and get the Sink from the map everytime I need to send something to it. I had some difficulty though. I think the confusion may stem from picturing things being pushed through the streams, but I think they are actually pulled, not pushed. (this is based on my understanding of backpressure. not sure) I want a sink that receives an object, then looks up another sink in a hashmap and -pushes- the value into the sink. But, I think this is backwards, because values aren't pushed they are pulled (I think), which means I'm trying to do something impossible. I think I -could- make a map of queues and push things to the queues, but then if one fills up and the next value on the input stream needs to go to the full one there'll be a traffic jam, and its all one big mess. For now, Im going to try and keep the routing simple and put information into the objects being passed around.

cjweigle avatar Jun 06 '15 03:06 cjweigle

Hey, I am going to close this for now. The gitter channel might be a good place to have these sorts of discussions. If a more concrete proposal comes out of that, we can create a new issue to track.

pchiusano avatar Feb 11 '16 16:02 pchiusano