ScalaでRubyのFile.joinみたいなのを実装
こんな感じで。
/** * 与えられた複数の文字列からファイルパスを作成する。 */ def createPath(pathes: String*): String = { import java.io.File pathes.foldLeft(""){(s, p) => (new File(s, p)).getAbsolutePath()} // ("" /: pathes){(s, p) => (new File(s, p)).getAbsolutePath()} }
foldLeftと/:、どちらでもいいのですが、どちらが主流なんでしょうね。
なんとなく自分はfoldLeft派。
ちなみに実行するとこうなります。
scala> createPath("tmp","hoge","moga","piyo.csv") res87: String = /tmp/hoge/moga/piyo.csv