<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users634</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/634" rel="self"/>
  <title>Tj Holowaychuk</title>
  <updated>Mon Jan 18 19:16:21 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor417761</id>
    <published>2010-01-18T19:16:21-08:00</published>
    <title>[Ruby] On ActiveRecord find_by_sql</title>
    <content type="html">&lt;p&gt;still there lol.. google sql injection&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1149-activerecord-find_by_sql/refactors/417761" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor417637</id>
    <published>2010-01-18T15:03:28-08:00</published>
    <title>[Ruby] On ActiveRecord find_by_sql</title>
    <content type="html">&lt;p&gt;well there is a huge security flaw in what you have now...&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1149-activerecord-find_by_sql/refactors/417637" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor386011</id>
    <published>2009-12-14T16:07:35-08:00</published>
    <title>[Ruby] On Stable sort in ruby</title>
    <content type="html">&lt;p&gt;whats wrong sort ?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1127-stable-sort-in-ruby/refactors/386011" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor375780</id>
    <published>2009-12-02T00:49:20-08:00</published>
    <title>[C] On Css minification</title>
    <content type="html">&lt;p&gt;I like keeping the state machines really simple, all the local vars kinda suck though, limited to macros&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1110-css-minification/refactors/375780" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor375778</id>
    <published>2009-12-02T00:47:36-08:00</published>
    <title>[C] On Css minification</title>
    <content type="html">&lt;p&gt;my bad, think your right. The macros were from another project that actually needed to buffer strings. I previously thought that fgetc() did some internal stream buffering, but I could not find the source or much info but yeah wow in my other project after just using fread() its a huge huge performance gain&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1110-css-minification/refactors/375778" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor370752</id>
    <published>2009-11-26T06:59:58-08:00</published>
    <title>[JavaScript] On Money formatting</title>
    <content type="html">&lt;p&gt;damn, mine is twice as slow lol to much baggage along with js arrays blah blah o well &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1111-money-formatting/refactors/370752" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor370719</id>
    <published>2009-11-26T05:42:41-08:00</published>
    <title>[JavaScript] On Money formatting</title>
    <content type="html">&lt;p&gt;It can be done pretty easily with regular expressoins, but im in C text parsing mode lol this is probably faster than regexps too not sure tho yet&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1111-money-formatting/refactors/370719" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1111</id>
    <published>2009-11-25T23:05:47-08:00</published>
    <updated>2010-01-05T10:13:50-08:00</updated>
    <title>[JavaScript] Money formatting</title>
    <content type="html">&lt;p&gt;Just needed to whip up a really quick routine for formatting money&lt;/p&gt;

&lt;pre&gt;
function format_money(n) {
  var buf = [], 
      str = parseFloat(n).toFixed(2).toString(), 
      i = str.length,
      s = str.length - 5,
      p = 0
  while (++p, i) {
    buf.push(str[--i])
    if (i &amp;amp;&amp;amp; i &amp;lt; s &amp;amp;&amp;amp; p % 3 == 0) buf.push(',')
  }
  return buf.reverse().join('')
}

print(format_money(125.99))
print(format_money(1.99))
print(format_money(15000.99))
print(format_money(1500000.99))
print(format_money(15000000000000.99))


// =&amp;gt;

125.99
1.99
15,000.99
1,500,000.99
15,000,000,000,000.99&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1111-money-formatting" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1110</id>
    <published>2009-11-25T22:05:35-08:00</published>
    <updated>2009-12-07T20:59:36-08:00</updated>
    <title>[C] Css minification</title>
    <content type="html">&lt;p&gt;Repo is here &lt;a href="http://github.com/visionmedia/cssmin" target="_blank"&gt;http://github.com/visionmedia/cssmin&lt;/a&gt; looking for any suggestions&lt;/p&gt;

&lt;pre&gt;// 
// cssmin.c
// 
// (c) 2009 TJ Holowaychuk &amp;lt;tj@vision-media.ca&amp;gt; (MIT Licensed)
//
 
#include &amp;quot;cssmin.h&amp;quot;
 
#define last l
#define undo (ungetc(n, stream), n)
#define next (n = fgetc(stream), undo)
#define buffer (buf[bufpos++] = c)
#define end (buf[bufpos] = '\0')
#define out (++nchars_out, putchar(c))
#define input (l = c, c = fgetc(stream))
#define stat(S, ...) fprintf(stderr, S &amp;quot;\n&amp;quot;, __VA_ARGS__);
#define option(S) (strcmp(argv[1], S) == 0)
 
static int 
minify(FILE *stream) {
  clock_t start = clock();
  char c, l, n, buf[CSSMIN_BUFFER_MAX];
  int bufpos = 0, inblock = 0, nchars = 0, nchars_out = 0;
start:
  switch (++nchars, input) {
    case '\n': goto start;
    case '{' : goto block;
    case '}' : goto blockend;
    case ' ' : goto space;
    case '&amp;quot;' : goto string;
    case EOF : goto finish;
    default  : out;
  }
  goto start;
block:
  out; inblock = 1;
  goto start;
blockend:
  out; inblock = 0;
  goto start;
string:
  out;
  if (input != '&amp;quot;') goto string;
  else out;
  goto start;
space:
  if ((isalpha(last) &amp;amp;&amp;amp; isalpha(next)) || // foo bar
      (isalpha(last) &amp;amp;&amp;amp; isdigit(next)) || // 1px 0
      (isalpha(last) &amp;amp;&amp;amp; next == '&amp;quot;') ||   // 1px &amp;quot;solid&amp;quot;
      (last == '&amp;quot;' &amp;amp;&amp;amp; !ispunct(next)) ||  // &amp;quot;solid&amp;quot; red
      (!inblock &amp;amp;&amp;amp; next == ':') ||        // foo :last-child
      isdigit(last) ||                    // 0 0
      (next == '*' || last == '*')) out;  // foo * bar
  goto start;
finish:
  stat(&amp;quot;bytes: %d&amp;quot;, nchars);
  stat(&amp;quot;bytes saved: %d&amp;quot;, nchars - nchars_out);
  stat(&amp;quot;duration: %0.5f milliseconds&amp;quot;, (float) (clock() - start) / CLOCKS_PER_SEC);
  return 0;
}
 
static int
usage() {
  printf(&amp;quot;cssmin: %s\n&amp;quot;
         &amp;quot;usage: cssmin &amp;lt; in.css &amp;gt; out.min.css\n&amp;quot;, CSSMIN_VERSION);
  exit(0);
}
 
int 
main(int argc, char **argv) {
  if (argc &amp;gt; 1)
    if (option(&amp;quot;--help&amp;quot;)) usage();
    else if (option(&amp;quot;--version&amp;quot;)) puts(CSSMIN_VERSION), exit(0);
  return minify(stdin);
}&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1110-css-minification" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor365060</id>
    <published>2009-11-18T16:39:24-08:00</published>
    <title>[C] On LPT garland</title>
    <content type="html">&lt;p&gt;SIGINT interrupts normally no? why handle it and say it cant be handled lol id probably just leave it out completely if thats all your doing &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1105-lpt-garland/refactors/365060" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor362385</id>
    <published>2009-11-16T16:54:55-08:00</published>
    <title>[PHP] On An easy one: traversing through an array</title>
    <content type="html">&lt;p&gt;If only PHP did not suck so much you could do this:&lt;/p&gt;

&lt;pre&gt;function GET($key, $default = null) {
  return $_GET[$key] || $default;
}&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1101-an-easy-one-traversing-through-an-array/refactors/362385" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor359752</id>
    <published>2009-11-13T22:27:08-08:00</published>
    <title>[Ruby] On lexer</title>
    <content type="html">&lt;p&gt;ya an enumerator would work fine, this is 1.8.x tho&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1098-lexer/refactors/359752" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1098</id>
    <published>2009-11-13T16:41:42-08:00</published>
    <updated>2009-11-16T22:13:32-08:00</updated>
    <title>[Ruby] lexer</title>
    <content type="html">&lt;p&gt;Everything else in my lib is pretty elegant, however this part bugs me. I cant use #find since I need the value the method  send(token) returns, not the token name itself. Is there a baked in iterator that I forgot about that would work good here?&lt;/p&gt;

&lt;pre&gt;    def get_token
      self.class.precedence.each do |token|
        if val = send(token)
          return val
        end
      end
      error 'syntax error'
    end&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1098-lexer" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor352319</id>
    <published>2009-11-01T20:56:09-08:00</published>
    <title>[JavaScript] On Need help with creating a loop</title>
    <content type="html">&lt;p&gt;dont use for / in with an array&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1085-need-help-with-creating-a-loop/refactors/352319" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor349136</id>
    <published>2009-10-29T20:30:00-07:00</published>
    <title>[C] On Enumerator</title>
    <content type="html">&lt;p&gt;Just playing around with a little prototype based lang im working on for fun. I prefer C's simplicity to c++ (though i dont know much c++).&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1082-enumerator/refactors/349136" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1082</id>
    <published>2009-10-29T16:53:06-07:00</published>
    <updated>2009-11-02T04:48:09-08:00</updated>
    <title>[C] Enumerator</title>
    <content type="html">&lt;p&gt;yup&lt;/p&gt;

&lt;pre&gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

typedef struct {
  int (* next)();
  void (* reset)();
} Enumerator;

Enumerator *
Enumerator_new(int (* next)(), void (* reset)()) {
  Enumerator *self = (Enumerator *) malloc(sizeof(Enumerator));
  if (!self) fprintf(stderr, &amp;quot;failed to allocate memory for Enumerator\n&amp;quot;);
  self-&amp;gt;next = next;
  self-&amp;gt;reset = reset;
  return self;
}

static int n = 0;
static int evens() { return n += 2; }
static void reset() { n = 0; }

int 
main(int argc, char **argv) {
  int n;
  Enumerator *e = Enumerator_new(evens, reset);
  while ((n = e-&amp;gt;next()) &amp;lt; 50)
    printf(&amp;quot;%d\n&amp;quot;, n);
  return 0;
}&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1082-enumerator" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor344798</id>
    <published>2009-10-19T21:17:35-07:00</published>
    <title>[C] On C Array Implementation</title>
    <content type="html">&lt;p&gt;ah lame. as far as i know this is the only exception&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1074-c-array-implementation/refactors/344798" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor344701</id>
    <published>2009-10-19T17:30:36-07:00</published>
    <title>[JavaScript] On My1stJavascript: Dice roller</title>
    <content type="html">&lt;p&gt;Check out my tiny js-oo lib, if your more comfortable with classes over prototypal idioms &lt;a href="http://github.com/visionmedia/js-oo" target="_blank"&gt;http://github.com/visionmedia/js-oo&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;
Dice = Class({
  init: function(){
    ...
  },

  roll: function(){
    ...
  }
})&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1075-my1stjavascript-dice-roller/refactors/344701" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor344700</id>
    <published>2009-10-19T17:29:39-07:00</published>
    <title>[JavaScript] On My1stJavascript: Dice roller</title>
    <content type="html">&lt;p&gt;Your not utilizing the OO aspects of javascript. &lt;/p&gt;

&lt;pre&gt;
;(function(){
  Dice = function() {
    
  }

  Dice.prototype = {
    roll: function(){ ... }
    renderTo: function(){ ... }
  }
})

(new Dice()).roll().renderTo('#some-element')&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1075-my1stjavascript-dice-roller/refactors/344700" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor344636</id>
    <published>2009-10-19T15:16:15-07:00</published>
    <title>[C] On C Array Implementation</title>
    <content type="html">&lt;p&gt;I dont know any C++ (well a tiny bit). that is ansi c99 no?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1074-c-array-implementation/refactors/344636" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1074</id>
    <published>2009-10-19T00:24:22-07:00</published>
    <updated>2010-09-30T17:48:11-07:00</updated>
    <title>[C] C Array Implementation</title>
    <content type="html">&lt;p&gt;im no c pro. may be some memory management issues. from my project &amp;quot;CKit&amp;quot; at &lt;a href="http://github.com/visionmedia/ckit" target="_blank"&gt;http://github.com/visionmedia/ckit&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;
