Transforming function arguments inline

Is there a way to transform a function call’s argument with an inline function?

A trivial example to illustrate my question:

`function foo( a, b ) {
  return a + b;
}
function bar (a) {
  return a * 10;
}

local val = 2;  

local result = foo ( 1, bar(val) );
`

This returns 21, as desired.

But what if I want to define the bar() transformation to be inline to the foo function call? Since I’d never need to be calling bar() anywhere else anyway, this approach seems better.

Also, let us say that foo’s definition cannot be altered in any way to make this work.

For example, this intuitive first attempt fails:

`function foo( a, b ) {
  return a + b;
}

local val = 2;  

local result = foo ( 1, function(val) {
  return val * 10;
});
`

The error being, of course, at “return a+b” in foo():
"ERROR: arith op + on between 'integer' and 'function' "

I realize the example transform I’ve used is trivial. The one I’m actually using is more complex, hence my question.

I’m sure it must be easy, but would sure appreciate some help. Have google’ed and scanned these forums in vain. Thank-you for any help!

With return a+b, you are literally adding an integer to a function, so the error message is expected.

You need need to put your local variable val within the scope of foo and use return a+b(val) instead.

Better still, you can just declare the function inside foo if you want to.

Sorry, rereading your post, I see that you don’t want to modify foo.

You could call foo with
local result = foo ( 1, function(val=val) {
return val * 10;
});

And then in foo, you could say

return a+b()

Thanks for the efforts, coverdriven. Much appreciated.

foo() is a library function I don’t have access to modify. Any other ideas?

How about passing foo into the anonymous function?

`local result = function(a,b,f) {
  local b1 = b * 10;
  return f(a,b1);
}(1, val, foo);`

That works. Thank-you!
But, I don’t know why or how it works, which bothers me very much.

…okay, after 30 minutes I think I understand. Man, you are a real marvel. I don’t think I’d ever have thought of that in a million years.

Thanks, philmy!!!