Re: 함수에 대한 참조를 전달할 수 있습니까?참조로 전달하는 간단한 방법은 없지만 함수와 데이터를 클래스 또는 네임스페이스에 캡슐화하고 개체 포인터 또는 네임스페이스 이름을 전달할 수 있습니다. 맨 위에 있는 사진을 보세요. JSL 객체를 사용한 함수형 프로그래밍 수업에서 어떻게 작동하는지 아이디어를 얻으려면.
Re: 함수에 대한 참조를 전달할 수 있습니까?나는 이와 같은 것을 많이 사용하고 내 경험에 따르면 NameExpr() 기능은 아주 잘 작동합니다.다른 함수에서 함수를 반환할 때 까다로울 수 있으며 @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
큰 기능에 대한 최대 2배의 성능 향상은 0;0;0으로 사라집니다. 더미 문이 제거됩니다. 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"
이것은 내가 필요한 것을 성취하지만 변수 이름을 사용하여 함수를 전달할 수 있으면 편리할 것입니다.
고급 통계 모델링자동화 및 스크립팅소비자 및 시장 조사품질 및 프로세스 엔지니어링Re: 함수에 대한 참조를 전달할 수 있나요?에 대한 추가 문서는 어디서 찾을 수 있나요? PassByRef 옵션?
아아.
Re: 함수에 대한 참조를 전달할 수 있나요?@호기 - 죄송합니다. 코드 중간에 중요한 부분을 묻어두었습니다! 다른 사람에게는
Define Class(
"PassByRef",
mFn = .;
_init_ = Method( {fn},
mFn = Name Expr( fn )
);
);
somefuncRef = New Object( PassByRef( Name Expr( someFunc ) ) );
라는 클래스 정의를 만들고 있습니다. PassByRef 그리고 그 클래스의 인스턴스는 somefuncRef . 인스턴스는 mFn을 둘러싼 래퍼로, 다음의 복제본을 보유합니다. someFunc . 그러나 해당 클론은 더 이상 클론을 만들지 않고도 계속해서 재사용됩니다. 집행관이 여기서 전화를 겁니다.
executor = Function( {fnRef, arg}, {},
If( print,
Print( "Executing for class..." )
);
fnRef:mFn( arg );
);