Up to Main Index                         Up to Journal for September, 2024

                  JOURNAL FOR WEDNESDAY 11TH SEPTEMBER, 2024
______________________________________________________________________________

SUBJECT: On indexing arrays, maps and strings
   DATE: Wed 11 Sep 19:52:17 BST 2024

The Institution of Analysts and Programmers had their monthly virtual coffee
meet-up last week. We chatted, conversation turned to AI, I showed some parts
of my AI presentation. They seemed quite interested and suggested I presented
it as a webinar for the IAP. My presentation on AI still gathers dust, quietly
waiting for the next company meeting. I might take up the offer to make it a
webinar…

Over the weekend I intended to write about progress on Mote. Instead my head
was buried in code working on an interesting, yet annoying, issue. I ended up
throwing out a lot of code and reimplemented how Mote handles array, map and
string indexing. It’s okay, it was mostly really crufty code I was so happy to
get rid of. Deleted code is debugged code ;)

One side effect of my efforts is that Mote can now handle parallel assignment
with element indexing. Yay! :) For example:


    a = []int 3 5
    println a               # displays "[3, 5]"
    a[0] a[1] ≡ a[1] a[0]   # swap array element values
    println a               # displays "[5, 3]"


There were a few casualties along the way :(

There is no automatic interpretation of single or parallel assignment. Before
you could specify ‘=’ or ‘≡’ and the compiler would try and work out which was
needed. Now you need to explicitly specify ‘=’ or ‘≡’ in code. As some people
have trouble typing ‘≡’ for parallel assignment, I borrowed ‘<-’ from F#, R, S
and OCaml, as an alias.

Parallel assignment can be used in the singular context. For example it is
perfectly reasonable to write any of the following for single assignment:


    x = 1
    x ≡ 1
    x <- 1


However, single assignment is more efficient than parallel assignment for the
singular case.

Another casualty were the ‘delete’ and ‘exists’ built-ins. I have been try
very hard to keep the syntax of the two the same. However, with the changes to
indexes this wasn’t possible. Well… it was, but it was heading back to ugly
crufty code again :/ The syntax is now:


    delete var key   # var can be an array, map or string.
    exists var       # var can be a simple var
    exists var[key]  # var can be an array, map or string


An example showing ‘delete’ and ‘exists’ usage, plus some more indexing:


    im = [int] 1 "ant", 2 "bat", 3 "cat"
    println "     map: " im
    println "   im[1]? " exists im[1]
    println "   im[9]? " exists im[9]
    println "im[2][0]? " exists im[2][0]
    println "im[2][0]= " im[2][0]
    delete  im 2
    delete  im[3] 1
    println "     map: " im


When run, the above code displays:


         map: [1 "ant", 2 "bat", 3 "cat"]
       im[1]? true
       im[9]? false
    im[2][0]? true
    im[2][0]= b
         map: [1 "ant", 3 "ct"]


Manipulating strings via indexes, which I really like, is currently broken and
needs some addition work. For example, the following should be possible:


         s     = "abc"      # s is now "abc"
         s[1]  = "B"        # s is now "aBc"
    s[0] s[1]  = s[1] s[0]  # s is now "Bac"
         s[1]  = "ZZZ"      # s is now "BZZZc"
         s[1] += "YYY"      # s is now "BZYYYZZc"
         s[]   = "!!!"      # s is now "BZYYYZZc!!!"


Currently only the appending (using s[] = "!!!") is working. In order to get
the other forms working I need to do a lot of work picking through string
instances and changing them over to []rune. This will have the added benefit
of speeding up many string functions that use temporary []rune and will reduce
allocations and garbage.

Once these changes are finished, adding support for index ranges for slicing
should be relatively easy.

For now, I need to commit all this work and then get string indexing working
properly.

--
Diddymus


  Up to Main Index                         Up to Journal for September, 2024