Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback functions in Chapel

Tags:

hpc

chapel

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?

like image 701
Brian Dolan Avatar asked May 23 '20 00:05

Brian Dolan


People also ask

What is callback function and how it works?

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.

What are the types of callback?

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).

Why callback function is used?

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.

What are database callback function called?

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.


2 Answers

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)))

like image 129
Vass Avatar answered Sep 22 '22 17:09

Vass


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.

like image 24
user3666197 Avatar answered Sep 23 '22 17:09

user3666197