Category Archives: functions

Function Chaining & Classes

Actionscript:
  1. package {
  2.     import flash.display.Sprite;
  3.     public class Chaining extends Sprite{
  4.         public function Chaining(){
  5.               print(n(100).divide(2).plus(2));
  6.               // outputs 52              
  7.               print(n(100).plus(n(10).multiply(2)));
  8.               // outputs 120
  9.         }
  10.     }
  11. }
  12.  
  13. function print(n:Num):void{
  14.    trace(n.getValue());
  15. }
  16. function n(n:Number):Num{
  17.    return new Num(n);
  18. }
  19.  
  20. class Num{
  21.     private var value:Number;
  22.    
  23.     public function Num(n:Number):void{
  24.        value = n;
  25.     }
  26.     private function convert(n:*):Number{
  27.         if (n is Num) n = n.getValue();
  28.         return n;
  29.     }
  30.     public function getValue():Number{
  31.         return value;
  32.     }
  33.     public function plus(n:*):Num{
  34.         n = convert(n);
  35.         value += n;
  36.         return this;
  37.     }
  38.     public function minus(n:*):Num{
  39.         n = convert(n);
  40.         value -= n;
  41.         return this;
  42.     }
  43.     public function multiply(n:*):Num{
  44.         n = convert(n);
  45.         value *= n;
  46.         return this;
  47.     }
  48.     public function divide(n:*):Num{
  49.         n = convert(n);
  50.         value /= n;
  51.         return this;
  52.     }
  53. }

This snippet is meant to be run as a document class. It shows how one might go about designing a class to make extensive use of function chaining.

Also posted in OOP | Tagged , | 2 Comments

var obj:Object = new Function()

Actionscript:
  1. var Pnt:Function = function(xp:Number, yp:Number){
  2.    
  3.     var x:Number = xp;
  4.     var y:Number = yp
  5.    
  6.     this.setX = function(v:int):void{
  7.         x = v;
  8.     }
  9.     this.setY = function(v:int):void{
  10.         y = v;
  11.     }
  12.     this.getX = function():int {
  13.         return x;
  14.     }
  15.     this.getY = function():int {
  16.         return y;
  17.     }
  18. }
  19.  
  20. var p:Object = new Pnt(10,10);
  21.  
  22. trace(p.getX(), p.getY());
  23. p.setX(100);
  24. trace(p.getX());
  25.  
  26. /*
  27. outputs:
  28. 10, 10
  29. 100
  30. */

Another way to define and instantiate Objects on the timeline. Interesting, but I don't recommend it over actual classes...

Also posted in OOP, Object | Tagged , | Leave a comment

Interesting Function Composition

Actionscript:
  1. var multBy2Add3:Function = add(3, mult(2));
  2.  
  3. trace(multBy2Add3(10));
  4. // 10 * 2  = 20
  5. //  20 + 3 = 23
  6.  
  7. var add2AndSquare = sq(add(2));
  8.  
  9. trace(add2AndSquare(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
  21. trace(sq(mult(5,add(1)))(4));
  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.  
  43. function add(off:Number, f:Function=null):Function{
  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();
add();
sq();
repeat();

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

Also posted in Math | Tagged , | Leave a comment

more(nesting(functions(graphDrawing)));

Actionscript:
  1. [SWF(width=800, height=600)]
  2.  
  3. var canvas:Graphics;
  4. var graphData:Array = sineData();
  5.  
  6. var graph0:Shape = Shape(addChild(new Shape()));
  7. graph0.x = 50;
  8. graph0.y = 150;
  9.  
  10. var graph1:Shape = Shape(addChild(new Shape()));
  11. graph1.x = 400;
  12. graph1.y = 150;
  13.  
  14. var graph2:Shape = Shape(addChild(new Shape()));
  15. graph2.x = 50;
  16. graph2.y = 400;
  17.  
  18. // use graphData to draw 3 different looking graphs:
  19.  
  20. canvas = graph0.graphics;
  21. axis(lines(graphData));
  22.  
  23. canvas = graph1.graphics;
  24. axis(dots(graphData, 0xFF0000), 0xFFCC00, 2);
  25.  
  26. canvas = graph2.graphics;
  27. axis(dots(dots(lines(lines(graphData, 0xCCCCCC, 20))), 0x0022FF, 0, 4), 0xFF);
  28.  
  29.  
  30. // generate data
  31. function sineData():Array{
  32.     var dat:Array = new Array();
  33.     for (var i:int = 0; i<60; i++){
  34.         dat.push(new Point(i * 4,  (30 + i) * Math.sin(i * 24 * Math.PI/180)));
  35.     }            
  36.     return dat;
  37. }
  38.  
  39. // render lines
  40. function lines(dat:Array, col:uint=0x000000, thick:Number=0):Array{
  41.     canvas.lineStyle(thick, col);
  42.     canvas.moveTo(dat[0].x, dat[0].y)
  43.     for (var i:int = 1; i<dat.length; i++){
  44.          canvas.lineTo(dat[i].x, dat[i].y);
  45.     }
  46.     return dat;
  47. }
  48.  
  49. // render dots
  50. function dots(dat:Array, col:uint=0xFF0000, thick:Number=0, rad:Number=1.5):Array{
  51.     canvas.lineStyle(thick, col);
  52.     for (var i:int = 0; i<dat.length; i++){
  53.          canvas.drawCircle(dat[i].x, dat[i].y, rad);
  54.     }
  55.     return dat;
  56. }
  57.  
  58. // render graph axis
  59. function axis(dat:Array, col:uint=0x000000, thick:Number=0):Array{
  60.     var d:Array = dat.concat();
  61.     d.sortOn("y", Array.NUMERIC);
  62.     var lastIndex:int = d.length - 1;
  63.     var minY:Number = d[0].y;
  64.     var maxY:Number = d[lastIndex].y;
  65.     d.sortOn("x", Array.NUMERIC);
  66.     var minX:Number = d[0].x;
  67.     var maxX:Number = d[lastIndex].x;
  68.     canvas.lineStyle(thick, col, .2);
  69.     canvas.moveTo(minX, 0);
  70.     canvas.lineTo(maxX, 0);
  71.     canvas.lineStyle(thick, col);
  72.     canvas.moveTo(minX, minY);
  73.     canvas.lineTo(minX, maxY);
  74.     canvas.lineTo(maxX, maxY);
  75.     return dat;
  76. }

This is something I've been meaning to post for awhile. Finally had time to write it today... It contains functions that are designed to be nested for the purpose of rendering a small data set in a few different ways...

The upper left image is rendered with axis labels and lines... and it defaults to the color black (line 21):

axis(lines(graphData));

The upper right image is rendered with yellow axis and red dots (line 24):

axis(dots(graphData, 0xFF0000), 0xFFCC00, 2);

etc... (line 27)
axis(dots(dots(lines(lines(graphData, 0xCCCCCC, 20))), 0x0022FF, 0, 4), 0xFF);

Alternatively you could write each function call on one line:

lines(graphData, 0xCCCCCC, 20);
lines(graphData);
dots(graphData);
dots(graphData, 0x0022FF, 0, 4)
axis(graphData, 0xFF);

NOTE: If you think this post is insane, please read the warning page of this site...

Also posted in Graphics, misc | Tagged , | Leave a comment