// 
// array.h - CKit
// 
// (c) 2009 TJ Holowaychuk &amp;lt;tj@vision-media.ca&amp;gt; (MIT Licensed)
//

#ifndef __CKIT_ARRAY__
#define __CKIT_ARRAY__

#include &amp;quot;ckit.h&amp;quot;

#define CKIT_ARRAY(N, T) CKIT_ARRAY_INIT(N, T, free)
#define CKIT_ARRAY_INIT(N, T, F)                                                      \
                                                                                      \
   typedef struct {                                                                   \
     int len, max;                                                                    \
     T *vals;                                                                         \
   } N;                                                                               \
                                                                                      \
  /*                                                                                  \
   * Allocate new N.                                                                  \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_new() {                                                                         \
    N *self = (N *) CKIT_MALLOC(sizeof(N));                                           \
    self-&amp;gt;len = 0, self-&amp;gt;max = 0;                                                     \
    self-&amp;gt;vals = NULL;                                                                \
    return self;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Push _val_ onto _self_.                                                          \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_push(N *self, T val) {                                                          \
    if (self-&amp;gt;len == self-&amp;gt;max)                                                       \
      self-&amp;gt;max = self-&amp;gt;max ? self-&amp;gt;max &amp;lt;&amp;lt; 1 : 2,                                     \
      self-&amp;gt;vals = (T *) CKIT_REALLOC(self-&amp;gt;vals, self-&amp;gt;max * sizeof(T));             \
    self-&amp;gt;vals[self-&amp;gt;len++] = val;                                                    \
    return self;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Slice _self_ at _beg_ to _len_.                                                  \
   *                                                                                  \
   *   Array_slice(array, 0, 3);                                                      \
   *   Array_slice(array, 5, 10);                                                     \
   *   Array_slice(array, 1, -1);                                                     \
   *   Array_slice(array, -5, -1);                                                    \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_slice(N *self, register int beg, register int len) {                            \
    N *slice = N##_new();                                                             \
    if (beg &amp;lt; 0) beg = self-&amp;gt;len + 1 -(-beg+1);                                       \
    if (len &amp;lt; 0) len = self-&amp;gt;len + 2 -(-len+1);                                       \
    if (beg &amp;lt; 0) beg = 0;                                                             \
    if (len &amp;lt; 0) len = 1;                                                             \
    if (self-&amp;gt;len &amp;amp;&amp;amp; len != 0)                                                        \
      for (; len &amp;gt; 0 &amp;amp;&amp;amp; beg &amp;lt; self-&amp;gt;len; --len, ++beg)                                \
        if (self-&amp;gt;vals[beg])                                                          \
          N##_push(slice, self-&amp;gt;vals[beg]);                                           \
    return slice;                                                                     \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Return elements after _i_.                                                       \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_from(N *self, const int i) {                                                    \
    return N##_slice(self, i, -1);                                                    \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Return elements upto _i_.                                                        \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_to(N *self, int i) {                                                            \
    return N##_slice(self, 0, ++i);                                                   \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Return last _n_ elements.                                                        \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_last_n(N *self, int n) {                                                        \
    return N##_slice(self, -n, n);                                                    \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Select from _self_ with _callback_. Returns                                      \
   * a new array of values which evaluate to 1.                                       \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_select(N *self, int (* callback)()) {                                           \
    N *selected = N##_new();                                                          \
    for (int i = 0; i &amp;lt; self-&amp;gt;len; ++i)                                               \
      if (callback(i, self-&amp;gt;vals[i]))                                                 \
        N##_push(selected, self-&amp;gt;vals[i]);                                            \
    return selected;                                                                  \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Map _self_ with _callback_. Returns a new array                                  \
   * of mapped values.                                                                \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_map(N *self, void *(* callback)()) {                                            \
    N *map = N##_new();                                                               \
    for (int i = 0; i &amp;lt; self-&amp;gt;len; ++i)                                               \
      N##_push(map, callback(i, self-&amp;gt;vals[i]));                                      \
    return map;                                                                       \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Find an element in _self_ using _callback_. When _callback_                      \
   * evaluates to 1, the value will be returned, otherwise NULL.                      \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  T                                                                                   \
  N##_find(N *self, int (* callback)()) {                                             \
    for (int i = 0; i &amp;lt; self-&amp;gt;len; ++i)                                               \
      if (callback(i, self-&amp;gt;vals[i]))                                                 \
        return self-&amp;gt;vals[i];                                                         \
    return NULL;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Check if an element in _self_ will evaluate to 1                                 \
   * when passed to _callback_.                                                       \
   *                                                                                  \
   * @api public                                                                      \
   * @see Array_find()                                                                \
   */                                                                                 \
                                                                                      \
  int                                                                                 \
  N##_any(N *self, int (* callback)()) {                                              \
    return !! N##_find(self, callback);                                               \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Return the last element in _self_.                                               \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  T                                                                                   \
  N##_last(N *self) {                                                                 \
    return self-&amp;gt;vals[self-&amp;gt;len - 1];                                                 \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Pop an element off the end of _self_'s stack.                                    \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  T                                                                                   \
  N##_pop(N *self) {                                                                  \
    return self-&amp;gt;len ?                                                                \
      self-&amp;gt;vals[--self-&amp;gt;len] :                                                       \
        NULL;                                                                         \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Merge _self_ with _other_ array.                                                 \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_merge(N *self, N *other) {                                                      \
    for (int i = 0; i &amp;lt; other-&amp;gt;len; ++i)                                              \
      N##_push(self, other-&amp;gt;vals[i]);                                                 \
    return self;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Unshift _val_ onto _self_'s stack, prepending it.                                \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_unshift(N *self, T val) {                                                       \
    *self = *N##_merge(N##_push(N##_new(), val), self);                               \
    return self;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Delete element at index _i_ from _self_.                                         \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  T                                                                                   \
  N##_delete_at(N *self, int i) {                                                     \
    register int len = self-&amp;gt;len;                                                     \
    if (i &amp;gt; -1 &amp;amp;&amp;amp; i &amp;lt; len) {                                                          \
      --self-&amp;gt;len;                                                                    \
      T val = self-&amp;gt;vals[i];                                                          \
      for (int j = i + 1; j &amp;lt; len; ++j, ++i)                                          \
        self-&amp;gt;vals[i] = self-&amp;gt;vals[j];                                                \
      return val;                                                                     \
    }                                                                                 \
    return NULL;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Remove duplicate values from _self_ using _callback_ for comparison.             \
   *                                                                                  \
   *  Array_unique(array, strcmp);                                                    \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_unique(N *self, int (* callback)()) {                                           \
    for (int i = 0; i &amp;lt; self-&amp;gt;len; ++i)                                               \
      for (int j = 0; j &amp;lt; self-&amp;gt;len; ++j)                                             \
        if (i != j &amp;amp;&amp;amp; callback(self-&amp;gt;vals[i], self-&amp;gt;vals[j]) == 0)                    \
          N##_delete_at(self, i);                                                     \
    return self;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Sort _self_ with comparison _callback_.                                          \
   *                                                                                  \
   *  static int                                                                      \
   *  compare_string(void *p1, void *p2) {                                            \
   *     return strcmp(* (char * const *) p1, * (char * const *) p2);                 \
   *  }                                                                               \
   *                                                                                  \
   *  Array_sort(array, compare_string);                                              \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_sort(N *self, int (* callback)(const void *, const void *)) {                   \
    qsort(self-&amp;gt;vals, self-&amp;gt;len - 1, sizeof(T), callback);                            \
    return self;                                                                      \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Create a union of _self_ and _other_ array. Merging                              \
   * and removing duplicates using _callback_ for comparison.                         \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  N *                                                                                 \
  N##_union(N *self, N *other, int (* callback)()) {                                  \
    return N##_unique(N##_merge(self, other), callback);                              \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Shift an element off the beginning of _self_'s stack.                            \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  T                                                                                   \
  N##_shift(N *self) {                                                                \
    return N##_delete_at(self, 0);                                                    \
  }                                                                                   \
                                                                                      \
  /*                                                                                  \
   * Destroy _self_.                                                                  \
   *                                                                                  \
   * @api public                                                                      \
   */                                                                                 \
                                                                                      \
  void                                                                                \
  N##_destroy(N *self) {                                                              \
    for (int i = 0; i &amp;lt; self-&amp;gt;len; ++i)                                               \
      F(self-&amp;gt;vals[i]);                                                               \
    if (self) free(self);                                                             \
  }

#endif&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1074-c-array-implementation" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor343419</id>
    <published>2009-10-16T22:05:17-07:00</published>
    <title>[Ruby] On Roman numeral parser</title>
    <content type="html">&lt;p&gt;oh.. fuck.. haha I should have ... actually known that first I suppose :P &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1070-roman-numeral-parser/refactors/343419" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1070</id>
    <published>2009-10-16T18:43:55-07:00</published>
    <updated>2009-10-16T22:05:18-07:00</updated>
    <title>[Ruby] Roman numeral parser</title>
    <content type="html">&lt;p&gt;little parser. I am working with strings that may or may not be roman numerals, so to prevent a bunch of cruft we pass through regular numerical values&lt;/p&gt;

&lt;pre&gt;
module RomanNumeral
  
  #--
  # Numerals
  #++
  
  I = 1
  V = 5
  X = 10
  L = 50
  C = 100
  D = 500
  M = 1000
  
  ##
  # Parse roman numerals where _val_
  # may be Numeric, a string consisting of 
  # digits, or roman numerals.
  
  def self.parse val
    return val if Numeric === val
    return val.to_i if val =~ /\A\d+\Z/
    val.scan(/[IVXLCDM]/).inject(0) do |sum, numeral| 
      sum + const_get(numeral) 
    end
  end
end


p RomanNumeral.parse('12')
p RomanNumeral.parse('II')
p RomanNumeral.parse('IV')
p RomanNumeral.parse('X')
p RomanNumeral.parse(1)
p RomanNumeral.parse(1.5)&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1070-roman-numeral-parser" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor342584</id>
    <published>2009-10-15T16:26:27-07:00</published>
    <title>[C] On Version comparison</title>
    <content type="html">&lt;p&gt;This allows for &amp;quot;1.0.0-alpha &amp;lt; 1.0.0-beta&amp;quot;, &amp;quot;yui3-1200 &amp;lt; yui3-9000&amp;quot; etc, but still the same issue you mentioned&lt;/p&gt;

&lt;pre&gt;int
Version_compare(Version *a, Version *b) {
  return (a-&amp;gt;major * 100 + a-&amp;gt;minor * 10 + a-&amp;gt;patch) - 
         (b-&amp;gt;major * 100 + b-&amp;gt;minor * 10 + b-&amp;gt;patch) +
         strcmp(a-&amp;gt;str, b-&amp;gt;str);
}&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1069-version-comparison/refactors/342584" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor342583</id>
    <published>2009-10-15T16:24:30-07:00</published>
    <title>[C] On Version comparison</title>
    <content type="html">&lt;p&gt;ya thats an issue. 2.0 and 1.10.0 == 0 currently which.. is no good lol. I need a better way to hash their position / value. Ideally I want to support arbitrary depth in terms of n.n.n.n.n+ so I can refactor for that, but first I should get this working better. Instead of 100 it could be 999 or something stupid&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Tj Holowaychuk</name>
      <email>tj@vision-media.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1069-version-comparison/refactors/342583" rel="alternate"/>
  </entry>
</feed>

