views:

25

answers:

1

I often cross this kind of code transformation (or even mathematical transformation). (Python example, but applies to any language.)

I've go a function

def f(x):
  return x

I use it into another one.

def g(x):
  return f(x)*f(x)

print g(2)

leads to 4

But I want to remove the functional dependency, and I change the function g into

def g(f):
  return f*f

print g( f(2) )

leads to 4 too

How do you call this kind of transformation, locally turning a function into a scalar ?

A: 

I'm not sure there is a specific term for it.

In general terms for functional programming there usually isn't a distinction made between passing scalar arguments and passing functions as arguments.

In the first example I could still call g(f(2)) and it should calculate f(f(2))*f(f(2)), which (since f(x) is the identity transformation) will also result in 4 as the answer.

Paolo
yes but if f(x) is time consuming (which is not the case here) g ( f(2) ) in the second case is twice as fast as g(2) in the first case.I'm looking for a naming for this kind of code optimization.
Ah, I see what you mean. In which case perhaps Memoization (http://en.wikipedia.org/wiki/Memoization) is the term you're after? I'd expect/hope the compiler and/or run-time would look after this type of optimisation though.
Paolo
Thanks for the link it answer the "optimization" part of the pattern, but i was looking for a more "mathematical" term:another example could have been, the ODE definition:in: y'(t) = y(t)^2 ; y is a function.The equation can be rewritten:f(t) = y(t)^2 ;To solve it, you build a new function:f2(y,t) -> y^2 ;where in this case y is a real.you've transformed f into f2 by the same exact process I've done with g(x)Something like "scalarization of a function" would make sense, unfortunately, it doesn't.