diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-07-24 23:48:22 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-07-24 23:48:22 +0100 |
| commit | ed976c214b3dec9692231aa0fe2ace1b1ed17527 (patch) | |
| tree | 3ced8de87c160c6a6d9d70f9152327ccdd9ad64a /challenge-122/james-smith/php/ch-1.php | |
| parent | 5817dd19fdf336e64e8521c6db776393c615e12b (diff) | |
| download | perlweeklychallenge-club-ed976c214b3dec9692231aa0fe2ace1b1ed17527.tar.gz perlweeklychallenge-club-ed976c214b3dec9692231aa0fe2ace1b1ed17527.tar.bz2 perlweeklychallenge-club-ed976c214b3dec9692231aa0fe2ace1b1ed17527.zip | |
ch-1 in php
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; + } +} |
