score:2
Accepted answer
I'm not entirely sure what your actual goal is. I guess what you are trying to achieve seem to be better solved by filter
and map
:
filter
serves as validator in this case, i.e.,people.filter(isFirstNameValid)
. This returns a collection of all people with a defined first name -- is this what you want?map
serves as extractor of the desired field, in this case the first name. So overallpeople.filter(isFirstNameValid).map(_.firstName)
in case you want your collection to represent first names instead of a complete person. If you furthermore want to convert fromOption
(of first name) to a concrete value you may want to useflatMap
instead (this also makes the explicit validation unnecessary).
In case you really what to stick to a solution based on modifying external state, you have to place your mutable variable in an external scope for instance...
Source: stackoverflow.com
Related Query
- Scala initialize a collection type variable to null outside a for loop and assign some method's return value to the variable inside the loop
- what is the use of assigning a value to a variable outside the for loop in scala
- Scala : How to use variable in for loop outside loop block
- Handle exception for Scala Async and Future variable, error accessing variable name outside try block
- Scala collection type for filter
- Different type inference for `def` and `val` in Scala
- Scala Function.tupled and Function.untupled equivalent for variable arity, or, calling variable arity function with tuple
- Why does Scala choose the type 'Product' for 'for' expressions involving Either and value definitions
- Use case and examples for type pattern with type variable
- Scala for loop and iterators
- Scala type inference for existential types and type members
- Scala 2.7.x type mismatch error when passing null for a reference type
- Scala type mismatch error in for loop
- Adding to scala map within for loop and conditional statement
- Best Scala collection type for vectorized numerical computing
- Assign to a variable in the condition of a while loop - scala
- Scala collections: transform content and type of the collection in one pass
- Scala inconsistence behavior for final vals with and without type ascription
- Can a Scala for loop modify variables outside its scope?
- Scala 2.10 reflection: Why do I get the same type "List()" for the list and list element?
- Scala type inference for both a generic type and it's type parameter - why doesn't it work?
- Efficiently iterate over one Set, and then another, in one for loop in Scala
- Scala - Pattern Matching and For loop issue
- Scala refined integer for both compile-time literal and run-time variable
- Checking null value in Scala for Java data type
- Extract part of String using regex and assign it to variable scala
- Why is the each iteration parameter val and not var in for loop in scala
- Yield an ArrayBuffer (or other mutable Collection type) from a for loop in Scala
- Appropriate collection type for selecting a random element efficiently in Scala
- Type for parallel or sequential collection in scala
More Query from same tag
- Scala: Print Double with decimal and thousands seperator and 2 decimal places
- Combining arbitrary number of sources with materialized values
- Play slick session connection timeout
- Scala wrap generic anonymous class
- Scala playfamework - how to wrap one JSON string with another?
- Use type class to calculate the sum of each string's length
- scala io throws error while reading a file
- Truncating the data-frame column values in Scala
- Sbt: why does it need scala-lang 2.10.3?
- Spark - Best way agg two values using ReduceByKey
- Loading a directory from resource file and loading files into a list
- reduceByKey: How does it work internally?
- Is there a Future.sequence analog in kotlin?
- Library to Integrate Facebook login with Play Framework?
- Scala/Play: Create a Future[ List[ ... ] ] from a (partial) List[ ... ]
- Scala/Play: load template dynamically
- How can I chain my custom actions when using ActionBuilder
- Lihaoyi PPrint Deep Tree
- Deserialize nested polymorphic json field with json4s
- sbt throws error while launching it
- Ordering doesn't hold on SortedSet
- What is the difference between adding a dependency via libraryDependencies versus an sbt plugin?
- Renaming the Option, Some, and None sealed types to a domain-specific language?
- Overcoming Scala Type Erasure For Function Argument of Higher-Order Function
- Is there a way to get a warning when a Scala Value Class needs to become instantiated?
- how to check errors happening inside scala interpreter programatically
- Applying partial functions where defined and a different function where not
- How to union 2 dataframe without creating additional rows?
- Scala pattern-matching confusion
- Why is Scala building its own ForkJoinPool instead of using java.util.concurrent.ForkJoinPool#commonPool?