Skip to content

Instantly share code, notes, and snippets.

@propensive
Last active August 29, 2015 14:12
Show Gist options
  • Save propensive/0f7c45b3d51a68bc9afa to your computer and use it in GitHub Desktop.
Save propensive/0f7c45b3d51a68bc9afa to your computer and use it in GitHub Desktop.
Collection.strap
Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.core._
import rapture.core._
scala> List.strap(1, Some(2), None)
res0: List[Int] = List(1, 2)
scala> Vector.strap("a", List("b", "c"), "d", Some("e"))
res1: scala.collection.immutable.Vector[String] = Vector(a, b, c, d, e)
scala> Array.strap(None, Nil, Some('a))
res2: Array[Symbol] = Array('a)
scala> Vector.strap(None, Some(None), Some(Some(8)))
res3: scala.collection.immutable.Vector[Option[Int]] = Vector(None, Some(8))
scala> Seq.strap(1, if(math.random < 0.5) List(2, 3) else Nil)
res4: Seq[Int] = List(1)
scala> Seq.strap(1, if(math.random < 0.5) List(2, 3) else Nil)
res5: Seq[Int] = List(1, 2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment