Re: 是否可以傳遞對函數的引用?沒有通過引用傳遞的簡單方法,但可以將函數和數據封裝在類或命名空間中,並傳遞對象指針或命名空間名稱。 看看上面的圖片使用 JSL 對象的函數式編程了解這如何適用於課堂。
Re: 是否可以傳遞對函數的引用?我經常使用這樣的東西,根據我的經驗,使用名稱表達式()功能運作良好。從另一個函數返回一個函數時可能會很棘手,並且@Craige_Hales方法通常是更好的選擇。
我在 Qt 方面有廣泛的背景,非常感謝他們的信號/槽——我在 JSL 中創建了一些非常相似的東西,有必要通過引用來穿梭函數,而使用命名空間/類包裝器來實現它不太實用,所以我使用了一些非常類似於此。
someFunc = Function( {arg},
{},
Print( "Within One Function" );
Print( arg );
);
executor = Function( {fn, arg},
{},
Print( "Executing..." );
fn( arg );
);
executor( Name Expr( someFunc ), "this string" )Re: 是否可以傳遞對函數的引用?這看起來確實更簡單,也更容易理解。 有一種權衡只會對被多次調用的大型函數產生影響。 NameExpr 正在克隆函數的表達式樹,並傳遞副本,而不是引用。 除了計時之外,我認為沒有任何方法可以告訴它是副本還是參考(就像您可以對返回時未修改的數組一樣):
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 <= 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 >>>>> requires JMP 17, a bug was fixed after 16.2
somefuncRef = New Object( PassByRef( Name Expr( someFunc ) ) );
<BR />/* for JMP < 17 use this work-around: <BR />somefuncRef = New Object( PassByRef( 0 ) );<BR />somefuncRef:mFn=Name Expr( someFunc );<BR />*/
/********** 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 <= loops, i += 1,
executor( somefuncRef, "this string" )
);
stop = HP Time();
Show( stop - start ); // ~3 seconds
當 0;0;0; 時,大型函數的 ~2x 性能提升消失了。刪除了虛擬語句;對於您可能以類似 Qt 的方式使用的短函數,它不會有什麼不同。
是否可以傳遞對函數的引用?例如,當我運行此腳本時:
someFunc = Function( {arg},
{},
Print( arg );
);
executor = Function( {fn, arg},
{},
fn( arg );
);
executor( someFunc, "this string" )我收到一個錯誤,這似乎意味著當我打算將對 someFunc 的引用傳遞給執行程序時我正在調用它。
我可以通過在調用中定義函數來傳遞引用,例如
executor = Function( {fn, arg},
{},
Print(arg);
fn();
);
executor( Function({}, {}, Print("anonymous function"); ), "wut" );輸出:
"wut"
"anonymous function"
這完成了我所需要的,但是能夠使用變量名傳遞函數會很方便。
高級統計建模自動化和腳本編寫消費者和市場研究質量和工藝工程回覆:是否可以傳遞對函數的參考?我可以在哪裡找到有關的更多文檔透過引用選項?
啊。
回覆:是否可以傳遞對函數的參考?@hogi - 抱歉,我確實把重要的部分埋在程式碼中間了! 對其他人來說,
Define Class(
"PassByRef",
mFn = .;
_init_ = Method( {fn},
mFn = Name Expr( fn )
);
);
somefuncRef = New Object( PassByRef( Name Expr( someFunc ) ) );
正在建立一個名為的類別定義透過引用和該類別的一個實例名為一些函數引用。 此實例是 mFn 的包裝器,它包含一個克隆一些函數。 但這個克隆會被一次又一次地重複使用,而不會產生更多的克隆。 執行者在這裡進行調用
executor = Function( {fnRef, arg}, {},
If( print,
Print( "Executing for class..." )
);
fnRef:mFn( arg );
);