Doing filter inside a map to avoid filtering later

I have a case class like this
case class LocalBackup(id:String, sizeMB:Double)

I want to create a List[LocalBackup]. I do it as given below, but the problem is, I have to do a filter in the end to filter out the objects which were created but the size on disk was Zero. The problem is that if in “createLocalBackupObject, I do not return anything sometimes, this code will not work”. So the only choice is to return Option type? Even then I will return None for some entries and I still have to call flatMap on it. Will that be better? Or below is just fine?

    val fullPath = f.getPath + "/backup"
    val sizeInMB = new File(fullPath).length().toFloat / 1000000.toFloat
    BigDecimal(sizeInMB).setScale(2, BigDecimal.RoundingMode.HALF_UP).toFloat

 ``` //Any invalid path will return size 0 and we will filter it out
  def listLocalBackups():List[LocalBackup] = {"Create local backup for node...")
    val f = new File("/opt/cisco/backups")
    f.listFiles().withFilter(_.isDirectory).map { dir => {
    }.filter(_.sizeMB != 0.0).toList

You may want to wrap methods that may fail in Try or use Try(_).toOption. Incidentally, please wrap code in triple quotes while posting


thanks, but I am unable to understand your suggestion about Try... Can you elaborate a bit?

TIL File.length is zero for various reasons a non-zero length isn’t returned.

If using an operation that could throw an exception like Files.readAttributes, then the usual idiom is Try(readAttributes).toOption.

Then your question is whether is worse than list.flatMap(op.toOption).

If it’s a huge list, then flatMap avoids building a huge intermediate collection, at the small cost of short-lived objects.

It seems odd to me to keep the length as a float; formatting for display is a separate concern.

Given, what I understood. I have made my code look like below now. Is that a better approach and will take care of the scenarios you pointed out? Also, when I am pasting the code here, the indentation is getting lost. I guess I need to read the guide on this website somewhere on how to format the code
Let me know what your thoughts are on below code
def listLocalBackups(): List[LocalBackup] = {"Fetch") Try { val f = new File("/test1/test2/backups") f.listFiles().withFilter(_.isDirectory).flatMap { dir => { val ret = createLocalBackupObject(dir) if (ret.sizeMB > 0) Some(ret) else None } }.toList } match { case Success(localBackups) => localBackups case Failure(f) => logger.error("Error message...."); Nil } }