Hi,
I'm not sure if this is possible but I want to partition a stream
based on some condition that depends on the current output
state of the stream. It will make sense with an example I think.
I will create a bunch of orders which I will stream since the
actual use case is a stream of orders coming in so it is not
known up front what the next order will be or even the full list
of orders:
scala> case class Order(item : String, qty : Int, price : Double)
defined class Order
scala> val orders = List(Order("bike", 1, 23.34), Order("book", 3, 2.34), Order("lamp", 1, 9.44), Order("bike", 1, 23.34))
orders: List[Order] = List(Order(bike,1,23.34), Order(book,3,2.34), Order(lamp,1,9.44), Order(bike,1,23.34))
Now I want to partition/group these orders into one set which
contain exactly the same order and another set which contains unique
orders. So in the above example, when I force the stream it should
create two streams: one with the two orders for a bike (since they are the
same)
and another stream containing all the other orders.
I tried the following:
Created the partitioning function:
scala> def matchOrders(o : Order, s : Stream[Order]) = s.contains(o)
matchOrders: (o: Order, s: Stream[Order])Boolean
then tried to apply this to stream:
scala> val s : (Stream[Order], Stream[Order]) = orders.toStream.partition(matchOrders(_, s._1))
I got a null pointer exception since I guess the s._1 is empty initially??
I'm not sure. I've tried other ways but I'm not getting very far. Is there
a
way to achieve this partitioning?
Regards
Iftikhar
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.