[Edu-sig] The Great Computer Language Shootout

Kirby Urner pdx4d@teleport.com
Sun, 03 Jun 2001 11:33:43 -0700


>How about:
>
>  def compose(f,g):
>      global _f, _g
>      _f = f
>      _g = g
>      return lambda x: _f(_g(x))
>
>  if __name__ == '__main__':
>      from math import *
>      print compose(sin, sqrt)(3)
>      print sin(sqrt(3))
>
>    0.98702664499
>    0.98702664499
>
>
>cheers, Luby 

The problem with this is if I want to save successive
composed functions, the 2nd will corrupt the 1st, e.g.

  >> def f(x): return x*x
  
  >> def g(x): return x+2

  >> def r(x): return r-100

  >> h = compose(f,g)
  >> h(10)
  144
  >> z = compose(f,r)
  >> h(10)
  8100

The compose method in proto2 has the advantage of not depending
on global definitions of the original functions remaining elsewhere 
in memory, i.e. you can go:

  >> from proto2 import compose
  >> def k(x):  return x**3

  >> def j(x):  return x-7

  >> h = compose(k,j)
  >> del k      # destroy k
  >> del j      # destroy j
  >> h(10)      # still works!
  27

Kirby