-
-
Save frodo821/1d26373970ef40108051435185b91e49 to your computer and use it in GitHub Desktop.
Reactive Extensions (Rx)のススメ ref: https://qiita.com/frodo821/items/250f13c4583dac7e8ffa
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
class Person { | |
public string | |
name, | |
mail; | |
public int age; | |
} | |
var people = new Person[] { | |
new Person {name="John", mail="john@hoge.com", age=32}, | |
new Person {name="Mike", mail="mike@fuga.com", age=41}, | |
new Person {name="Paul", mail="paul@foo.com", age=26}, | |
new Person {name="Stive", mail="stive@bar.com", age=29}, | |
new Person {name="Oren", mail="oren@hoge.com", age=35} | |
}; | |
var names = people.Where(p => p.age < 30) | |
.Select(p => p.name); | |
foreach(var n in names) { | |
Console.WriteLine(n); | |
} | |
// 出力: | |
// Paul | |
// Stive |
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
function init() { | |
window.timer = {closed: true}; | |
window.indicator = document.getElementById("head"); | |
window.hours = document.getElementById("hours"); | |
window.minutes = document.getElementById("minutes"); | |
window.seconds = document.getElementById("seconds"); | |
window.startButton = document.getElementById("start"); | |
window.resetButton = document.getElementById("reset"); | |
window.stopButton = document.getElementById("stop"); | |
indicator.innerText = "タイマー"; | |
Rx.Observable.fromEvent(startButton, "click") | |
.filter(() => timer.closed) | |
.subscribe(onStart); | |
Rx.Observable.fromEvent(resetButton, "click") | |
.filter(() => timer.closed) | |
.subscribe(onReset); | |
Rx.Observable.fromEvent(stopButton, "click") | |
.filter(() => !timer.closed) | |
.subscribe(onStop); | |
} | |
function onStart(e) { | |
var time = (parseInt(hours.value) * 3600 | |
+ parseInt(minutes.value) * 60 | |
+ parseInt(seconds.value)); | |
if(time == 0) | |
return; | |
indicator.innerText = formatTime(time); | |
window.timer = Rx.Observable.interval(1000).take(time).subscribe(function (t) { | |
var remined = time - t - 1; | |
if(remined != 0) { | |
indicator.innerText = formatTime(remined); | |
return; | |
} | |
indicator.innerText = "終了!"; | |
Rx.Observable.timer(1000).subscribe(() => indicator.innerText = "タイマー"); | |
}); | |
} | |
function onStop(e) { | |
console.log("stoped") | |
if(timer) | |
timer.unsubscribe(); | |
indicator.innerText = "停止しました"; | |
Rx.Observable.timer(1000).subscribe(() => indicator.innerText = "タイマー"); | |
} | |
function onReset(e) { | |
hours.value = 0; | |
minutes.value = 0; | |
seconds.value = 0; | |
indicator.innerText = "タイマー"; | |
} | |
function formatTime(secs) { | |
var h = Math.floor(secs / 3600); | |
var m = Math.floor(secs / 60 % 60); | |
var s = Math.floor(secs % 60); | |
return (h ? h.toString() + "時間" : "") + | |
(m ? m.toString() + "分" : "") + | |
(s ? s.toString() + "秒" : ""); | |
} |
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
class Person { | |
public string | |
name, | |
mail; | |
public int age; | |
} | |
var people = new Person[] { | |
new Person {name="John", mail="john@hoge.com", age=32}, | |
new Person {name="Mike", mail="mike@fuga.com", age=41}, | |
new Person {name="Paul", mail="paul@foo.com", age=26}, | |
new Person {name="Stive", mail="stive@bar.com", age=29}, | |
new Person {name="Oren", mail="oren@hoge.com", age=35} | |
}; | |
Observable.ToObservable(people) | |
.Where(p => p.age < 30) | |
.Subscribe( | |
p => Console.WriteLine(p), | |
_ => {}, | |
() => Console.WriteLine("Completed!")); | |
// 出力: | |
// Paul | |
// Stive |
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
from rx.subjects import Subject | |
Subject.interval(1000).subscribe(print) |
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
from time import sleep | |
count = 0 | |
while True: | |
sleep(1) | |
count += 1 | |
print(count) |
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
<!doctype html> | |
<html> | |
<head> | |
<title>Rx タイマー</title> | |
<meta charset="utf-8" /> | |
<script src="https://unpkg.com/@reactivex/rxjs@5.0.0/dist/global/Rx.min.js"></script> | |
<script src="main.js"></script> | |
</head> | |
<body> | |
<h1 id="head"></h1> | |
<form> | |
<input id="hours" type="number" value="0" min="0" />時間 | |
<input id="minutes" type="number" value="0" min="0" max="60" />分 | |
<input id="seconds" type="number" value="0" min="0" max="60" />秒 | |
<button type="button" id="start">スタート</button> | |
<button type="button" id="reset">リセット</button> | |
<button type="button" id="stop">ストップ</button> | |
</form> | |
<script>init();</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment