score:1
Accepted answer
If you're intent on applying a function only to specific entries in a list, you could:
list.zipWithIndex.flatMap {
case (entry, idx) if (idx == 2) =>
entry.split(" ")
case (entry, idx) => List(entry)
}
This could be generalized a little more by going along the lines of:
// Add variance, curry, etc. as needed...
def flatMapOnly[A](list: List[A], offsets: Set[Int], f: A => Seq[A]): List[A] =
list.zipWithIndex.flatMap {
case (entry, idx) if (offsets.contains(idx)) =>
f(entry)
case (entry, _) => List(entry)
}
scala> flatMapOnly(List("hi", "bye", "see ya later"), Set(1, 2), { str: String => str.split(" ").toList })
res39: List[String] = List(hi, bye, see, ya, later)
score:0
list.flatMap {
case x@"see ya later" => x.split(" ")
case x => Seq(x)
}
Source: stackoverflow.com
Related Query
- Scala: Run function on only one member of a list
- Apply function to one element only in list or array in Scala
- Run only one package test cases scala
- Run a function in scala with a list as input
- Why is Scala unable to infer the type of an anonymous function when there is method overloading and only one method takes a function?
- scala function returning only single value of list but not all why?
- How best to keep a cached list of member fields, one each for a family of case classes in Scala
- Scala subclass field hiding superclass var only within member function
- 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
- Run a function periodically in Scala
- How to remove an item from a list in Scala having only its index?
- Applying an argument list to curried function using foldLeft in Scala
- Scala List function for grouping consecutive identical elements
- Unwrapping a list or map as function arguments in Scala
- Scala sort one list according to the values in another one
- Scala projections in Slick for only one column
- Scala error: value sort is not a member of List
- Scala Map, ambiguity between tuple and function argument list
- scala override protected member function
- Scala - evaluate function calls sequentially until one return
- Generate a List with values generated by function in Scala
- Scala polymorphic function for filtering an input List of Either
- How can one list all csv files in an HDFS location within the Spark Scala shell?
- Remove one element from Scala List
- Different brackets style on Scala function definition parameter list
- Transform only the first element of a Scala list
- Scala group consecutive elements in list where function is true
- Copy elements from one list to another in Scala
- Java function with "..." in parameter list from Scala
- Scala - map function - Only returned last element of a Map
More Query from same tag
- bounded generics in Scala (as <E extends MyClass> in Java )
- Help with generic function in Scala
- nondeterminism.njoin: maxQueued and prefetching
- Accessing variables from a parent class when using an instance of the child does not seem to be working
- How to store pure scala code
- Two methods in one scala
- Error message with Scala AKKA Actor
- groupBy Id and get multiple records for multiple columns in scala
- Tagged Type (@@) Comparison in Scalaz
- Spring-Scala bean with dynamic constructor value
- merge `Tree` and `List[Tree]` in scala compile-time reflection
- For each RDD in a DStream how do I convert this to an array or some other typical Java data type?
- Understanding Scala implicit conversion and generic type inference
- Is it possible to extend the Scala compiler to infer return types of recursive methods?
- Contravariant method arguments in Scala
- Find the duplicate values of an array column in the dataframe in Scala
- Mockito doReturn: ambiguous reference to overloaded definition
- How to get more meaningful failure message / assert with Selenium WebDriverWait?
- little problem on code for finding substring within string scala
- why = is optional in def in Scala
- How to develop and run spark scala from vscode using sbt or Metals
- In scala, how to turn object's values into Map[String, String]?
- Big Int in scala
- Filter DataFrame by Float column values in Scala
- Counting layers in tree like structure
- Slick: Return inserted row with auto increment id
- How do I read from a json-lines file into a Dataset with an immutable.HashMap?
- Scala Json4s CustomKeySerializer
- Lift tracking a logged in user
- Gatling proxy configuration