Open ekrich opened 1 year ago
For the record, I think of an API like:
def writeToFile[A](path: Path, contents: IterableOnce[A])(formatLine: A => String): Try[Unit]
Maybe, even something like Sink
to complement Source
trait Sink extends AutoClosable {
def write(str: String): Unit
final def writeLine(str:): Unit = {
write(str)
write(System.lineSeparator)
}
final def writeAllFormat[A](data: IterableOnce[A])(formatLine: A => String): Unit = {
data.foreach(a => writeLine(formatLine(a)))
}
/** Uses toString */
final def writeAll[A](data: IterableOnce[A]): Unit = {
wrtieAllFormat(data)(_.toString)
}
def close(): Unit
}
object Sink {
def fromFile(path: Path): Sink
}
Which then could be used with Using
.
I know we are not wanting a big Scala library and such but this is where we could make some things easier for beginners or just for easy scripting. I was just doing some analysis on the sbt build for Scala Native and wrote the following. I think we could have something even easier than Python.