'use strict'
var stream = require('stream')
var stdin = process.stdin
var stdout = process.stdout
// 本実装
stream.prototype.addLineSwitch = function (outputs, controller) {
var input = this
var output_trash = new stream.Writable()
output_trash._write = function () {}
var rl = require('readline').createInterface(input, output_trash)
rl.on('line', function (line) {
var output = outputs[controller(line, outputs)]
if (output) output.write(new Buffer(line + '\n'))
})
}
// ObjectStream
function ObjectStream() {
var obj_stream = new stream.Duplex({objectMode: true})
obj_stream.data_array = []
obj_stream._write = function (data, _, callback) {
this.data_array.push(data)
callback()
}
obj_stream._read = function (size) {
this.push(this.data_array.shift() || null)
}
return obj_stream
}
//
var even = new ObjectStream
var odd = new ObjectStream
// 出力先とコントローラーの設定
var outputs = [even, odd]
var controller = function (line, outputs) {
return line % 2
}
// 標準入力にスイッチリスナを追加
stdin.addLineSwitch(outputs, controller)
###以下どこかのタイミングで###
for (var i of even.data_array) console.log(i) //等
J3VzZSBzdHJpY3QnCgp2YXIgc3RyZWFtID0gcmVxdWlyZSgnc3RyZWFtJykKdmFyIHN0ZGluID0gcHJvY2Vzcy5zdGRpbgp2YXIgc3Rkb3V0ID0gcHJvY2Vzcy5zdGRvdXQKCgovLyDmnKzlrp/oo4UKc3RyZWFtLnByb3RvdHlwZS5hZGRMaW5lU3dpdGNoID0gZnVuY3Rpb24gKG91dHB1dHMsIGNvbnRyb2xsZXIpIHsKCXZhciBpbnB1dCA9IHRoaXMKCXZhciBvdXRwdXRfdHJhc2ggPSBuZXcgc3RyZWFtLldyaXRhYmxlKCkKCW91dHB1dF90cmFzaC5fd3JpdGUgPSBmdW5jdGlvbiAoKSB7fQoJdmFyIHJsID0gcmVxdWlyZSgncmVhZGxpbmUnKS5jcmVhdGVJbnRlcmZhY2UoaW5wdXQsIG91dHB1dF90cmFzaCkKCXJsLm9uKCdsaW5lJywgZnVuY3Rpb24gKGxpbmUpIHsKCQl2YXIgb3V0cHV0ID0gb3V0cHV0c1tjb250cm9sbGVyKGxpbmUsIG91dHB1dHMpXQoJCWlmIChvdXR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