score:1
@Aavik please read How does yield work? article.
Desugared version of your example will look like:
case class Book(title: String, authors: List[String])
val books: List[Book] = List(
Book(title = "kids watching", authors = List("Aavik","Arjun"))
)
books.flatMap { (book: Book) =>
book.authors.withFilter { (name: String) =>
name.startsWith("Aavik")
}.map { _ =>
book.title
}
}
As you see map { _ => book.title }
has the same scope as book
variable. You can run this example in Scastie.
Ammonite REPL desugar can help to investigate how does Scala translate for-comprehensions.
score:0
if I got your question right, in the above example i.e
for{
book <- books
name <- book.authors
if name.startsWith("Aavik")
} yield book.title
This will actually be converted to:
books.flatMap(((book) => book.authors.withFilter(((name) => name.startsWith("Aavik"))).map(((name) => book.title))))
So, as you can see we have book that is the input to the flatMap and it can be accessed anywhere inside it. So there is no issue in accessing book.title inside map as map is enclosed inside the flatMap.
Let me know if you want more clarity.
Source: stackoverflow.com
Related Query
- Scala: Converting for expression to higher order function
- Scala For loop to Higher Order Function
- Implementing a higher order function that performs currying in scala
- Higher order functions with Scala Slick for DRY goodness
- Confusing Scala Higher order function call syntax
- Scala higher order function that filters even numbers
- Recursive higher order function type in Scala 3
- Scala Higher Order Function Little Confused
- function literals in scala for expression
- how to calculate factorial of list using higher order function in scala
- Scala - Perfect Number usage in higher order function - anything wrong?
- Custom high order function for Scala sequence via implicits?
- Error for parentheses in higher order function definitions (Scala)
- Higher order function in scala
- How to use @unchecked for an Option in a higher order function
- Scala higher order function issue
- Scala - Make signature of function parameter f of higher order function g dependent on varars of g
- In Scala how to curry existing function with single argument and pass to a higher order function?
- Scala type classes for transform on option using higher order functions
- Workflow of the Higher Order Function in Scala
- Refactoring to higher order function in Scala
- arguments to higher order function weirdness in scala
- Is there any relation between Curried function/ Partially applied function and Higher order functions in scala
- How to access to the parameter of the higher order function in scala
- Is there any way to replace nested For loop with Higher order methods in scala
- Scala underscore - ERROR: missing parameter type for expanded function
- Is there any analog for Scala 'zip' function in Groovy?
- Why does Scala not have a return/unit function defined for each monad (in contrast to Haskell)?
- Scala unexpectedly not being able to ascertain type for expanded function
- In Scala, is there a pre-existing library function for converting exceptions to Options?
More Query from same tag
- Functions / methods in Scala. How does this work?
- Scala RDD matching with similar wording
- Scala. Sangria.Print json schema
- Cannot Connect to Docker postgres container - Scala
- Copying a circular buffer to a larger buffer while preserving contents and modulus indices
- typesafe activator dependency error
- Raw extraction of frames from a movie
- Maven Gatling 3.0.2 - NoSuchMethodError
- Main class not found error while creating Maven Project in Scala IDE
- Seq, SeqLike, GenSeq or GenSeqLike?
- Unable to write multiple queries in foreachbatch
- In Scala, why do I get this "polymorphic expression cannot be instantiated to expected type"?
- deleting/updating spark dataframe rows based on values in other columns
- Returning all elements from a Map[List()] in Scala
- Spark installation failed on cygwin
- Converting a JSON object to a value list of a particular key in scala (play)
- sbt-assembly: How do I include the static files in src/main/webapp
- java.lang.NoClassDefFoundError: scala/Product$class for gradle JUNIT test
- How to iterate and add elements to a list in scala without changing state
- Transpose Specific Columns and Rows in Dataframe based on Condition in Spark Scala
- How to access variables declared before try catch block in a catch block in spark scala
- How to covert nested struct into nested map for Spark DataFrame
- Casting cassandra timestamp column as timeuuid
- How to convert a Java Stream to a Scala Stream?
- How to swap implicit JSON formatter for two controller endpoints in Scala Play
- Play framework 2.5 logs `?` question marks instead of line numbers
- Scala / Java Sandbox for untrusted code
- How to create a xml file per records in spark scala
- java.lang.ClassNotFoundException: scala.reflect.api.TypeCreator
- Scala Check/Scala Test: Compose Generators