Non-anonymous function arguments

cjm_usenet at yahoo.co.nz cjm_usenet at yahoo.co.nz
Fri Apr 29 01:40:45 EDT 2005


I had an idea for passing functions as arguments:
Allow a block syntax (like with class definition)
for keyword arguments, attached to a statement
that includes a function call but doesn't need
the block for something else (like loops and ifs).
Apologies for the contrived examples.

  squares = map(*):
    def function(x): return x*x
    list=[1,2,3]

  def odds_and_evens(list, on_odd, on_even):
    for i in list:
      if (i % 2 == 0):
        on_even(i)
      else:
        on_odd(i)

  odds_and_evens([1,2,3,4,5],*):
    def on_odd(i):
      print (i/2) " * 2 + 1"
    def on_even(i):
      print "2 * " (i/2)

  some_numbers=filter(*):
    sequence=[1,2,3,4,5]
    def function(x):
      return x > 2

  strange_list=sorted(["hello","world",7],*):
    def key(x):
      if (x==7):
        return "SEVEN"
      else:
        return upper(x)

Unlike with lambda, the functions have names.
The marker inside the function call helps make
it obvious where the keywords go, especially
if the statement contains multiple function
calls.
There could be a keyword before the colon:

  some_numbers=filter(*) using:
    sequence=[1,2,3,4,5]
    def function(x):
      return x > 2

The marker is an asterisk because it's like
doing footnotes, alternatives are a plus sign
and multiple asterisks.
-- 
Caspian Maclean




More information about the Python-list mailing list