blob: 2825161761633e9bf349ccaead5662931abea988 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php
class Stream {
private $stream;
function __construct() {
$this->stream = [];
}
function add( $var ) {
if(is_array($var)) {
foreach( $var as $v ) {
$this->stream[] = $v;
}
} else {
$this->stream[] = $var;
}
}
function get() {
if(sizeof($this->stream)>0) {
return array_shift($this->stream);
}
throw new Exception('Empty stream');
}
}
function stream_average($stream) {
static $n = 0, $t = 0;
return ($t+=$stream->get())/++$n;
}
$s = new Stream();
$s->add( array_map( function($x) { return $x*10;}, range(1,50) ) );
while(1) {
try {
echo stream_average($s),"\n";
} catch(Exception $e) {
break;
}
}
|