1

I'm reading a file line by line, replacing substring "replace" with substring "replacment". Once string manipulation is complete I want to insert each line into a list.

def importFile(replaceString:String, filePath:String, replacement:String)= {
  val fileLineList = io.Source.fromURL(getClass.getResource(filePath))
    .getLines
    .foreach(line => {line.replace(replaceString,replacement)}.toList)
  print(fileLineList)
}

When I call the function all that is returned is:

()

Any Ideas, ?

user2313658
  • 41
  • 1
  • 2
  • 7
  • 2
    What do you intend to return? The last statement is a `print`, which returns `()`. If you mean for it to return `fileLineList` you need to add a statement saying that. Note that setting a `val` will _not_ also give you the contents of that `val`. You have to explicitly ask for it (or just do the computation and not assign it to a `val` but let it be the last expression executed). – Rex Kerr Jun 13 '14 at 00:42
  • @rex kerr The println was for debugging. I want it to return fileLineList. However, when I add the return statement and return type(List[String]), I get an error stating "Expression of type Unit doesn't conform to expected type List[String] " – user2313658 Jun 13 '14 at 00:47

1 Answers1

2

If you want to return your list of strings, you could do one of the two things:

def importFile(replaceString:String, filePath:String, replacement:String)= {
  io.Source.fromURL(getClass.getResource(filePath))
    .getLines
    .map(_.replace(replaceString,replacement))
}

or

def importFile(replaceString:String, filePath:String, replacement:String)= {
  val fileLineList = io.Source.fromURL(getClass.getResource(filePath))
    .getLines
    .map(_.replace(replaceString,replacement))
  print(fileLineList)
  fileLineList
}

The first variant will not print anything, but will return the result (all lines from file after replacement). The second variant will print the replaced version and then return it.

In general, in Scala the result of the function is its last statement. Keep in mind that the statement like:

val myValue = 5

will not return anything (its type is Unit), whereas

myValue

(if it was defined before) will specify the result as whatever is stored in myValue.

the .map(_.replace(replaceString,replacement)) part should transform each of the original lines , using replace. _ is syntactic sugar for

.map(x => x.replace(replaceString, replacement))

which can be also written as

.map{x => x.replace(replaceString, replacement)}

but in this simple case it's not necessary. Curlies would make sense if you had a mapping function that consisted of several statements, for example:

.map{x => 
    val derivedValue = someMethod(x)
    derivedValue.replace(replaceString, replacement)
 }

Most important part is the difference between .map and .foreach:

.map transforms the original sequence into the new sequence (according to the mapping function) and returns this sequence (in your case, list of strings).

.foreach iterates over the given sequence and performs the specified operations over every entry in the sequence, but it does not return anything - it's return type is Unit.

(Check Scaladoc for List for more information about these and other functions: http://www.scala-lang.org/api/2.10.3/index.html#scala.collection.immutable.List )

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
  • still returning type Unit. Is it because it keeps trying to overwrite the list everytime foreach is called? – user2313658 Jun 13 '14 at 01:04
  • ah, wait, I havent paid attention to that part. try the updated version – Ashalynd Jun 13 '14 at 01:11
  • I apologize for not being more clear but I'm working with Gatling. Correct me if Im wrong but doesn't map work asynchronously? I need the list to be created before the program continues. But I can make the program wait for the list to be created by using the Await? – user2313658 Jun 13 '14 at 01:27
  • No, not at all, see my updated comments. In this particular case you work with the List of strings, and .map is its own method. – Ashalynd Jun 13 '14 at 01:29
  • Good luck further! Gatling looks like an interesting tool btw. – Ashalynd Jun 13 '14 at 01:39