Nifty Traits, Classes and Methods Since Scala 2.8

scala.Predef

  • object scala.Predef
    • Predef$ArrowAssoc#-> has been defined with @inline.
    • @inline locally[T](x: T): T = x
      object X{
        class Foo
        {
          println("in the definition of Foo")
        }
        class Bar
        locally{
          println("not in the definiton of Bar")
        }
      }
      new X.Foo; new X.Bar


scala.Option

  • trait scala.Option
    • Option.apply(null) or Option(null) // => None
      Option.apply(1) or Option(1)     // => Some(1)
  • trait scala.Option
    • None orNull           // => null
      Some(1) orNull     
      // => 1

scala.FunctionX


scala.io.PartialFunction

  • object scala.PartialFunction
    • import PartialFunction._
      def strangeConditional(other: Any): Boolean = cond(other) {
        case x: String if x == "abc" || x == "def"  => true
        case x: Int => true
      }
    • import PartialFunction._
      def onlyInt(v: Any): Option[Int] = condOpt(v) { case x: Int => x }
  • trait scala.PartialFunction
    • {{case x if x.length> 0 => x.length}:PartialFunction[String, Int]} lift "" // => None

scala.runtime.AbstractFunctionX


Collections

  • trait scala.collection.SeqLike
    • (0 to 5) sortWith {_>_} toList // => List(5, 4, 3, 2, 1, 0)
    • "ABC" sortBy {'Z'-_} // => "CBA"
    • "aabbaabbaa" indexOfSlice "ab" // => 1
    • "aabbaabbaa" lastIndexOfSlice "ab" // => -1 ??? Ticket 3455
    • ("abc" corresponds "ABC") {_.toUpper==_} // => true
    • "aBc" indexWhere(_.isUpper) // => 1
    • "aBc" lastIndexWhere(_.isUpper) // => 1
    • "aBBc" segmentLength(_.isUpper, 1) // => 2
    • "BBc" prefixLength(_.isUpper) // => 2 ; def prefixLength(p: A => Boolean) = segmentLength(p, 0)
    • "abc" updated( 1, 'B') // => "aBc"
    • "abcXXXXdef" patch(3, "YYY", 4)  // => "abcYYYdef"
    • "abc" padTo(5, '!') // => "abc!!"
    • "abc" indices // => Range(0, 1, 2)
    • "abbc" distinct // => "abc"
    • "abc" union "def" // => "abcdef"
    • "abc" diff "ac" // => "b"
    • "abcdef" intersect "bcd" // => "bcd"
    • "ab" :+ 'c' // => "abc"
    • 'a' +: "bc" // => "abc"
    • "abc" zipWithIndex // => Vector(('a',0), ('b',1), ('c',2))
    • "abc" zip "def" // => Vector(('a',0), ('b',1), ('c',2))
    • "abc" tail // => "bc"
    • "abc".reverseIterator.toList // => List('c', 'b', 'a')
    • "abc" reverseMap{_.toUpper} // => "CBA"
  • trait scala.collection.TraversableLike
    • List(1,1,2,5,5,5) groupBy identity mapValues (_.size) // => Map((1,2), (2,1), (5,3))
    • "" nonEmpty // => false
    • "abc" head // => 'a'
    • "aBc" filterNot {_.isUpper} // => "ac"
    • List(1,"2",3) collect {case c:String => c} // => List(2)
    • (0 to 10) withFilter { _%2==1 } map identity // => Vector(1, 3, 5, 7, 9)
    • val x = for(i<-(0 to 10)) yield { println(i); i }
    • val x = for(i<-(0 to 10).view) yield { println(i); i } ; x(0); x(1)
  • trait scala.collection.MapLike
    • List(1,1,2,5,5,5) groupBy identity mapValues (_.size) // => Map((1,2), (2,1), (5,3))
  • trait scala.collection.IndexedSeqOptimized
    • Map((1->2),(2->3)) span {case(k,v) => k%2==1} // => (Map(1 -> 2),Map(2 -> 3))
    • "abc" splitAt 1 // => (a,bc)
  • trait scala.collection.Iterator
    • (1 to 5).iterator.sliding(3).toList  // => List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5))
    • (1 to 5).iterator.sliding(4, 3).toList // => List(List(1, 2, 3, 4), List(4, 5))
    • (1 to 5).iterator.sliding(4, 3).withPartial(false).toList // => List(List(1, 2, 3, 4))
    • val it2 = Iterator.iterate(20)(_ + 5)
      (1 to 5).iterator.sliding(4, 3).withPadding(it2.next).toList
      // => List(List(1, 2, 3, 4), List(4, 5, 20, 25))
    • (1 to 7).iterator grouped 3 toList    // => List(List(1,2,3),List(4,5,6),List(7))
    • (1 to 7).iterator grouped 3 withPartial false toList  // => List(List(1,2,3), List(4,5,6))
    • val it2 = Iterator.iterate(20)(_ + 5)
      (1 to 7).iterator grouped 3 withPadding it2.next toList // => List(List(1, 2, 3), List(4, 5, 6), List(7, 20, 25)
    • (0 to 10) sameElements (10 to 0 by -1).reverse // => true
    • (0 to 4) partition {_>2} // => (IndexedSeq(3, 4),IndexedSeq(0, 1, 2))
  • object scala.collection.Iterator
    • Iterator.fill(3)(0) toList // => List(0, 0, 0)
  • trait scala.collection.IterableLike
    • "foo.jpg" dropRight 4 => // "foo"
    • "foo.jpg" takeRight 3 => // "jpg"
    • "abc".iterator.toSeq => ArrayBuffer('a', 'b', 'c')
    • "abc" zipAll ("AB", "x", "X") // => Vector((a,A), (b,B), (c,X))
  • trait scala.collection.TraversableOnce
    • (1 to 10) sum // => 55
    • (1 to 10) product // => 3628800
    • (1 to 10) min // => 1
    • (1 to 10) max // => 10
    • (1 to 10) count { _%2==1 } // => 5
    • Array.empty[Int].reduceLeftOption(_+_) // => None
    • val x:List[Int] = Nil; x.reduceRightOption(_+_) // => None
    • List((1->2),(3->4)).toMap // => Map((1,2), (3,4))
    • List((1->2),(3->4)).toSet // => Set((1,2), (3,4))
    • List((1->2),(3->4)).toMap.toList // => List((1,2), (3,4))
    • "abc" toIterable // => abc in Iterable[Char]
    • "abc" toIndexedSeq // => ArrayBuffer(a, b, c)
    • "abc" toStream // => Stream(a, ?)
  • trait scala.collection.TraverableLike
    • Map().stringPrefix // => Map
  • trait scala.collection.generic.GenericTraversableTemplate
    • (0 to 3) unzip {p => (p, -p)} // => (Vector(0, 1, 2, 3),Vector(0, -1, -2, -3))
    • List(List(1, 2, 3), List(4, 5), List(6, 7, 8)) transpose // => List(List(1, 4, 6), List(2, 5, 7), List(3, 8))
  • abstract class scala.collection.generic.TraversableFactory
    • List.tabulate[Int](3, 3)(_ * _) // => List(List(0, 0, 0), List(0, 1, 2), List(0, 2, 4))
  • trait scala.collection.immutable.List
    • xs reverse_::: ys is equivalent to xs.reverse ::: ys but is more efficient.
  • trait scala.collection.immutable.StringLike
    • "foo.jpg" stripPrefix "foo" // => "jpg"
    • "foo.jpg" stripSuffix ".jpg" // => "foo"
    • "%d" formatLocal(java.util.Locale.US, 1)  // => "1"
  • object scala.collection.mutable.Array
    • Array.ofDim[Int](1,2,3) // => Array(Array(Array(0, 0, 0), Array(0, 0, 0)))
  • class scala.collection.immutable.Vector
  • Available Methods for String on Scala 2.7 & 2.8
  • The previously deprecated method List.+ has been removed on 2.8.
  • the deprecated methods in 2.8, compiled by @jorgeortiz85, http://tinyurl.com/yzrk6e7 ,http://tinyurl.com/ygxam39

