Skip to content

Instantly share code, notes, and snippets.

@stevez
Created April 30, 2015 02:32
Show Gist options
  • Save stevez/8bdab52680053829c7d5 to your computer and use it in GitHub Desktop.
Save stevez/8bdab52680053829c7d5 to your computer and use it in GitHub Desktop.
scala swap array using yield
val s = Array(1,2,3,4,5)
for ( i<-0 until s.length) yield {
if ( i%2 == 1) s(i-1)
else if(i == s.length-1) s(i)
else s(i+1)
}
//scala.collection.immutable.IndexedSeq[Int] = Vector(2, 1, 4, 3, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment