Function object in Scol » History » Version 3
iri, 10/07/2012 09:01 PM
| 1 | 1 | iri | h1. Function object in Scol |
|---|---|---|---|
| 2 | |||
| 3 | To call a function, we write the function name followed by parameters, if any. |
||
| 4 | |||
| 5 | <pre> |
||
| 6 | fun <function_name_1> ( <args> )= |
||
| 7 | instructions |
||
| 8 | ;; |
||
| 9 | |||
| 10 | fun <function_name_2> ( <args> )= |
||
| 11 | instructions; |
||
| 12 | <function_name_1> <parameters>; |
||
| 13 | instructions |
||
| 14 | ;; |
||
| 15 | </pre> |
||
| 16 | |||
| 17 | However, we can use a function as an object. A such object could be useful for a subsequent operation. This is common with the reflex (callbacks) : call a function when an event occurs. You can need these objects in other cases. The function object can be passed as an argument what is important for us. |
||
| 18 | |||
| 19 | 3 | iri | To create it, add @@@ before the name of function : @@<function_name_1>@ |
| 20 | 1 | iri | |
| 21 | Here is a very basic example (adpated from the Scol tutoriel by Sylvain huet) : |
||
| 22 | Three ways to add two numbers. |
||
| 23 | |||
| 24 | h2. The classic way : |
||
| 25 | |||
| 26 | a function is called from another function. |
||
| 27 | |||
| 28 | <pre> |
||
| 29 | fun add (x, y)= |
||
| 30 | x+y;; |
||
| 31 | |||
| 32 | fun main ()= |
||
| 33 | _showconsole; |
||
| 34 | _fooId add 1 2; // displays 3 in the console |
||
| 35 | 0;; |
||
| 36 | </pre> |
||
| 37 | |||
| 38 | h2. Using exec ... with [...] : |
||
| 39 | |||
| 40 | We create a function object. We call it with exec. |
||
| 41 | @exec <function_object> with [<arguments>]@ |
||
| 42 | |||
| 43 | <pre> |
||
| 44 | fun add (x, y)= |
||
| 45 | x+y;; // displays 3 in the console |
||
| 46 | |||
| 47 | fun main ()= |
||
| 48 | _showconsole; |
||
| 49 | _fooId exec @add with [1 2]; // object created by a @ before the function name |
||
| 50 | 0;; |
||
| 51 | </pre> |
||
| 52 | |||
| 53 | @exec <function_name> with <tuple>@ apply to an object some arguments and return the result. Note that the arguments are passed in a tuple. exec returns the same type than the <function_name>. |
||
| 54 | |||
| 55 | @exec @add with [1 2];@ : calculate the function in the object @add with two arguments in the tuple [1 2] |
||
| 56 | |||
| 57 | h2. Passing an object in parameter : |
||
| 58 | |||
| 59 | <pre> |
||
| 60 | fun add (x, y)= |
||
| 61 | x+y;; // displays 3 in the console |
||
| 62 | |||
| 63 | fun fooadd (x, y, objFun)= |
||
| 64 | exec objFun with [x y];; |
||
| 65 | |||
| 66 | fun main ()= |
||
| 67 | _showconsole; |
||
| 68 | _fooId fooadd 1 2 @add; // call fooadd with an function object @add as parameter |
||
| 69 | 0;; |
||
| 70 | </pre> |
||
| 71 | |||
| 72 | Of course, in these different manners, you must keep a typing correct. |
||
| 73 | |||
| 74 | h2. A fourth way ! |
||
| 75 | |||
| 76 | There is a fourth answer : create a node from a function from another function and an argument. |
||
| 77 | In fact, we "add" an argument to a subsequent use. A function with N-1 argument "becomes" a function with N arguments. This is commonly used in the callbacks. |
||
| 78 | To use this, we have mkfunN functions (N : 2 -> 8) |
||
| 79 | |||
| 80 | Here, we get again the previous and very basic example. |
||
| 81 | |||
| 82 | <pre> |
||
| 83 | fun add (x, y)= |
||
| 84 | x+y;; // displays 3 in the console |
||
| 85 | |||
| 86 | fun fooadd (x)= |
||
| 87 | let mkfun2 @add 1 -> foofun in // we fix the second argument to 1 |
||
| 88 | exec foofun with [x];; |
||
| 89 | |||
| 90 | fun main ()= |
||
| 91 | _showconsole; |
||
| 92 | _fooId fooadd 2;; |
||
| 93 | </pre> |
||
| 94 | |||
| 95 | Now, another example using callback with this last way. |
||
| 96 | We create a window object and when the user closes it a message is displayed in the console and a value is returned. |
||
| 97 | The function called when the user closes a window has two arguments. In this example, we want three arguments, we use thus mkfun3. |
||
| 98 | |||
| 99 | <pre> |
||
| 100 | /* mkfun_parameter is the third argument added when the callback has been defined */ |
||
| 101 | fun endWin (objwin, user_parameter, mkfun_parameter)= |
||
| 102 | _fooS mkfun_parameter; |
||
| 103 | user_parameter;; |
||
| 104 | |||
| 105 | fun crWin ()= |
||
| 106 | // create a simple window |
||
| 107 | let _CRwindow _channel nil 0 0 250 50 WN_NORMAL "function objects" -> window in |
||
| 108 | let "THE END" -> string in |
||
| 109 | /* define a callback when the user closes the window creating a function object. |
||
| 110 | 2 | iri | This callback takes two arguments normally (the object window and an user parameter). |
| 111 | With mkfun3, we "add" a third argument */ |
||
| 112 | 1 | iri | _CBwinDestroy window mkfun3 @endWin string 0; |
| 113 | 0;; |
||
| 114 | |||
| 115 | fun main ()= |
||
| 116 | _showconsole; |
||
| 117 | crWin;; |
||
| 118 | </pre> |
||
| 119 | |||
| 120 | Of course, this example is simply. We get the same result passing a tuple as our user_parameter ... |
||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | License : "CC-BY-SA-2.0":https://creativecommons.org/licenses/by-sa/2.0/ |
||
| 125 | Tutorial by iri |
||
| 126 | Updated by / |