# Interesting Function Composition

Actionscript:
2.
4. // 10 * 2  = 20
5. //  20 + 3 = 23
6.
8.
10. // 8 + 2 = 10
11. // 10 * 10 = 100
12.
13. var multBy2_3times:Function = repeat(3,mult(2));
14.
15. trace(multBy2_3times(3));
16. // 3 * 2 = 6;
17. // 6 * 2 = 12;
18. // 12 * 2 = 24
19.
20. // you can also chain for even less readability
22. // 4 + 1 = 5
23. // 5 * 5 = 25;
24. // 25 * 25 = 625;
25.
26. /*
27. outputs:
28. 12
29. 100
30. 24
31. 625
32. */
33.
34. // function  composition
35. const F:Function = function(a:*):*{return a};
36. function mult(scalar:Number, f:Function=null):Function{
37.     if (f == null) f = F;
38.      return function(n:Number){
39.         return f(n) * scalar;
40.     }
41. }
42.
44.      if (f == null) f = F;
45.      return function(n:Number){
46.         return f(n) + off;
47.     }
48. }
49. function sq(f:Function=null):Function{
50.      if (f == null) f = F;
51.      return function(n:Number){
52.          var v:Number = f(n);
53.         return  v * v;
54.     }
55. }
56. function repeat(times:int, f:Function):Function {
57.      if (f == null) f = F;
58.      return function (n:Number){
59.          var v:Number = n;
60.          for (var i:int = 0; i<times; i++) v = f(v);
61.          return v;
62.      }
63. }

The above shows some interesting function composition... this demo contains the following functions:

mult();
sq();
repeat();

These functions are designed to be combined to create new functions.