vendredi 30 mars 2018

Better way to write nested if-else statements

What is a most Scala way to write the following piece of logic?

def collatzAlgorithm(target: Int): Int = {
    if (target % 2 == 0) {
      val new_target = target / 2
      if (new_target == 1) {
        new_target
      } else {
        if (new_target % 2 != 0) {
          new_target * 3 + 1
        } else {
          new_target
        }
      }
    } else {
      target
    }
  }

Thanks!!!!

Aucun commentaire:

Enregistrer un commentaire