I recently wrote the following bit of Scala:
val f: File = ... // pretend this file came from somewhere
val foo = toFoo(io.Source.fromFile(f).mkString)
I really didn't like the way this flowed. To understand what's happening, you have to start with f
in the middle, read left to fromFile
, read right to mkString
, read left again to toFoo
. Ugh.
Especially after getting used to functional transformations of sequences, this is difficult to read. My next attempt looks like this:
val foo = Some(f)
.map(io.Source.fromFile)
.map(_.mkString)
.map(toFoo)
.get
I like the flow of this much better. You can see what happens Is this a good use of the Option
class? Or am I abusing it? Is there a better pattern that I can use to achieve the same flow?