diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-07-27 11:09:20 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-07-27 11:09:20 +0800 |
| commit | 2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af (patch) | |
| tree | a2b64a6a110d201ceed00119701e94e5dc7c965f /challenge-122/james-smith/php/ch-1.php | |
| parent | f4e18fcb93b12bfc825272d2800e63ad4e56b140 (diff) | |
| parent | 4231c2f762b397e1cacd2cb7e3c2799254fcc1a4 (diff) | |
| download | perlweeklychallenge-club-2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af.tar.gz perlweeklychallenge-club-2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af.tar.bz2 perlweeklychallenge-club-2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-122/james-smith/php/ch-1.php')
| -rw-r--r-- | challenge-122/james-smith/php/ch-1.php | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-122/james-smith/php/ch-1.php b/challenge-122/james-smith/php/ch-1.php new file mode 100644 index 0000000000..2825161761 --- /dev/null +++ b/challenge-122/james-smith/php/ch-1.php @@ -0,0 +1,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; + } +} |
