Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
vince_faller
Super User

Aliasing a function

Is there a way to truly alias a function and not just create a copy of it?

 

Names default to here(1);
f = function({x, y}, 
	print(x);
);

// I'm assuming this just copies it. 
alias_f = nameexpr(f);

alias_f(14, 28);
Vince Faller - Predictum
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Craige_Hales
Staff (Retired)

Re: Aliasing a function

I'm pretty sure the internal code generated by the function(...) function gets cloned if there is a second variable for the same function.

Depending why you need an alias, consider

a = b = Function( {c}, Show( c ) );

Here's a way to peek around the corner and see there's probably a clone:

a = b = Function( {c}, Show(asconstant (c) ) );
a(42);//42
b(43);//43
a(44);//42
b(45);//43

What are you trying to accomplish? Namespaces can be passed around without cloning their content (and the content can be function definitions.) If the function is huge and cloning it takes a noticeable amount of time, a tiny wrapper with the alias name might be appropriate. If it is related to ParallelAssign, it really needs the clone for internal reasons, similar to the asConstant example.

 

Craige

View solution in original post

2 REPLIES 2
Highlighted
Craige_Hales
Staff (Retired)

Re: Aliasing a function

I'm pretty sure the internal code generated by the function(...) function gets cloned if there is a second variable for the same function.

Depending why you need an alias, consider

a = b = Function( {c}, Show( c ) );

Here's a way to peek around the corner and see there's probably a clone:

a = b = Function( {c}, Show(asconstant (c) ) );
a(42);//42
b(43);//43
a(44);//42
b(45);//43

What are you trying to accomplish? Namespaces can be passed around without cloning their content (and the content can be function definitions.) If the function is huge and cloning it takes a noticeable amount of time, a tiny wrapper with the alias name might be appropriate. If it is related to ParallelAssign, it really needs the clone for internal reasons, similar to the asConstant example.

 

Craige

View solution in original post

Highlighted
vince_faller
Super User

Re: Aliasing a function

interesting.  

 

For more context.  I'm trying to alias a new custom function to another name because of a refactor. 
trying to turn 

MyNCF:crappy_function_name

to 

MyNCF:better_function_name

 

without breaking old scripts.  

I like the idea of wrapping.  

 

Haven't used As Constant before.  That's super cool.  

 

I think this is more than enough for me to play with.  Much appreciated.  

Vince Faller - Predictum
Article Labels

    There are no labels assigned to this post.