2013-11-03 16 views
2

我有一些代碼,這是否:習慣scala處理文件A或文件B?

for d in directories: 
    if d/f1 exists: process d/f1 
    else if d/f2 exists: process d/f2 
    else skip 

其中,「過程」,在這種情況下是:

read all the lines and save them for later use 

什麼是一些慣用的方式在Scala中做到這一點?

+1

你描述的不是它,它可能是f1或f2或兩者都沒有 – Ashalynd

+0

謝謝 - 編輯僞代碼來澄清。 –

回答

2

應該是這樣的?

fileList.filter(Files.exists(_)).forEach(process(_)) 
+0

這將處理所有不是原始代碼意義的現有文件。 fileList.find(Files.exists(_)).fold(()){process(_)}保留原文。 –

相關問題