by anonymoushn · 12y ago · 0 comments
List(1,2,3,4) filter isEven sum println
error: type mismatch;
found : Unit
required: Numeric[?]
List(1,2,3,4) filter isEven sum println
^ list filter isEven foreach println
As a side note omitting that many commas and parenthesis is very rare at least based on the Scala projects I've worked on. The above example is equal to the one below: list.filter(isEven).foreach(println)If you wanted to be really explicit you could do:
list.filter(x=> isEven(x)).foreach(x=> println(x))
yuck ;-)foreach takes a collection and applies some function (in this case println).
I've actually had a use case for what you're trying do, so created an extension method on Any, which means you can then do, on Any-thing:
(list filter isEven sum).echo
I also hate typing "println". "echo" I can bang out instantly, and can do so on any expression, before or after its definition.