[Assorted-commits] SF.net SVN: assorted:[1829] sandbox/trunk/src/scala/EnrichCollections.scala
Brought to you by:
yangzhang
From: <yan...@us...> - 2012-05-17 19:30:49
|
Revision: 1829 http://assorted.svn.sourceforge.net/assorted/?rev=1829&view=rev Author: yangzhang Date: 2012-05-17 19:30:43 +0000 (Thu, 17 May 2012) Log Message: ----------- Add enrich-collections sandbox Added Paths: ----------- sandbox/trunk/src/scala/EnrichCollections.scala Added: sandbox/trunk/src/scala/EnrichCollections.scala =================================================================== --- sandbox/trunk/src/scala/EnrichCollections.scala (rev 0) +++ sandbox/trunk/src/scala/EnrichCollections.scala 2012-05-17 19:30:43 UTC (rev 1829) @@ -0,0 +1,44 @@ +// For playing with Miles Sabin's 2.10 commit in 2.9. +// https://github.com/scala/scala/commit/25d1f6d0cb720129b5ac647542cfca3d18304ae2 + +import scala.collection._ +import scala.collection.generic._ + +object GenTraversableLike { + type HasElem[Repr, A] = Repr => GenTraversableLike[A, Repr] + trait FromRepr[Repr] { + type A + val hasElem: HasElem[Repr, A] + } + /** Manufacture a conversion from collection representation type `Repr` to + * its corresponding `GenTraversableLike` given an implicitly available + * instance of `FromRepr[Repr]`. + * @see [[scala.collection.generic.FromRepr]] + */ + implicit def fromRepr[Repr](implicit fr : FromRepr[Repr]) = fr.hasElem + + implicit val stringFromRepr : FromRepr[String] { type A = Char } = new FromRepr[String] { + type A = Char + val hasElem = implicitly[HasElem[String, Char]] + } + + implicit def genTraversableLikeFromRepr[C[_], A0] + (implicit hasElem0: HasElem[C[A0], A0]) : FromRepr[C[A0]] { type A = A0 } = new FromRepr[C[A0]] { + type A = A0 + val hasElem = hasElem0 + } + + + def typed[T](t : => T) {} + + class FilterMapImpl[A, Repr](val r : Repr)(implicit hasElem : HasElem[Repr, A]) { + def filterMap[B, That](f : A => Option[B])(implicit cbf : CanBuildFrom[Repr, B, That]) : That = r.flatMap(f(_).toSeq) + } + + implicit def filterMap[Repr : FromRepr](r : Repr) = new FilterMapImpl(r) + + val s = "Hello World" + val fms1 = s.filterMap(c => if(c >= 'A' && c <= 'Z') Some(c) else None) + typed[String](fms1) + println(fms1) +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |