These methods perform transformations on their input. They'll be tied together in different ways separately. Look for the blog post at blog.jessitron.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://github.com/jessitron/BlogCode/blob/master/optionAsFlow.scala | |
object transformers { | |
def openFile(n: String) : Option[java.io.File] = { | |
val f = new java.io.File(n) // catch IOException | |
if (f.exists) | |
Some(f) | |
else | |
None | |
} | |
def readFirstLine (f : java.io.File) : Option[String] = { | |
val source = io.Source.fromFile(f) | |
try { | |
if (!source.hasNext) | |
None | |
else | |
Some(source.getLines.next) | |
} finally { | |
source.close | |
} | |
} | |
def parseLine(line : String) : Option[SecretMessage] = { | |
val ExpectedFormat = "The secret message is '(.*)'".r | |
line match { | |
case ExpectedFormat(secretMessage) => Some(SecretMessage(secretMessage)) | |
case _ => None | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment