Paul
Paul

Reputation: 157

How to make a function to do the mapping

I am using source to process data from a csv file. I am trying to refactor some of the code because when I save it I am using double map on the same line

   _contents = source.getLines().map(line => format.extract(line)).map(fields => factory.newItem(fields)).toList

the line above works but I am trying to add a function so

 _contents = source.getLines().map(line => mapData(line)).toList




   def mapData(line: String):Unit = {
    val data = format.extract(line)
    data.map(fields => factory.newItem(fields))
  }

this is what I have so far. problem is when I hover over fields in '''factory.newItem(fields)''' I get Type mismatch, expected:Array[String], actual: String I understand what the issue is I just cant think of how to fix it

problem is when I hover over fields in '''factory.newItem(fields)''' I get Type mismatch, expected:Array[String], actual: String I understand what the issue is I just cant think of how to fix it

Upvotes: 0

Views: 65

Answers (1)

Bertrand
Bertrand

Reputation: 1826

What you're trying to do is:

_contents = source
  .getLines()
  .map(line => factory.newItem(format.extract(line)))
  .toList

Upvotes: 3

Related Questions