I have the following Chapel code.
proc update(x: int(32)) {
return 2*x;
}
proc dynamics(x: int(32)) {
return update(x);
}
writeln(dynamics(7));
I would like to send some kind of callback to dynamics
, like
proc update(x: int(32)) {
return 2*x;
}
proc dynamics(x: int(32), f: ?) {
return f(x);
}
writeln(dynamics(7, update));
Is this possible? Are there examples I could browse?
A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function, which contains the logic for when the callback function gets executed.
There are two types of callbacks, differing in how they control data flow at runtime: blocking callbacks (also known as synchronous callbacks or just callbacks) and deferred callbacks (also known as asynchronous callbacks).
Need of Callback Functions. We need callback functions because many JavaScript actions are asynchronous, which means they don't really stop the program (or a function) from running until they're completed, as you're probably used to. Instead, it will execute in the background while the rest of the code runs.
With the sqlbreakcallback() function, you also specify a callback function to be called at several points in the execution of an SQL request. A callback function is a user-defined IBM® Informix® ESQL/C function that specifies actions to take during execution of an SQL request.
Chapel has first-class functions . They are work in progress, at the same time have been used successfully (details are escaping me).
Your example works if you either remove the :?
or specify the function's type as func(int(32), int(32))
:
proc dynamics(x: int(32), f) // or
proc dynamics(x: int(32), f: func(int(32), int(32)))
The same intention may also get fulfilled with passing a lambdified "callback", potentially with an ALAP-associatively mapped callback-wrapper.
var f = lambda(n:int){ return -2 * n;};
writeln( f( 123 ) ); // lambda --> -246
proc update( x: int(32) ) { // Brian's wish
return 2*x;
}
proc dynamics( x: int(32), f ) { // Vass' solution
return f( x );
}
// ---------------------------------------------------------------------------
writeln( dynamics( 7, f ) ); // proof-of-work [PASS] --> -14
var A = [ lambda( n:int ){ return 10 * n; }, // associatively mapped lambdified-"callbacks"
lambda( n:int ){ return 20 * n; },
lambda( n:int ){ return 30 * n; }
];
// ---------------------------------------------------------------------------
writeln( dynamics( 8, lambda(i:int){ return f(i); } ) ); // proof-of-work [PASS] --> -16
writeln( dynamics( 8, lambda(i:int){ return A[1](i); } ) ); // proof-of-work [PASS] --> 80
// ---------------------------------------------------------------------------
forall funIDX in 1..3 do
writeln( dynamics( 8, A[funIDX] ) ); // proof-of-work [PASS] --> 80 | 160 | 240
The whole TiO.run online-IDE mock-up code is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With