[Assorted-commits] SF.net SVN: assorted: [289] scala-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-02 09:09:07
|
Revision: 289 http://assorted.svn.sourceforge.net/assorted/?rev=289&view=rev Author: yangzhang Date: 2008-02-02 01:09:09 -0800 (Sat, 02 Feb 2008) Log Message: ----------- added median, sortStats; improved error handling for `run` Modified Paths: -------------- scala-commons/trunk/src/commons/Collections.scala scala-commons/trunk/src/commons/Io.scala Modified: scala-commons/trunk/src/commons/Collections.scala =================================================================== --- scala-commons/trunk/src/commons/Collections.scala 2008-02-02 09:08:15 UTC (rev 288) +++ scala-commons/trunk/src/commons/Collections.scala 2008-02-02 09:09:09 UTC (rev 289) @@ -89,6 +89,7 @@ def +(p: (Int, Int)) = Pair(x+p.x, y+p.y) } def sum(xs: Iterator[Int]) = xs.foldLeft(0)(_+_) + def mean(xs: Seq[Int]) = sum(xs.elements) / xs.size def not[a](pred: a => Boolean)(x: a) = !pred(x) // TODO make the following into rich iterator methods? // 3 [a,b,c,d,e] -> ([a,b,c],[d,e]) @@ -501,6 +502,14 @@ // implicit def force[a](lz: Lazy[a]) = lz.get def iterator2array[a](xs: Iterator[a]) = xs.toList.toArray def sum(xs: Seq[Double]) = xs reduceLeft ((x:Double,y:Double)=>x+y) + def median(xs: Seq[Long]) = xs(xs.length / 2) + def sortStats(xs: Array[Double]) = { + Sorting quickSort xs + val (mean, variance) = meanAndVariance(xs) + val (min, median, max) = (xs(xs.length / 2), xs(0), xs.last) + val sdev = Math sqrt variance + (mean, median, sdev, variance, min, max) + } def meanAndVariance(xs: Seq[Double]) = { val mean = sum(xs) / xs.length val variance = sum( xs map (x => Math pow (x - mean, 2)) ) / xs.length Modified: scala-commons/trunk/src/commons/Io.scala =================================================================== --- scala-commons/trunk/src/commons/Io.scala 2008-02-02 09:08:15 UTC (rev 288) +++ scala-commons/trunk/src/commons/Io.scala 2008-02-02 09:09:09 UTC (rev 289) @@ -90,12 +90,14 @@ def run(cmd: String, input: String) = { val proc = Runtime.getRuntime.exec(cmd) using (TextWriter(proc.getOutputStream)) (_ println input) + val out = using (TextReader(proc.getInputStream)) (_ read) + val err = using (TextReader(proc.getErrorStream)) (_ read) if (proc.waitFor != 0) { + Console.err println err throw new Exception( "command failed with exit status " + proc.exitValue + ": " + cmd ) } - using (TextReader(proc.getInputStream)) (_ read) } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |