Exchange array values โ€‹โ€‹using for and yield scala

I am trying to swap each pair of values โ€‹โ€‹in my array using for and yield, and so far I am very unsuccessful. I tried to do the following:

val a = Array(1,2,3,4,5) //What I want is Array(2,1,4,3,5) for(i<-0 until (a.length-1,2),r<- Array(i+1,i)) yield r 

The above fragment returns the vector 2,1,4,3 (and 5 is omitted)

Can someone point out what I'm doing wrong here, and how to get the correct U-turn using for and gives?

thanks

+7
source share
8 answers

It would be easier if you did not use for/yield :

 a.grouped(2) .flatMap{ case Array(x,y) => Array(y,x) case Array(x) => Array(x) }.toArray // Array(2, 1, 4, 3, 5) 
+11
source
 a.grouped(2).flatMap(_.reverse).toArray 

or if you need / crop (in this case it is less concise and actually expands to the same code):

 (for {b <- a.grouped(2); c <- b.reverse} yield c).toArray 
+35
source

I don't know if OP Scala is reading for the intolerant, but that was exercise 3.3.

I like the map solution, but we are not in this chapter yet, so this is my ugly implementation using the one required for / yield. You can probably move some profitability logic into defense / definition.

 for( i <- 0 until(a.length,2); j <- (i+1).to(i,-1) if(j<a.length) ) yield a(j) 

I am a Java guy, so I do not confirm this statement, but I'm curious about the overhead of maps / groups and iterators. I suspect it all boils down to the same Java bytecode.

+8
source

Another simple solution to work with:

 def swapAdjacent(array: ArrayBuffer[Int]) = { for (i <- 0 until array.length) yield ( if (i % 2 == 0) if (i == array.length - 1) array(i) else array(i + 1) else array(i - 1) ) } 
0
source

Here is my solution

  def swapAdjacent(a: Array[Int]):Array[Int] = (for(i <- 0 until a.length) yield if (i%2==0 && (i+1)==a.length) a(i) //last element for odd length else if (i%2==0) a(i+1) else a(i-1) ).toArray 

https://github.com/BasileDuPlessis/scala-for-the-impatient/blob/master/src/main/scala/com/basile/scala/ch03/Ex03.scala

0
source

If you are doing exercises 3.2 and 3.3 in Scala for the Impatient, here are my answers. They coincide with the movement of logic.

 /** Excercise 3.2 */ for (i <- 0 until a.length if i % 2 == 1) {val t = a(i); a(i) = a(i-1); a(i-1) = t } /** Excercise 3.3 */ for (i <- 0 until a.length) yield { if (i % 2 == 1) a(i-1) else if (i+1 <= a.length-1) a(i+1) else a(i) } 
0
source
 for (i <- 0 until arr.length-1 by 2) { val tmp = arr(i); arr(i) = arr(i+1); arr(i+1) = tmp } 

I recently started learning about Scala, and all the solutions from the Scala book for Impatient (1st Edition) are available on my github:

Chapter 2 https://gist.github.com/carloscaldas/51c01ccad9d86da8d96f1f40f7fecba7

Chapter 3 https://gist.github.com/carloscaldas/3361321306faf82e76c967559b5cea33

0
source

I have my decision, but without a lesson. Maybe someone will find this helpful.

 def swap(x: Array[Int]): Array[Int] = { for (i <- 0 until x.length-1 by 2){ var left = x(i) x(i) = x(i+1) x(i+1) = left } x } 
0
source

All Articles