[Assorted-commits] SF.net SVN: assorted: [348] scala-commons/trunk/src/commons/Collections. scala
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-10 04:04:40
|
Revision: 348 http://assorted.svn.sourceforge.net/assorted/?rev=348&view=rev Author: yangzhang Date: 2008-02-09 20:04:44 -0800 (Sat, 09 Feb 2008) Log Message: ----------- added bunch of stuff to Collections Modified Paths: -------------- scala-commons/trunk/src/commons/Collections.scala Modified: scala-commons/trunk/src/commons/Collections.scala =================================================================== --- scala-commons/trunk/src/commons/Collections.scala 2008-02-10 03:54:41 UTC (rev 347) +++ scala-commons/trunk/src/commons/Collections.scala 2008-02-10 04:04:44 UTC (rev 348) @@ -330,11 +330,20 @@ def str(xs: Iterable[Char]) = xs mkString "" /** - * Return an infinite stream, where each element evaluates gen. + * Return an infinite stream, where each element is an evaluation of gen. */ def repeat[a](gen: => a): Stream[a] = Stream.cons(gen, repeat(gen)) /** + * Return a stream of n elements, where each element is an evaluation of gen. + * Note that gen may be evaluated more than n times. See: + * + * "How to add laziness to a strict language, without even being odd" + * http://homepages.inf.ed.ac.uk/wadler/papers/lazyinstrict/lazyinstrict.ps + */ + def replicate[a](n: Int, gen: => a): Stream[a] = repeat(gen) take n + + /** * Return a stream of length at least n whose first elements are from the * given iterator, but if the iterator has fewer than n elements, then the * remaining elements are repeat(gen). @@ -388,9 +397,9 @@ /** * Indexes the result of groupBy. * <p> - * 0 1 2 3 4 5 6 7 8 9 - * [a,b,c,c,c,d,d,e,f,f] -> [[0],[1],[2,3,4],[5,6],[7],[8,9]] - * [] -> [] + // 0 1 2 3 4 5 6 7 8 9 + // [a,b,c,c,c,d,d,e,f,f] -> [[0],[1],[2,3,4],[5,6],[7],[8,9]] + // [] -> [] */ def indexGroups[a,b](xs: Seq[a])(f: a => b) = { val i = Iterator from 0 @@ -437,6 +446,9 @@ object Tree { abstract class Tree[a] { + /** + * Show the tree as a string. + */ // Leaf("a") -> // a // @@ -457,9 +469,34 @@ } r(this) mkString "\n" } + /** + * Access a node via the given index path. + */ + def get(path: Seq[Int]): a = (this, path) match { + case (Leaf(x), Seq() ) => x + case (Branch(ts), Seq(x, xs@_*)) => ts(x) get xs + case _ => throw new Exception("invalid path") + } + /** + * Flatten the leaves into a single stream. + */ + def flatten: Stream[a] = this match { + case Branch(ts) => Stream concat (ts map (_.flatten)) + case Leaf(x) => Stream cons (x, Stream empty) + } } case class Branch[a](ts: Seq[Tree[a]]) extends Tree[a] case class Leaf[a](x: a) extends Tree[a] + + /** + * Build a tree whose i-th level branch has a fanout of xs(i). + */ + def treeFromFanouts[a](gen: => a, fanouts: Seq[Int]): Tree[a] = + fanouts match { + case Seq() => Leaf(gen) + case Seq(fanout, rest@_*) => + Branch(replicate(fanout, treeFromFanouts(gen, rest)).toArray) + } } case class TreeNode[a](value: a, children: Seq[TreeNode[a]]) { @@ -611,4 +648,49 @@ // [1,3,5,6,7,8] odd -> ([1,3,5],[6,7,8]) // [] _ -> ([],[]) // + + def camelToLower(s: String, sep: String) = { + val xs = + for (c <- s) + yield if (c.isUpperCase) sep + c.toLowerCase else c + xs mkString "" + } + def camelToUnder(s: String) = camelToLower(s, "_") + def camelToHyphen(s: String) = camelToLower(s, "-") + + // TODO: this isn't really rot encoding + def rot(n: Int, s: String) = s map (_ + n toChar) mkString + + def untilNull[a](f: => a) = new Iterator[a] { + var upcoming = f + override def hasNext = upcoming != null + override def next = { + val emit = upcoming + upcoming = f + emit + } + } + + /** + * Returns the positive (unsigned int) modulo. + */ + def mod(n: Int, m: Int) = { + val r = n % m + if (r < 0) r + m else r + } + + /** + * Returns pairs of elements. + * <p> + * <code> + * pairs([a,b,c,d]) == [(a,b),(c,d)] + * pairs([a,b,c,d,e]) == [(a,b),(c,d)] + * </code> + */ + def pairs[a](xs: Seq[a]): Stream[(a,a)] = { + xs match { + case Seq() => Stream empty + case Seq(a, b, rest @ _*) => Stream cons ((a,b), pairs(rest)) + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |