Tuesday, October 18, 2016

lambda is a macro expansion

Look at this code, generates a list of (line,text) pairs. See how I define a lambda to define simple access to the list by index?  I could do that many other ways, but the lambda as a macro does it on the spot.  So, whenever I need the line text for the nth break point, it is simply: BLine(n).  No fuss with adjoining ][ symbols or worry about who's on first.

A lot of geeks didn;t get this, they started the biz after macro expansions flittered away.  But the python folks got it, a bunch of old 'c' defs users.  The lambda operators nest just fine, no worry. But these are better macros, they expand at run time.

BreakPoints = []
BreakPoints.append((1,"Dummy"))


BString = lambda i : BreakPoints[i][1]
BLine =  lambda i : BreakPoints[i][0]

print(str(BreakPoints))
print(BLine(0),BString(0),len(BreakPoints))

I use these three all the time:

BString = lambda i : BreakPoints[i][1]
BLine =  lambda i : BreakPoints[i][0]
SIndex = lambda j : ("%d.0")%(j)

The last one makes a tkinter compatible index  to line j in the text box.  Microsoft usesthis, or used to, a lot.

A c compiler goes through a free macro expansion phase,then restarts.  The interpreter is a one pass shot, so it expands macros in run time.  A really great idea, and an idea that will keep me using the language.

No comments: