score:4
Accepted answer
Your code is actually fine but you are missing what you need to print inside the for loop you are not giving the value in println which is a.
When you write for(a <- list)
that means for a in list where a is the element of the list. And you need to print the element of the list which is a. So you do
println(a)
You can use foreach
without(originalList).foreach(println)
You can directly do println(list)
In your code
val list=without(originalList)
for( a <- list ){
println(a)
}
Actually in your code you don’t need return statement as the last line of a function in scala is considered as return value.
val originalList = List(5, -1, 0, 3, 2)
def without(originalList: List [Int] ) : List[Int] = originalList.filter(_ == 3)
val list=without(originalList)
for( a <- list ){
println(a)
}
Source: stackoverflow.com
Related Query
- How to print list returned by a function
- How do I print a List of anything in Scala?
- How to print a list of strings to standard error in Scala?
- How to create function which would make flat List from arbitrarily deeply nested lists?
- How to print out the type of a list in Scala?
- how to convert rdd to list effectively without using collect function
- How to apply sequence function to List of ValidatedNel in cats?
- Scala how to print contents of a class's returned string and not Class@3c8bdd5b
- How to call returned function with implicits without assigning to val
- Scala: How to print a List in a Future
- how to calculate factorial of list using higher order function in scala
- How to pretty print value returned by Option in scala?
- How to define a infinite Lazy List using a given function or property in Scala?
- How to pass the initial value to foldLeft from a filtered List with function chaining?
- How to apply function to list of options in Scala?
- How to understand type in the function body instead of in the function parameter list
- How to write a function that takes a list of column names of a DataFrame, reorders selected columns the left and preserves unselected columns
- Scala: How to print a list of map using scala
- how can I write a function in scala called Order that takes one arguement: a list of Ints. And returns the same List of Ints from least to greatest
- How to return a list of lists from a recursive function in scala?
- How can I print a list which is in another list? (scala)
- How can i count occurrences in a list using this function in scala
- how to use reduce function to add every tuple2's value from every inner List
- How do I return a new list from a function in scala?
- Scala: how to use immutable values returned from function
- How does the underscore placeholder in (println _) represent an entire parameter list of its original function literal?
- How to remake function that group list to simple recursion?
- Is it possible to print the returned value of a function of different class in case of Anonymous object of scala
- How do I pass a partial function to a list which contains partial function?
- Using scala spark how to print just the response body value returned from a HTTP post call
More Query from same tag
- Sink.asPublisher(fanout = false) only supports one subscriber (which is allowed, see reactive-streams specification, rule 1.12)
- self actor hangs in repl
- Scala calculating two values in a map
- How to remove last line from RDD Spark Scala
- How to create instance from Json when one of the constructor params is initialized by default?
- How can I empty the cart contents without closing the session?
- How to parametrize a function with inherited types in Scala
- How do I un/marshall attributed elements in JAXB?
- Regex parse a list of comma separated number ranges, and capture them in individual groups
- PlayFramework scala template generic types cause error: ']' expected but eof found. And error: identifier expected but $XMLSTART$< found
- How can I convert a String to a Symbol in Runtime in Scala?
- Spark export to Teradata
- Scala child class inherits parent class traits?
- How to sum up pair elements individually in Scala
- How to enable a curried function to be inherited and then partially applied
- Lenses support in Play framework
- How to use LongSerializer in scala with hector?
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- how to declare a mutable map with key value typing in Scala without initialization
- Scala: implementing Map with concrete types
- Iterate and trim string based on condition in spark Scala
- Cache system in Scala
- Cleanup work in Scala or Java
- Why does Scala Option.tapEach return Iterable, not Option?
- RDD Aggregate in spark
- How to map multiple Futures and pass them as arguments to a view using Play with Scala
- Scala - copy fields of a StructType results in 'cannot resolve symbol fields'
- Replace values of a DataFrame using Scala's API
- Sort HList by pattern
- Early initializer `new {} with SomeTrait` fails