衍生流的最强组合

时间:2016-02-12 15:29:58

标签: javascript rxjs

我有3个流,一个说主流s1,两个派生流。我想获得这些流的最新组合。

const O = Rx.Observable

let s1$ = O.from([1,2,3])
let s2$ = s1$.map(x => x*10)
let s3$ = s1$.map(x => x*100)

let s$ = O.combineLatest(s1$, s2$, s3$, (s1, s2, s3) => {
  //console.log('s1, s2', s1, s2, s3)  
  return s1 + s2 + s3
})

s$.subscribe(x => {
  console.log('s = ' + x)
})

输出结果为:

"s = 111"
"s = 112"
"s = 122"
"s = 222"
"s = 223"
"s = 233"
"s = 333"

但我想要的是:

"s = 111"
"s = 222"
"s = 333"

所以只有最后一个并从s2和s3形成s1及其派生值。实现它的最佳方法是什么?

小提琴: https://jsbin.com/haduvedule/edit?js,console,output

1 个答案:

答案 0 :(得分:1)

You have to from PyQt4 import QtCore, QtGui import pandas as pd import re import sys class MainWindow(QtGui.QWidget): def __init__(self): self.app = QtGui.QApplication(sys.argv) super(MainWindow, self).__init__() layout = QtGui.QVBoxLayout() self.setLayout(layout) self.inputBox = QtGui.QLineEdit() self.tableView = QtGui.QTableView() layout.addWidget(self.inputBox) layout.addWidget(self.tableView) self.inputBox.textChanged.connect(self._textChanged) self.show() def startup(self): sys.exit(self.app.exec_()) def _textChanged(self): text = self.inputBox.text() pattern = text + ".*" self.setView(pattern) def setData(self, data): self.model = PandasModel(data) self.tableView.setModel(self.model) def setView(self, regexp): rows = self.model.getRows(regexp) for row in range(self.model.totRows): if row in rows: self.tableView.setRowHidden(row, False) else: self.tableView.setRowHidden(row, True) class PandasModel(QtCore.QAbstractTableModel): """ Class to populate a table view with a pandas dataframe Stolen from http://stackoverflow.com/questions/31475965/fastest-way-to-populate-qtableview-from-pandas-data-frame """ def __init__(self, data, parent=None): QtCore.QAbstractTableModel.__init__(self, parent) self._data = data self.curRows = 0 self.totRows = len(self._data.values) def canFetchMore(self, index): """ Note this and my fetchMore implementation were stolen from https://riverbankcomputing.com/pipermail/pyqt/2009-May/022968.html """ if self.curRows < self.totRows: return True else: return False def fetchMore(self, index): remainder = self.totRows - self.curRows itemsToFetch = min(5, remainder) self.beginInsertRows(QtCore.QModelIndex(), self.curRows, self.curRows+(itemsToFetch-1)) self.curRows += itemsToFetch self.endInsertRows() def rowCount(self, parent=None): return self.curRows def columnCount(self, parent=None): return self._data.columns.size def data(self, index, role=QtCore.Qt.DisplayRole): if index.isValid(): if role == QtCore.Qt.DisplayRole: QtCore.pyqtRemoveInputHook() return str(self._data.iloc[index.row(), index.column()]) return None def headerData(self, col, orientation, role): if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: return self._data.columns[col] return None def getRows(self, regexp): out = [] col = self._data.columns.get_loc('data') for row in range(self.rowCount()): check = self.data(self.index(row, col)) match = re.match(regexp, check) if match: out.append(row) return out if __name__ == "__main__": myApp = MainWindow() data = {'a':range(100), 'b':[str(chr(i+97))for i in range(10)]*10, 'data':['abc', 'acd', 'ade', 'bcd', 'bde', 'bef', 'cde', 'cef', 'cfg', 'def']*10, 'c':['123', '456', '789', '101', '102', '103', '104', '105', '106', '107']*10} data = pd.DataFrame(data) myApp.setData(data) myApp.startup() the s1$ observable. i.e. your code becomes :

share

Why is that has been extensively discussed on SO, I encourage to have a look at the distinction between hot and cold observables. It is a key concept of Rx and a common stumbling block for new comers.