##- # Author: Brian Tiffin # Dedicated to the public domain # # Date: November 2016 # Modified: 2016-11-22/15:33-0500 ##+ # # table-operations.icn, some simple table() examples # link ximage procedure main() T1 := table() # the default default is &null T2 := table(0) # default element is 0 # tables are mutable, key value pair structures # accessing an unknown key returns the table default value # setting an unknown key inserts the key-value pair write("T1[\"unknown\"]: ", image(T1["unknown"])) write("T2[\"unknown\"]: ", image(T2["unknown"])) T1["unknown"] := "now known" write("T1[\"unknown\"]: ", image(T1["unknown"])) # T2 elements default to 0, math is ok for unknown elements write("T2[\"unknown\"] + 42: ", image(T2["unknown"] + 42)) write("\nximages:") write(ximage(T1)) write(ximage(T2)) # Access is by square bracket operator # keys can be any type, values can be any type T2[3] := "42" T2["3"] := 42 write("\nElements:\nT2[3]: ", image(T2[3])) write("T2[\"3\"]: ", image(T2["3"])) # Size, note: T2["unknown"] was not assigned during the computation write("size of T1: ", *T1) write("size of T2: ", *T2) # element generator gives values write("\nT2 values") every write(image(!T2)) # key() function, generates the keys write("\nT2 keys") every write(image(key(T2))) write(ximage(T2)) # Membership if member(T2, 3) then write("\ninteger 3 is a key in T2") # removal v := delete(T2, 3) write("\ndelete T2[3], return value is: ", type(v)) # membership test will now fail, no output written if member(T2, 3) then write("\ninteger 3 is a key in T2") write("T2 keys after delete") every write(image(key(T2))) # table values can also be functions fT := table() fT["add"] := adder operation := "add" # add two values, invoked from table key write("\noperation ", operation, " returns ", fT[operation](1,2)) write(ximage(fT)) end # # adder function # procedure adder(a,b) return a + b end