Category Archives: binary

Haskell Inspired zipWith() Function

Actionscript:
  1. initOperators();
  2.  
  3. trace(zipWith("-", [1,2,3], [1,2,3]));
  4. trace(zipWith("+", [1,2,3], [1,2,3]));
  5. trace(zipWith("*", [1,2,3], [1,2,3]));
  6. trace(zipWith("+", [1,1,1,3], [4,5,6,7]));
  7. trace(zipWith("<<", [2, 4], [1,1]));
  8. /*
  9. outputs:
  10.  
  11. 0,0,0
  12. 2,4,6
  13. 1,4,9
  14. 5,6,7,10
  15. 4,8
  16. */
  17.  
  18. function zipWith(op:String, a:Array, b:Array):Array{
  19.     var aLeng:int = a.length;
  20.     var bLeng:int = b.length;
  21.     var leng:Number = (aLeng <bLeng) ? aLeng : bLeng;
  22.     var zipped:Array = [];
  23.    
  24.     if (!this[op])return [];
  25.    
  26.     for (var i:int = 0; i<leng; i++){
  27.         zipped[i]=this[op](a[i], b[i]);
  28.     }
  29.     return zipped;
  30. }
  31.  
  32. function initOperators():void{
  33.     this["+"]=function(a:Number, b:Number):Number{ return a + b };
  34.     this["-"]=function(a:Number, b:Number):Number{ return a - b };
  35.     this["/"]=function(a:Number, b:Number):Number{ return a / b };
  36.     this["*"]=function(a:Number, b:Number):Number{ return a * b };
  37.     this["%"]=function(a:Number, b:Number):Number{ return a % b };
  38.    
  39.     this["&"]=function(a:Number, b:Number):Number{ return a & b };
  40.     this["<<"]=function(a:Number, b:Number):Number{ return a <<b };
  41.     this["|"]=function(a:Number, b:Number):Number{ return a | b };
  42.     this[">>"]=function(a:Number, b:Number):Number{ return a>> b };
  43.     this[">>>"]=function(a:Number, b:Number):Number{ return a>>> b };
  44.     this["^"]=function(a:Number, b:Number):Number{ return a ^ b };
  45. }

This snippet is basically like the haskell zipWith() function. It can combines two arrays into one array given a single function. In this case I defined a bunch of operator functions, but it would work with any kind of function that takes two arguments and returns a value. You could extend this to work with strings and do other strange things I guess.

If you have yet to go play with haskell ... go do it now.

Also posted in Number, Operators, arrays, functions, misc, return values | Tagged , , , | 2 Comments

Four by Four on Flickr

I took some pictures of Four by Four last night:

Have a look at them over at flickr...


Posted in binary | Tagged | Leave a comment

Bitwise OR | and Variable Function Arguments (bitwise flags)

Actionscript:
  1. const A:uint = 1;
  2. const B:uint = 2;
  3. const C:uint = 4;
  4. const D:uint = 8;
  5. const E:uint = 16;
  6.  
  7. function orArgs(args:uint):void{
  8.     if (args & A){
  9.         trace("A");
  10.     }
  11.     if (args & B){
  12.         trace("B");
  13.     }
  14.     if (args & C){
  15.         trace("C");
  16.     }
  17.     if (args & D){
  18.         trace("D");
  19.     }
  20.     if (args & E){
  21.         trace("E");
  22.     }
  23. }
  24.  
  25. // test out the function:
  26. orArgs(A | B);
  27. trace("--");
  28. orArgs(A | C | E);
  29. trace("--");
  30. orArgs(B | E | D);
  31. trace("--");
  32. orArgs(C | A);
  33.  
  34. /* outputs:
  35. A
  36. B
  37. --
  38. A
  39. C
  40. E
  41. --
  42. B
  43. D
  44. E
  45. --
  46. A
  47. C
  48. */

If you've every used Array.sort(Array.NUMERIC | Array.DESCENDING) you should have at least a vague idea about what this snippet is doing. It shows how you can pass a variable number of arguments to a function using | (bitwise OR) and & (bitwise AND). I believe the correct term for these kind of arguments is "bitwise flags". This snippet works by having a series of constant values... in this case A - E. Each constant is assigned an unsigned integer... now you may not see the significance of the values 1, 2, 4, 8 and 16 until you see them in binary... get ready this is a pretty cryptic description...

A = 00001 = 1
B = 00010 = 2
C = 00100 = 4
D = 01000 = 8
E = 10000 = 16

If we OR all these together we get: 11111... If we do:

A | E
00001 | 10000

we end up with 10001...

...we can then check which values are stored in the resulting unsigned integer by using AND:

check for A... 10001 & 00001 = 00001 = true
check for E... 10001 & 10000 = 10000 = true
check for C... 10001 & 00100 = 00000 = false

That's it... I just guessed at the way this was being done... if you have another way to do the same thing, feel free to post it in the comments....

Also posted in Math, Operators, misc | Tagged , , , | 1 Comment