0

Imagine I have a list[list[object]] and I wanted to get a list[list[object.field]]

How would i be able to do this through functional programming

as in if val list = list[list[object]] i tried

val newList = list(_)(_1.map(object.field))

but this gave me an error and I am confused

I just started functional programming and scala so there might be something completely illogical with this statement

1
  • 1
    What is list(_) supposed to mean? And what is _1 ? Once you get past these two, the compiler error message should become more informative. Commented Nov 13, 2019 at 11:07

1 Answer 1

5

You need to use map as following

  case class Obj(field: String)
  val list = List[List[Obj]]()

  val fields: List[List[String]] = list.map(_.map(_.field))

or same as

  val fields: List[List[String]] = list.map(innerList => innerList.map(obj => obj.field))

or if you want to have a flattened list of fields

  val fields: List[String] = list.flatMap(_.map(_.field))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.