Up to Main Index                            Up to Journal for August, 2023

                    JOURNAL FOR THURSDAY 17TH AUGUST, 2023
______________________________________________________________________________

SUBJECT: Performance improvements and range/next loops
   DATE: Thu 17 Aug 19:14:04 BST 2023

Yesterday I surprised people with an impromptu release of Mere ICE v0.0.5 :)

Quite a quick turn around with some big features in only ten days…

Well, I’m not letting up. I’ve managed to improve the performance of the new
if-elif-else-fi blocks and for/next loops. Looks like that 5% performance hit
is getting smaller all the time. Speaking of for/next loops…

I just got this working:


  animals = [string] "a" "ant", "b" "bat", "c" "cat"

  range letter animal animals
    printf "%s is for %s\n" letter animal
  next


Is that a new range statement? Yes it is :) That code produces:


  a is for ant
  b is for bat
  c is for cat


How about nested ranges? No problem:


  data = [string](
      "animals" []string("ant" "bat" "cat"),
      "colours" [int](1 "aqua", 2 "blue" 3, "charcoal"),
      "fruits"  [string]("green" "apple", "yellow" "banana", "red" "cherry"),
      "numbers" []int(3 5 7),
    )

  range kd vd data
    printf "%8s = " kd
    range k v vd
      printf "%6v: %-8v" k v
    next
    println
  next


That code produces:


   animals =      0: ant          1: bat          2: cat
   colours =      1: aqua         2: blue         3: charcoal
    fruits =  green: apple      red: cherry  yellow: banana
   numbers =      0: 3            1: 5            2: 7


You can use break and continue statements like a normal for/next loop. With a
range/next loop array and map variables, literals or function call results can
be iterated — as you would expect. For example these also work:


  range letter animal [string] "a" "ant", "b" "bat", "c" "cat"
    printf "%s is for %s\n" letter animal
  next


  range letter animal call data
    printf "%s is for %s\n" letter animal
  next

  data: func
    return [string] "a" "ant", "b" "bat", "c" "cat"
  endfunc


A range/next loop sort of works with strings. For example:


  range x y "♠♣♥♦"     // Produces:   ♠ <nil>
    println x " " y    //             ♣ <nil>
  next                 //             ♥ <nil>
                       //             ♦ <nil>


So don’t use ‘y’ in the above and it’s okay. To be honest I hadn’t actually
coded for strings yet. The first time I tried strings I was surprised it
worked as well as it did…

It’s close, but not ready for prime-time release yet. There is another minor
todo, I need to add some clean-up after the range/next loop. Then I need to
write tests and document range. Give me a few days ;)

--
Diddymus


  Up to Main Index                            Up to Journal for August, 2023