<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Is it possible to pass a reference to a Function? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630843#M82903</link>
    <description>&lt;P&gt;That does look simpler, and easier to understand. There is a tradeoff that will only make a difference for large functions that are called a lot. NameExpr is cloning the expression tree for the function, and passing the copy, not a reference. I don't think there is any way to tell it is a copy vs reference (like you can for arrays that are not modified on return) other than timing it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;loops=1e5; // ~10 seconds/100K loops
print = (loops==1e0);
someFunc = Function( {arg}, {},
	If( print,
		Print( "Within One Function" );
		Print( arg );
	);
	Return( 0 );
	// this creates a stiff penalty for nameexpr  
	// needing to clone this function. These
	// statements are simple and never executed but still
	// must be copied to the nameExpr result by allocating
	// a bunch of memory objects.
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
);

/////////////////////////////////////
Write( "\!nUsing NameExpr" );
executor = Function( {fn, arg}, {},
	If( print,
		Print( "Executing for NameExpr..." )
	);
	fn( arg );
);
start = HP Time();
For( i = 1, i &amp;lt;= loops, i += 1,
	executor( Name Expr( someFunc ), "this string" )
);
stop = HP Time();

Show( stop - start ); // ~7 seconds

/////////////////////////////////////
Write( "\!nUsing class" );
Define Class(
	"PassByRef",
	mFn = .;
	_init_ = Method( {fn},
		mFn = Name Expr( fn )
	);
);
// grab the identical function into a reference object &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; requires JMP 17, a bug was fixed after 16.2
somefuncRef = New Object( PassByRef( Name Expr( someFunc ) ) );
&lt;BR /&gt;/* for JMP &amp;lt; 17 use this work-around: &lt;BR /&gt;somefuncRef = New Object( PassByRef( 0 ) );&lt;BR /&gt;somefuncRef:mFn=Name Expr( someFunc );&lt;BR /&gt;*/

/********** this also works and looks ... better?
somefuncRef = New Object( PassByRef( Function( {arg}, {},
	If( print,
		Print( "Within One Function" );
		Print( arg );
	);
	Return( 0 );
	// this creates a stiff penalty for nameexpr  
	// needing to clone this function. These
	// statements are simple and never executed but still
	// must be copied to the nameExpr result by allocating
	// a bunch of memory objects.
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
);
));
*********/



executor = Function( {fnRef, arg}, {},
	If( print,
		Print( "Executing for class..." )
	);
	fnRef:mFn( arg );
);
start = HP Time();
For( i = 1, i &amp;lt;= loops, i += 1,
	executor( somefuncRef, "this string" )
);
stop = HP Time();

Show( stop - start ); // ~3 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The ~2x performance boost for a large function vanishes as the 0;0;0; dummy statements are removed; for the short functions you might be using in a Qt-like way it won't make a difference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 May 2023 03:14:03 GMT</pubDate>
    <dc:creator>Craige_Hales</dc:creator>
    <dc:date>2023-05-11T03:14:03Z</dc:date>
    <item>
      <title>Is it possible to pass a reference to a Function?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630751#M82897</link>
      <description>&lt;P&gt;For example when I run this script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;someFunc = Function( {arg},
	{},
	
	Print( arg );
);

executor = Function( {fn, arg},
	{},
	
	fn( arg );
);

executor( someFunc, "this string" )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get an error, that seems to imply that I am calling someFunc when I am intending to pass a reference to it to executor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am able to pass a reference by defining the function in the invocation, e.g.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;executor = Function( {fn, arg},
	{},
	
	Print(arg);
    fn();
);

executor( Function({}, {}, Print("anonymous function"); ), "wut" );&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Outputs:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;"wut"
"anonymous function"&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Which accomplishes what I need, but it would be convenient to be able to pass functions using their variable names.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:09:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630751#M82897</guid>
      <dc:creator>mat-ski</dc:creator>
      <dc:date>2023-06-09T16:09:42Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to pass a reference to a Function?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630830#M82901</link>
      <description>&lt;P&gt;There is no simple way to pass by reference, but you can encapsulate functions and data in classes or namespaces and pass the object pointer or namespace name. Take a look at the picture at the top of &lt;LI-MESSAGE title="Functional programming using JSL objects" uid="272870" url="https://community.jmp.com/t5/Uncharted/Functional-programming-using-JSL-objects/m-p/272870#U272870" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-blog-thread lia-fa-icon lia-fa-blog lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp; to get an idea how that might work for classes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 00:02:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630830#M82901</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-05-11T00:02:47Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to pass a reference to a Function?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630835#M82902</link>
      <description>&lt;P&gt;I use something like this a lot, and from my experience, using the &lt;CODE class=" language-jsl"&gt;NameExpr()&lt;/CODE&gt; function works quite well.&amp;nbsp; It can be tricky when returning a function from another function, and&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt;&amp;nbsp;method is usually a better choice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have extensive background in Qt and really appreciate their signals / slots -- I created something quite similar in JSL and it is necessary to shuttle functions around by reference and not so practical to do it with namespace / class wrappers, so I use something very similar to this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;someFunc = Function( {arg},
	{},
	Print( "Within One Function" );
	Print( arg );
);

executor = Function( {fn, arg},
	{},
	Print( "Executing..." );
	fn( arg );
);

executor( Name Expr( someFunc ), "this string" )&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 May 2023 00:51:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630835#M82902</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2023-05-11T00:51:51Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to pass a reference to a Function?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630843#M82903</link>
      <description>&lt;P&gt;That does look simpler, and easier to understand. There is a tradeoff that will only make a difference for large functions that are called a lot. NameExpr is cloning the expression tree for the function, and passing the copy, not a reference. I don't think there is any way to tell it is a copy vs reference (like you can for arrays that are not modified on return) other than timing it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;loops=1e5; // ~10 seconds/100K loops
print = (loops==1e0);
someFunc = Function( {arg}, {},
	If( print,
		Print( "Within One Function" );
		Print( arg );
	);
	Return( 0 );
	// this creates a stiff penalty for nameexpr  
	// needing to clone this function. These
	// statements are simple and never executed but still
	// must be copied to the nameExpr result by allocating
	// a bunch of memory objects.
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
);

/////////////////////////////////////
Write( "\!nUsing NameExpr" );
executor = Function( {fn, arg}, {},
	If( print,
		Print( "Executing for NameExpr..." )
	);
	fn( arg );
);
start = HP Time();
For( i = 1, i &amp;lt;= loops, i += 1,
	executor( Name Expr( someFunc ), "this string" )
);
stop = HP Time();

Show( stop - start ); // ~7 seconds

/////////////////////////////////////
Write( "\!nUsing class" );
Define Class(
	"PassByRef",
	mFn = .;
	_init_ = Method( {fn},
		mFn = Name Expr( fn )
	);
);
// grab the identical function into a reference object &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; requires JMP 17, a bug was fixed after 16.2
somefuncRef = New Object( PassByRef( Name Expr( someFunc ) ) );
&lt;BR /&gt;/* for JMP &amp;lt; 17 use this work-around: &lt;BR /&gt;somefuncRef = New Object( PassByRef( 0 ) );&lt;BR /&gt;somefuncRef:mFn=Name Expr( someFunc );&lt;BR /&gt;*/

/********** this also works and looks ... better?
somefuncRef = New Object( PassByRef( Function( {arg}, {},
	If( print,
		Print( "Within One Function" );
		Print( arg );
	);
	Return( 0 );
	// this creates a stiff penalty for nameexpr  
	// needing to clone this function. These
	// statements are simple and never executed but still
	// must be copied to the nameExpr result by allocating
	// a bunch of memory objects.
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
	0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;
);
));
*********/



executor = Function( {fnRef, arg}, {},
	If( print,
		Print( "Executing for class..." )
	);
	fnRef:mFn( arg );
);
start = HP Time();
For( i = 1, i &amp;lt;= loops, i += 1,
	executor( somefuncRef, "this string" )
);
stop = HP Time();

Show( stop - start ); // ~3 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The ~2x performance boost for a large function vanishes as the 0;0;0; dummy statements are removed; for the short functions you might be using in a Qt-like way it won't make a difference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 03:14:03 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/630843#M82903</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-05-11T03:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to pass a reference to a Function?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/679043#M86528</link>
      <description>&lt;P&gt;&lt;STRIKE&gt;Where can I find further documentation on the &lt;STRONG&gt;PassByRef&lt;/STRONG&gt; option?&lt;BR /&gt;&lt;/STRIKE&gt;argh.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 19:52:50 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/679043#M86528</guid>
      <dc:creator>hogi</dc:creator>
      <dc:date>2023-09-18T19:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to pass a reference to a Function?</title>
      <link>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/679103#M86531</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/26800"&gt;@hogi&lt;/a&gt;&amp;nbsp; - sorry, I did kind of bury the important bit in the middle of that code! For anyone else,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Define Class(
	"PassByRef",
	mFn = .;
	_init_ = Method( {fn},
		mFn = Name Expr( fn )
	);
);
somefuncRef = New Object( PassByRef( Name Expr( someFunc ) ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is making a class definition named &lt;EM&gt;PassByRef&lt;/EM&gt; and an instance of that class named &lt;EM&gt;somefuncRef&lt;/EM&gt;. The instance is a wrapper around mFn, which holds a clone of &lt;EM&gt;someFunc&lt;/EM&gt;. But that clone is reused again and again, without making more clones. The executor makes that call here&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;executor = Function( {fnRef, arg}, {},
	If( print,
		Print( "Executing for class..." )
	);
	fnRef:mFn( arg );
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Sep 2023 23:57:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Is-it-possible-to-pass-a-reference-to-a-Function/m-p/679103#M86531</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-09-18T23:57:26Z</dc:date>
    </item>
  </channel>
</rss>

