Skip to content

Instantly share code, notes, and snippets.

@jsuereth
Created March 17, 2012 00:55
Show Gist options
  • Select an option

  • Save jsuereth/2053986 to your computer and use it in GitHub Desktop.

Select an option

Save jsuereth/2053986 to your computer and use it in GitHub Desktop.
Head/Tail + Init/Last extractors mirroring append/prepend
(master *$%=) ➜ ./build/quick/bin/scala
Welcome to Scala version 2.10.0-SNAPSHOT-20120316-312-d267988ddb (OpenJDK 64-Bit Server VM, Java 1.6.0_23).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val x = Vector(1,2,3,4)
x: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3, 4)
scala> val y :+ z = x
y: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)
z: Int = 4
scala> val y +: z = x
y: Int = 1
z: scala.collection.immutable.Vector[Int] = Vector(2, 3, 4)
scala> val y +: z = collection.mutable.ArrayBuffer(1,2)
y: Int = 1
z: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2)
scala> val y +: z = collection.mutable.ListBuffer(1,2)
y: Int = 1
z: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment