Feedback loop without Subject in RX

回眸只為那壹抹淺笑 提交于 2019-12-23 10:15:17

问题


I have the following motion equations

move = target_position - position
position = position + move

where target_position is a stream and position is initialized at zero. I would like to have a stream of position. I have tried something like this (in rx pseudo code)

moves = Subject()
position = moves.scan(sum,0)
target_position.combine_latest(position,diff).subscribe( moves.on_next)

It works but I have read that using Subject should be avoided. Is it possible to compute the position stream without Subject?

In python the full implementation looks like this

from pprint import pprint 
from rx.subjects import Subject

target_position = Subject()

moves = Subject()

position = moves.scan(lambda x,y: x+y,0.0)

target_position\
    .combine_latest(position,compute_next_move)\
    .filter(lambda x: abs(x)>0)\
    .subscribe( moves.on_next)

position.subscribe( lambda x: pprint("position is now %s"%x))

moves.on_next(0.0)
target_position.on_next(2.0)
target_position.on_next(3.0)
target_position.on_next(4.0)

回答1:


You could use the expand operator

targetPosition.combineLatest(position, (target, current) => [target, current])
  .expand(([target, current]) => {
    // if you've reached your target, stop
    if(target === current) {
      return Observable.empty()
    }
    // otherwise, calculate the new position, emit it
    // and pump it back into `expand`
    let newPosition = calcPosition(target, current);
    return Observable.just(newPosition)
  })
  .subscribe(updateThings);


来源:https://stackoverflow.com/questions/32732019/feedback-loop-without-subject-in-rx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!