These are chat archives for ReactiveX/RxJava

1st
Nov 2015
Paco
@pakoito
Nov 01 2015 03:25
Hi channel
I need advice on how to tackle this problem
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:25
Yo there
Paco
@pakoito
Nov 01 2015 03:25
I have a hot observable sending like 50 signals per second
those signals are tied to an id, so i can get 10 for id 1, 5 for id 2, none for id 3, etc...
I need to throttle them to send, once a second, the latest value for each of the ids
is groupBy, then sample, the correct approach?
AFAIK groupBy actually concatenates values, so it requires termination
which my hot observable can't assure
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:31
signalsForIds
  .buffer(1, SECONDS)
  .flatMap(listOfSignals -> { ... merge signals for same ids and emit as values })
Paco
@pakoito
Nov 01 2015 03:32
so, imperative code
there's a functional solution
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:33
sure
Nothing stops you from writing it as functional solution inside of flatMap
Paco
@pakoito
Nov 01 2015 03:34
you're grouping by time first, sorting by type second, taking latest third
with groupBy into sample you sort by type first, then group + latest in one operation
yeah groupby doesn't require termination, it's just the marbles being silly
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:40
signalsForIds
  .buffer(1, SECONDS)
  .flatMap(listOfSignals -> Observable.from(listOfSignals).groupBy(signal -> signal.id).last())
Paco
@pakoito
Nov 01 2015 03:43
fair enough
thanks
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:43
:+1:
Oh shi, no, it'll emit only last item from the last group
Paco
@pakoito
Nov 01 2015 03:46
don't worry, change the buffer to a window
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:46
problem in flatMap
Paco
@pakoito
Nov 01 2015 03:47
This message was deleted
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:48
The problem is to apply last() correctly so it 'll emit last item per group and not last item of the whole stream
Paco
@pakoito
Nov 01 2015 03:48
This message was deleted
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:49
Again will emit last item of the last group :)
Paco
@pakoito
Nov 01 2015 03:50
signalsForIds
  .groupBy(signal -> signal.id)
  .flatMap(obsSignals-> obsSignals.window(1, SECONDS).last())
I'll need to check on monday, np
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:53
Looks like an overhead because of using window per each group rather than window whole stream and then divide it to groups
Paco
@pakoito
Nov 01 2015 03:55
it's more expensive, isn't it?
okay
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Nov 01 2015 03:57
yep, seems so. Anyway your problem is solvable, but requires some non-trivial combination of operators :smile: Write unit tests and experiment with implementation!