Skip to content

Instantly share code, notes, and snippets.

@ymnk
Created October 29, 2010 06:06
Show Gist options
  • Save ymnk/653010 to your computer and use it in GitHub Desktop.
Save ymnk/653010 to your computer and use it in GitHub Desktop.
$ scala
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.
scala> "IBM".map(_-1)
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(72, 65, 76)
scala> "IBM".map(_-1):String
<console>:6: error: type mismatch;
found : scala.collection.immutable.IndexedSeq[Int]
required: String
"IBM".map(_-1):String
scala> "IBM".map(_-1)(collection.breakOut):String
<console>:6: error: type mismatch;
found : java.lang.Object with scala.collection.generic.CanBuildFrom[Any,Char,String]
required: scala.collection.generic.CanBuildFrom[String,Int,String]
"IBM".map(_-1)(collection.breakOut):String
scala> Vector(73, 66, 77).map(_-1)(collection.breakOut):String
<console>:6: error: type mismatch;
found : java.lang.Object with scala.collection.generic.CanBuildFrom[Any,Char,String]
required: scala.collection.generic.CanBuildFrom[scala.collection.immutable.Vector[Int],I\
nt,String]
Vector(73, 66, 77).map(_-1)(collection.breakOut):String ^
scala> :load Test.scala
:load Test.scala
Loading Test.scala...
defined module Test
scala> import Test._
import Test._
scala> "IBM".map(_-1):String
res2: String = HAL
scala> Vector(73, 66, 77).map(_-1)
res3: scala.collection.immutable.Vector[Int] = Vector(72, 65, 76)
scala> Vector(73, 66, 77).map(_-1)(collection.breakOut):String
res5: String = HAL
scala>
object Test {
implicit def foo = new collection.generic.CanBuildFrom[String, Int, String]{
import collection.mutable.{Builder, StringBuilder}
def apply(from: String) = apply()
def apply() = new Builder[Int, String]{
val s = new StringBuilder
def +=(elem: Int) = { s+=(elem.toChar); this }
def clear() = s.clear()
def result(): String = s.result()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment