jqtouch

trying out jQTouch again. i remembered when i first got my iphone 3G, i tried to write a better webapp version of my favorite bus schedule app for iphone. i was torn between iUI and jQTouch. being a jQuery fanboy, choosing jQTouch was a no brainer. the animation wasn’t as smooth as a native app but it’s easier to work with. the project? like most of my toys, it’s in limbo.

ruby-like syntax for javascript array functions

At work, we recently decided to use YUI (Yahoo UI) to generate our user interface. YUI is great. It got almost all the widgets we need. Tabs, tree, carousel... As a result, we have consistent look all throughout. We dump the javascript framework that came with Ruby on Rails and use YUI instead.

Now, here's the problem. If you've use prototype or jQuery, you must be used to using the utility functions these frameworks provide. I go big on jQuery's each function for iterating through arrays and array-like object. Unfortunately, YUI - in version 2.x, at least - does not provide these array functions.

In a course of an hour, I wrote a handy set of functions to get me through. It could've been easier if I just include jQuery since the noConflict function makes it work with any js framework but then I got a mandate to focus on YUI.

JavaScript:
  1. Array.each = function(a, fn){
  2.     for(var i=0; i <a.length; i++){
  3.         fn(a[i]);
  4.     }
  5. };
  6.  
  7. Array.prototype.each = function(fn){
  8.     Array.each(this,fn);
  9. };
  10.  
  11. Array.collect = function(a, fn){
  12.     var new_array = new Array();
  13.     a.each(function(e){
  14.         new_array.push(fn(e));
  15.     });
  16.     return new_array;
  17. };
  18.  
  19. Array.prototype.collect = function(fn){
  20.     return Array.collect(this, fn);
  21. };
  22.  
  23. Array.inject = function(a, init, fn){
  24.     a.each(function(e){
  25.         init = fn(init, e)
  26.     });
  27.     return init;
  28. };
  29.  
  30. Array.prototype.inject = function(init, fn){
  31.     return Array.inject(this, init, fn);
  32. };
  33.  
  34. Array.detect = function(a, fn){
  35.     for(var i=0; i <a.length; i++){
  36.         if(fn(a[i])){
  37.             return a[i];
  38.         }
  39.     }
  40. };
  41.  
  42. Array.prototype.detect = function(fn){
  43.     return Array.detect(this, fn);
  44. };
  45.  
  46. Array.select = function(a, fn){
  47.     var new_array = new Array();
  48.     a.each(function(e){
  49.         if(fn(e)){
  50.             new_array.push(e);
  51.         }
  52.     });
  53.     return new_array;
  54. };
  55.  
  56. Array.prototype.select = function(fn){
  57.     return Array.select(this, fn);
  58. };
  59.  
  60. Array.reject = function(a, fn){
  61.     return a.select(function(e){
  62.         return (!fn(e));   
  63.     });
  64. };
  65.  
  66. Array.prototype.reject = function(fn){
  67.     return Array.reject(this,fn);
  68. };
  69.  
  70. Array.reduce = Array.inject;
  71. Array.prototype.reduce = Array.prototype.inject;
  72. Array.map = Array.collect;
  73. Array.prototype.map = Array.prototype.collect;

Use it like:

JavaScript:
  1. >>> var a = [1,2,3,4];
  2. >>> a.reduce(0, function(x,y){ return (x+y); });
  3. >>> 10

Basically, almost all functions are built on top of Array.each so I'm not sure how fast it'll go.

Happy hacking.

programming clojure

just acquired programming clojure book which i've been waiting for months (really, that long?). i'll be a happy hermit for weekends to come.

objective-c and clojure

spending my weekends context switching between Clojure and Objective-C/Cocoa. Clojure for filling my functional programming needs. Objective-C/Cocoa for creating pretty user interfaces for Mac and, hopefully, iPhone.

productive sunday

felt that this sunday is my most productive in recent months. it's even a father's day.

Trying out CakePHP

When I was evaluating PHP web frameworks that I would focus on and use on a regular basis, I didn't go with CakePHP. Instead, I went with CodeIgniter for reason that I was able make and run a sample app with CodeIgniter a few minutes after install while I was pulling hairs with CakePHP.

But not anymore. I had a pleasant experience experimenting with CakePHP over the last couple of days. It might be that I have I already know Rails which made Cake's approach fits my brain easily as Cake and Rails have lots of similarities.

Deciding which PHP framework to use on the next project will be a very hard decision to make for me. Cake's ActiveRecord implementation is better than that of CI. On the other hand, CI gives me full control of almost everything.

Rails vs Django Comparison

Scratching your head which one to use? Read this comparison document. Yes, I was even more confused after. Why can't people take sides nowadays.

Prototype Modal CSS Window

Just discovered this amazing javascript library for modal CSS window over at http://prototype-window.xilinus.com. Too bad, I've hacked Lightbox Gone Wild from Particletree to fit our current project.

CodeIgniter 1.5.0 Release

Rick Ellis is on fire. Just nearly two months after CodeIgniter 1.4 was out, version 1.5 is released. I've been happy with version 1.4.1. But I can't wait to try 1.5's database transaction support.

Why PHP Sucks

PHP is a great language for web development. Although I hate it, but I can't blame tonguelashers for looking so low at PHP. This article is one good example why PHP suck. PHP newbies like to think they already knew everything and start writing tutorials.

Next Page »