From ed976c214b3dec9692231aa0fe2ace1b1ed17527 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sat, 24 Jul 2021 23:48:22 +0100 Subject: ch-1 in php --- challenge-122/james-smith/php/ch-1.php | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 challenge-122/james-smith/php/ch-1.php (limited to 'challenge-122/james-smith/php/ch-1.php') 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 @@ +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; + } +} -- cgit