pre-PEP: Simple Thunks

Bengt Richter bokr at oz.net
Sun Apr 17 23:10:14 EDT 2005


On Sun, 17 Apr 2005 15:32:56 -0700, Brian Sabbey <sabbey at u.washington.edu> wrote:

>Bengt Richter wrote:
>> Hm, one thing my syntax does, I just noticed, is allow you
>> to pass several thunks to a thunk-accepter, if desired, e.g.,
>> (parenthesizing this time, rather than ending ():<suite> with
>> dedented comma)
>>
>>    each(
>>        ((i):  # normal thunk
>>            print i),
>>        ((j):  # alternative thunk
>>            rejectlist.append(j)),
>>        [1,2])
>>
>> <snip>
>>
>
>I see that it might be nice to be able to use multiple thunks, and to be 
>able to specify the positions in the argument list of thunks, but I think 
>allowing a suite inside parentheses is pretty ugly.  One reason is that it 
>is difficult to see where the suite ends and where the argument list 
>begins again.  I'm not sure even what the syntax would be exactly.  I 
>suppose the suite would always have to be inside its own parentheses? 
>Also, you wind up with these closing parentheses far away from their 
>corresponding open parentheses, which is also not pretty.  It's getting 
>too Lisp-like for my tastes.
>
Having had a past love affair (or at least fling) with scheme,
that doesn't bother me so much ;-)

The "where" specifier syntax might help, e.g.,
      
      each(thk1, thk2, [1, 2]) where:
           thk1 = (i):  # normal thunk
              print i
           thk2 = (j):  # alternative thunk
              rejectlist.append(j)

This still uses my (<arglist>):<suite> expression for thunks
but they don't need to be parenthesized, because their suites
terminate with normal dedenting under the where: suite

I.e., the 'p' of 'print i' is the left edge of the (i): suite
and thus the 't' of 'thk2 = ...' ends the (i): suite. The (j): suite
left edge is at the 'r' of 'rejectlist'... so anything to the left
of that (excluding comments) will end the (j): suite, like normal
indentation.

I could have used dedent termination in the previous example too
by moving the commas around to let them trigger dedent level out of
the preceding suite, e.g., with args evaluated in place again:

      each((i):                     # normal thunk
              print i
          ,(j):                     # alternative thunk
              rejectlist.append(j)
          ,[1,2])

Of course, if you want lispy, the above simple thunks can be done in a oneliner:

      each(((i):print i), ((j):rejectlist.append(j)), [1,2])

I personally like the power of being able to write that, but given a clean sugar
alternative, I would use it. But if it's an exclusive-or choice, I'll take primitives
over sugar, because sugar never covers all the useful combinations of primitives that
will turn up later.

Regards,
Bengt Richter



More information about the Python-list mailing list