-1

I have a list:

val k = List(1,2,3,4,-69,78)

and would like to remove all negative elements in the list

I have the following code:

val k = List(1,2,3,4,-69,78)
val a = List()


for( k <- k){
         if(k > 0){
           a=a:+k
         }
      }

      println(a)

What it is supposed to run through the list and if an element in the list is positive, it should append it to another list in this case list a

however I get the following error:

ScalaFiddle.scala:9: error: reassignment to val a=a:+k ^

how can I fix this

Please note that I intentionally do not want to use l.filter.

If anyone has a better Idea as to how I can do this, it would be greatly appreciated

Thanks in advance

Koos
  • 117
  • 4
  • 15
  • 2
    **Scala** encourages _immutability_, in this case both the `val` an the list are _immutable_, you can either change `val` with `var` or `List` with `ListBuffer` _(you would need to use `a += k` instead)_. However for this problem there is a better _(simpler?)_ functional solution `val a = k.filter(i => i > 0)`. BTW, append to lists is **O(N)**, try todo avoid that. – Luis Miguel Mejía Suárez May 22 '19 at 12:43
  • Possibly duplicate of the question https://stackoverflow.com/questions/56258320/how-do-i-write-a-function-in-scala – oybek May 23 '19 at 08:30

4 Answers4

3

You are doing in a Java way. In Scala you should avoid mutable variables and use immutable variables as much as possible.

A better "Scala" solution for your problem is to do the following:

val k = List(1,2,3,4,-69,78)
val a = k.filter(_ > 0)

If you still want to append to the list, you need to change a to be:

val a = new mutable.MutableList[Int]()

And append like this:

a += k
Gal Naor
  • 2,397
  • 14
  • 17
0

You are trying to reassign a value to a constant (i.e val) which is not possible, the List a needs to be var instead:

val k = List(1,2,3,4,-69,78)
var a = List[Int]()


for(k <- k){
   if(k > 0){
       a = a :+ k
   }
}

println(a)
// Display List(1, 2, 3, 4, 78)
Valy Dia
  • 2,781
  • 2
  • 12
  • 32
0

You should use val instead of var. In short: var can be modified after initialized and val not.

But is important to notice that Scala discourage the use of var because of immutability.

val k = List(1,2,3,4,-69,78)
var a: List[Int] = List()

for( k <- k){
  if(k > 0){
    a=a:+k
  }
}

println(a)
Clairton Menezes
  • 85
  • 1
  • 1
  • 7
0

val in scala cannot be reassigned. When you write val a = List(), you are getting a which holds empty list.

When you write a :+ k you get new list, and you have to assign it to another value in order to access it in future, problem is that you cant reassign a cause it is val.

You could use var but this way is considered as a bad practice.

Another solution is to use functions:

def onlyPositives(k: List[Int], a: List[Int] = List.empty[Int]): List[Int] = k match {

    // case when there is no elements left in k
    case Nil => a

    // we get first element of k and
    // if it is positive, append it to a
    case x::tail => onlyPositives(tail, if (x > 0) a:+x else a)
}

Now you can write:

val k = List(1, 2, 3, 4, -69, 78)
val a = onlyPositives(k) // List(1, 2, 3, 4, 78)

P. S. I wonder why you do not use the filter?

oybek
  • 630
  • 4
  • 14