My Icon programs often are written as a series of filters on objects in a stream. The filters do one object look-ahead on the stream with a read/pushback sequence, implemented something like this:
global push_back_o procedure next_o() local o if \push_back_o then { o := push_back_o push_back_o := &null return o } else return read_o() end # next_o
It eventually occurred to me that I could use the local variable's&null
initialization to rewrite the then part of the if expression as
which lets the procedure body collapse toreturn o :=: push_back_o
return ((\push_back_o & (o :=: push_back_o)) | read_o())
and again to
return (o :=: \push_back_o) | read_o()
I wanted to write an Icon procedure to check if a string has precisely 22 characters (the size of the Hebrew alphabet) and no duplicate characters, so I wrote the following:
The procedure worked fine, but I said: "That isn't an Icon procedure. It's a thinly disguised Pascal function. Now write an Icon procedure." So I forsook "if mouse in hole" and wrote:procedure checkstring(abc) local cs, current if *abc ~= 22 then fail # check length cs := ' ' # initialize cset abc ? every 1 to 22 do { current := move(1) # select a char if cs ** current ~=== ' ' then fail # already a member cs ++:= current } # char is ok return end
More Icon constructs, but no better really. Then I wrote:procedure checkstring2(abc) local t, current if *abc ~= 22 then fail t := table() abc ? every 1 to 22 do { current := move(1) /t[current] | fail t[current] := 1 } return end
Now that's an Icon procedure.procedure checkstring3(abc) return *abc = *cset(abc) = 22 end