Last active
December 7, 2017 01:45
-
-
Save mogeta/d557aace28a37c7134b7a8639ee6c540 to your computer and use it in GitHub Desktop.
https://github.com/ReactiveX/RxPY の解説。Subjectについて。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rx.subjects import Subject | |
def is_even(num): | |
""" | |
偶数かどうかを判定する。filterで呼び出されている。 | |
TrueかFalseを返すのであって、数値自体は返さない。 | |
:param num: | |
:return: Bool | |
""" | |
return num % 2 == 0 | |
def double(num): | |
""" | |
受け取った値を倍返し | |
:param num: | |
:return: int | |
""" | |
return num * 2 | |
# subjectを作成 | |
stream = Subject() | |
# 一連の処理を作成する。投げられた値が偶数だったら倍にして表示する。 | |
d = stream\ | |
.filter(is_even) \ | |
.map(double) \ | |
.subscribe(lambda x: print("Got: %s" % x)) | |
# 試しに値をなんぼか送信 | |
stream.on_next(42) | |
stream.on_next(43) | |
stream.on_next(44) | |
stream.on_next(45) | |
# disposeしたあとに値を送信しても何もおこらない。 | |
d.dispose() | |
stream.on_next(46) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment