#2895 Enhanced For loop in Fantom?

chikega Sun 27 Aug

Hi everyone, I'm learning Fantom. Does Fantom support enhanced for-loop (or foreach) syntax introduced in Java 5

// print array elements in Java

class Main {
  public static void main(String[] args) {

    int[] numbers = {3, 9, 5, -5};

    for (int number: numbers) {
      System.out.println(number);
    }
  }
}

Ok, I may have spoken to soon, it looks like Closures are the primary mechanism for iteration in Fantom :)

class Main {

    public static Void main() {

        numbers := [3, 9, 5, -5];
        numbers.each |Int val, Int index| { echo("$index = $val")}
    }
}

SlimerDude Sun 27 Aug

Yep, you got it! :)

chikega Sun 27 Aug

Thx @SlimerDude, I'm enjoying learning Fantom so far. As an intermediate-level programmer and as part of my learning process, I'm looking over the Fantom code examples on Rosetta code. I also attempt to translate from other languages as an exercise. Currently, I'm translating a Kotlin code example under the Determine if a string is numeric category. You can probably see why I was asking about the enhanced for loop.

// Kotlin
fun isNumeric(input: String): Boolean =
    try {
        input.toDouble()
        true
    } catch(e: NumberFormatException) {
        false
    }

fun main() {
    val inputs = arrayOf("152", "-3.141", "Foo123", "-0", "456bar", "1.0E10")
    for (input in inputs) println("$input is ${if (isNumeric(input)) "numeric" else "not numeric"}")
}

I realize a sample code already exists for Fantom in this category. But there seems to be room for another type of example. :)

class Main {

    static Void main() {
        inputs := ["152", "-3.141", "Foo123", "-0", "456bar", "1.0E10"]
        inputs.each |Str input| { echo("$input \tis " + (isNumeric(input) ? "numeric" : "not numeric"))}
    }

    static Bool isNumeric(Str input) {
        try {
            input.toFloat
            return true
        }
        catch(Err e) {
            return false
        }
    }
}

Output:

152     is numeric
-3.141  is numeric
Foo123  is not numeric
-0      is numeric
456bar  is not numeric
1.0E10  is numeric

SlimerDude Sun 27 Aug

Hi @chikega,

Indeed your isNumeric() method translates very well to other languages.

Although I find the use of the checked argument (where available) to be very idiomatic Fantom, and usually cuts down the amount of code you need to write.

For example, isNumeric() could be written like this:

static Bool isNumeric(Str input) {
    input.toFloat(false) != null
}

chikega Sun 27 Aug

Hi @SlimerDude, Thank you for the comment and thank you for the code snippet! I just read something in Jeff Szuhay's Learn C Programming book that really reminds me of your code snippet. He started off with this bit of code:

bool isEven( int num )  {
  bool isEven = false;  // Initialize with assumption that 
                        // it's not false.
  if( (num % 2) == 0 )
    isEven = true;
  return isEven;
}

Then he whittled it down to this:

bool isEven ( int num)  {
  if( num % 2 ) 
    return false;
  return true;
}

Then he finally whittled it down to its most condensed form which reminds me of what you posted. That's so awesome! :D

bool isEven ( int num)  {
  return !(num % 2 ) 
}

Login or Signup to reply.