scalascala-script

how do I use filter function in Scala script


As part of my learning, I am trying to write the Scala expression into a scala script but struck with an error.

The scala code I am having it successfully executed in Scala REPL is

def intList = List[1,2,3,4,5]
intList.filter(x => x%2 ==1).map(x => x * x).reduce((x,y) => x+y)

This successfully gets executed and the following is the result I get.

scala> intList.filter(x => x % 2 == 1).map(x => x * x).reduce((x,y) => x + y)
res15: Int = 35

I am trying to make this as a Scala script or class so as to rerun any number of times on demand. I save this in a file named SumOfSquaresOfOdd.scala

import scala.collection.immutable.List

object SumOfSquaresOfOdd extends App
{
    def main(args:Array[String]):Unit =
    {
            var intList = List[Integer](1,2,3,4,5,6,7,8,9,10)
            def sum = intList.filter(x => x % 2 ==1).map(x => x * x).reduce((x+y) => x + y)
            println sum
    }
}

When I compile this using scalac, the following error is printed on the console.

λ scalac SumOfSquaresOfOdd.scala
SumOfSquaresOfOdd.scala:8: error: not a legal formal parameter.
Note: Tuples cannot be directly destructured in method or function parameters.
  Either create a single parameter accepting the Tuple1,
  or consider a pattern matching anonymous function: `{ case (param1, param1) => ... }
            def sum = intList.reduce(x => x % 2 ==1).map(x => x * x).reduce((x+y) => x + y)
                                                                              ^
one error found

How do I use the filter, map, reduce methods in a script? Appreciate your help and support.

UPDATE: Typos updated in the code.


Solution

  • I can answer your question:

    How do I use the filter, map, reduce methods in a script?

    But I can't fully solve your specific use case because you didn't specify what the script should be doing.

    Try this code

    object SumOfSquaresOfOdd {
        def main(args: Array[String]) : Unit = {
            var intList = List(1,2,3,4,5,6,7,8,9,10)
            def sum = intList.filter(x => x % 2 ==1).map(x => x * x)
            println(sum)
        }
    }
    

    Then

    ~/Code/stack-overflow $ scalac SumOfSquaresOfOdd.scala
    ~/Code/stack-overflow $ scala SumOfSquaresOfOdd
    List(1, 9, 25, 49, 81)
    

    You seem to be a little lost. Here are some tips:

    1. Use Int rather than Integer; Int is Scala's integer type. And you don't need to import it.
    2. Don't extend App in this case. Refer to this question Difference between using App trait and main method in scala
    3. Use wrapping parens for println
    4. A literal List(1,2,3) will be type-inferenced to List[Int]; no need to explicitly type it. Check in the Scala REPL.
    5. I think you confused reduce with filter. Compare both in the latest scaladoc: http://www.scala-lang.org/api/current/#scala.collection.immutable.List
    6. Other ways to run scala code: http://joelabrahamsson.com/learning-scala-part-three-executing-scala-code/
    7. Highly recommend you do Functional Programming Principles in Scala if you're serious about learning.

    Good luck learning Scala! :)