Created
December 7, 2017 03:34
-
-
Save mogeta/9f8775ff487cae81b836b07198b80023 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"install\n", | |
"\n", | |
"```\n", | |
"pip install rx\n", | |
"```\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Got: 84\nGot: 88\n" | |
] | |
} | |
], | |
"source": [ | |
"from rx.subjects import Subject\n", | |
"\n", | |
"\n", | |
"def is_even(num):\n", | |
" \"\"\"\n", | |
" 偶数かどうかを判定する。filterで呼び出されている。\n", | |
" TrueかFalseを返すのであって、数値自体は返さない。\n", | |
" :param num: \n", | |
" :return: Bool\n", | |
" \"\"\"\n", | |
" return num % 2 == 0\n", | |
"\n", | |
"\n", | |
"def double(num):\n", | |
" \"\"\"\n", | |
" 受け取った値を倍返し\n", | |
" :param num: \n", | |
" :return: int\n", | |
" \"\"\"\n", | |
" return num * 2\n", | |
"\n", | |
"\n", | |
"# subjectを作成\n", | |
"stream = Subject()\n", | |
"# 一連の処理を作成する。投げられた値が偶数だったら倍にして表示する。\n", | |
"d = stream\\\n", | |
" .filter(is_even) \\\n", | |
" .map(double) \\\n", | |
" .subscribe(lambda x: print(\"Got: %s\" % x))\n", | |
"\n", | |
"# 試しに値をなんぼか送信\n", | |
"stream.on_next(42)\n", | |
"stream.on_next(43)\n", | |
"stream.on_next(44)\n", | |
"stream.on_next(45)\n", | |
"\n", | |
"# disposeしたあとに値を送信しても何もおこらない。\n", | |
"d.dispose()\n", | |
"stream.on_next(46)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment