-3

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.

5
  • You need (x,y) not (x+y) for the argument declaration. Commented May 6, 2016 at 6:58
  • You have an arrow pointing to your typo in scalac error... The code you claim to successfuly execute in repl certainly does not. Also you rather want to use Int not javas Integer. And you should either use extends App or def main ... not both. Commented May 6, 2016 at 7:08
  • "I am having it successfully executed in Scala REPL". No, you're not as it's syntactically incorrect. Please update your question with your actual code. Commented May 6, 2016 at 8:14
  • @jwvh and The Archetypal Paul: Thanks for pointing out. It was a typo. Got that corrected. I am not sure for a typo I deserve a negative score! :( Commented May 6, 2016 at 15:35
  • @Lukasz, Thanks for the suggestions. I will get to either of them in my Scala script/class. Commented May 6, 2016 at 15:36

1 Answer 1

3

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! :)

Sign up to request clarification or add additional context in comments.

2 Comments

Hello @mlg, Thank you very much for your understanding and the considerate suggestions with the solution! I sincerely appreciate the same. Thanks for NOT making a down vote which appears to be scary for the people who are newbie to a particular technology. Thanks for the additional links, which I will surely go through.
Hello @mlg, Yes, it was a mistake. I intended to do filter but typed reduce, which is acutally used at the end of the chaining to get the Sum of the final list of integers. Got that corrected and the code works like a charm with all your other suggestions mentioned in 2, 3. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.