Latest Changes

Differences between version #19851 and #20107

1c1
< ====== Callback ======
---
> ====== _ (underscore) ======
3c3
< //callback //creates a wrapper function around another function, automatically inserting arguments into the original function that will persist every time the wrapper function is called.
---
> HaXe 3.0 provides an intuitive language construct for partial function application, also known as currying. Call a function with an underscore character in place of one or more argument values, and it will return a wrapper function that takes only those "fill-in-the-blank" arguments. Then when you call this wrapper function, it will call the original function and automatically insert the values for the other arguments you specified.
5d4
< In haxe 3.0 it is used:
7c6
< public function foo(x:Int, y:Int) {return x+y;}
---
> public function sumXplusY(x:Int, y:Int) {return x + y;}
9,14c8,11
< 	// creates a copy of foo() that automatically makes the first argument a 4
< 	var foo_cb = foo( _, 4 );
< 	trace(foo_cb(5)); // traces 9
< 	// or
< 	// var foo_cb = foo( 4, _ );
< 	// trace(foo_cb(5)); // traces 9
---
> 	var sumXplus2 = foo( _, 2 );
> 	var sum4plusY = foo( 4, _ );
> 	trace(sumXplus2(3)); // traces 5
> 	trace(sum4plusY(3)); // traces 7
17c14,20
< but currently unless you use the //-D haxe3// compiler switch you will have to use the more verbose style ( although maybe you can use both need to check ).
---
> 
> Currently this syntatic sugar requires the //-D haxe3// compiler switch, otherwise you must use the more verbose style described below.
> 
> ====== Callback ======
> 
> In older versions (2.8 and prior), currying was applied with the //callback// function. //callback// creates a wrapper function around another function, automatically inserting arguments into the original function that will persist every time the wrapper function is called.
> 
28c31
< Callback is similar to what you can accomplish through some of the methods in [[/api/Reflect|Reflect.callMethod()]]. Callback were more limited than any of the Reflect methods but with the new 3.0 syntax are more flexible. 
---
> Callback is similar to what you can accomplish through some of the methods in the [[/api/Reflect|Reflect API]]. However, with //callback// you can only insert arguments in order (i.e. you must specify the first argument before you can specify the second argument, and so forth). In this way, callback was more limited than any of the Reflect methods, but the new 3.0 syntax is more flexible. 
30c33
< The big feature of using callback is that it is a fully typed call, for both arguments and arity, so you get type-safe code (vs. [[/api/Reflect]]).  A benefit of callback comes inside of functional routine arguments, such as the Lambda methods.  The Lambda method arguments typically require  the form f: A->B, so they accept only a single argument, but they are also particularly useful for injecting data into signal less signals / with events.
---
> ====== Usage ======
32c35
< With callback, you can quickly create wrapper functions that save you the trouble of writing out one-off function blocks.  Consider the following two snippets:
---
> The big advantage of using //callback// (or the new syntax) is that it is a fully typed call, for both arguments and arity, so you get type-safe code (vs. [[/api/Reflect]]). This is very useful in functional routine arguments, such as the Lambda methods, which typically require a function that takes only one argument. For example, you can do this:
35c38,42
< // calculate the following powers of 3
---
> // calculate the powers of 3 for each element of an array,
> // and return the results in a new array:
> trace(Lambda.map( [10,2,36,4,5], Math.pow(3, _)));
> 
> // that is the same as this:
36a44,46
> 
> // which is also the same as this:
> trace(Lambda.map( [10,2,36,4,5], function(x) { return Math.pow(3, x);}));
38c48,50
< vs.
---
> 
> With the new syntax you can also do this:
> 
40c52,53
< trace(Lambda.map( [10,2,36,4,5], function(x) { return Math.pow(3,x);}));
---
> // calculate the squares of each element of an array
> trace(Lambda.map( [10,2,36,4,5], Math.pow(_, 2)));
43,45c56
< 
< ==== haxe 2.08 using old style callback notes:  ====
< You can only insert arguments in order (i.e. you must specify the first argument before you can specify the second argument, and so forth).  So, you can't really use it to only specify the **final** argument of a method.
---
> That cannot be done with the old syntax.
\ No newline at end of file

	
Ver Date Entry Lg User Action
#20122 2014-05-24 12:52:26 com/libs/cocktail/cocktail_web_app_demo en codam View | Diff
#20121 2014-05-22 20:00:15 doc/libraries en Confidant View | Diff
#20120 2014-05-22 19:49:58 doc/libraries en Confidant View | Diff
#20119 2014-05-22 11:25:38 manual/macros kr papapang View | Diff
#20118 2014-05-22 11:25:38 manual/macros kr papapang Set title to macros (매크로)
#20117 2014-05-22 07:16:17 doc/glossary en vitalyo View | Diff
#20116 2014-05-21 19:35:43 doc/libraries en Confidant View | Diff
#20115 2014-05-21 15:17:54 doc/libraries en Confidant View | Diff
#20114 2014-05-21 15:16:44 doc/libraries en Confidant View | Diff
#20113 2014-05-21 13:13:34 ref/callback en Simn View | Diff
#20112 2014-05-20 19:48:30 doc/libraries en Confidant View | Diff
#20111 2014-05-20 19:47:59 doc/libraries en Confidant View | Diff
#20110 2014-05-20 19:18:18 doc/libraries en Confidant View | Diff
#20109 2014-05-17 18:07:43 ref/callback en galundin View | Diff
#20108 2014-05-17 18:05:56 ref/callback en galundin View | Diff
#20107 2014-05-17 17:51:30 ref/callback en galundin View | Diff
#20106 2014-05-11 22:04:20 ref/oop en Chax0 View | Diff
#20105 2014-05-11 22:02:31 ref/oop en Chax0 View | Diff
#20104 2014-05-09 04:42:48 doc/java/lwjgl en JLM View | Diff
#20103 2014-05-08 13:52:07 doc/java/lwjgl en JLM View | Diff

Previous | Next