scala.util

  • object scala.util.control.TailCalls
    • import scala.util.control.TailCalls._
      def isEven(xs: List[Int]): TailRec[Boolean] =
        if (xs.isEmpty) done(true) else tailcall(isOdd(xs.tail))
      def isOdd(xs: List[Int]): TailRec[Boolean] =
        if (xs.isEmpty) done(false) else tailcall(isEven(xs.tail))
      isEven((1 to 100000).toList).result

  • object scala.util.control.Exception
    • util.control.Exception.catching(classOf[NumberFormatException]) opt "55bloogle".toInt
    • util.control.Exception.allCatch opt "55bloogle".toInt getOrElse(-1)
    • util.control.Exception.allCatch either "55bloogle".toInt
  • class scala.util.control.Breaks
    • val mybreaks = new Breaks
      import mybreaks.{break, breakable}
      breakable {
        for (...) {
          if (...)
            break
        }
      }
  • trait scala.util.control.ControlThrowable
    • A marker trait indicating that the Throwable is intended for flow control.
    • import scala.util.control.ControlThrowable
      try {
         // Body might throw arbitrarily
      } catch {
       case ce : ControlThrowable => throw ce // propagate
       case t : Exception => log(t)                      // log and suppress
      }
  • trait scala.util.NoStackTrace
    • A trait for exceptions which, for efficiency reasons, do not fill in the stack trace.
    • scala.util.control.ControlThrowable has been defined with this trait.
  • object scala.util.JenkinsHash
    • Original algorithm due to Bob Jenkins.
        http://burtleburtle.net/bob/c/lookup3.c
      Scala version partially adapted from java version by Gray Watson.
        http://256.com/sources/jenkins_hash_java/JenkinsHash.java

scala.Stream


scala.io.Source

  • use scala.io.Source#fromPath if you pass a String, and io.Source#fromFile if you pass a java.io.File.
  • Source#close

scala.annotation

  • @elidable: An annotation for methods for which invocations might be removed in the generated code.
  • @switch: An annotation to be applied to a match expression.
  • @tailrec: A method annotation which verifies that the method will be compiled with tail call optimization.
  • @migration: An annotation that marks a member as having changed semantics between versions.

scala.util.Random

  • Random object
    • scala.util.Random.shuffle(0 to 10 toSeq)
    • scala.util.Random.alphanumeric.take(5).mkString

scala.xml


scala.Actor


Parser combinators


Manifest


scala.util.parsing.combinator.PackratParser