<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>kuwamoto.org &#187; actionscript</title>
	<atom:link href="http://kuwamoto.org/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://kuwamoto.org</link>
	<description>music, technology, interfaces</description>
	<lastBuildDate>Tue, 03 Jan 2012 19:50:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Worth a read</title>
		<link>http://kuwamoto.org/2009/10/21/worth-a-read/</link>
		<comments>http://kuwamoto.org/2009/10/21/worth-a-read/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 16:44:24 +0000</pubDate>
		<dc:creator>sho</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://kuwamoto.org/?p=278</guid>
		<description><![CDATA[James Polanco of DevelopmentArc has a great post on Flash Player internals. Worth a read. http://www.developmentarc.com/site/2009/10/flash-player-internals-101-recap-part-one/]]></description>
			<content:encoded><![CDATA[<p>James Polanco of DevelopmentArc has a great post on Flash Player internals. Worth a read.</p>
<p><a href="http://www.developmentarc.com/site/2009/10/flash-player-internals-101-recap-part-one/">http://www.developmentarc.com/site/2009/10/flash-player-internals-101-recap-part-one/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kuwamoto.org/2009/10/21/worth-a-read/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The pluralizer: weekend results</title>
		<link>http://kuwamoto.org/2008/01/07/the-pluralizer-weekend-results/</link>
		<comments>http://kuwamoto.org/2008/01/07/the-pluralizer-weekend-results/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 20:07:58 +0000</pubDate>
		<dc:creator>sho</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://kuwamoto.org/2008/01/07/the-pluralizer-weekend-results/</guid>
		<description><![CDATA[Over the weekend, people tried out around three hundred words, and taught the pluralizer a bunch of new words. Some words I rejected: German words (Büch -> Bücher, for example) Kanji / Chinese (木 -> 木) Someone also claimed that the plural of &#8220;singularity&#8221; is &#8220;not applicable&#8221;. Uh&#8230; funny, but my Physics background tells me [...]]]></description>
			<content:encoded><![CDATA[<p>Over the weekend, people tried out around three hundred words, and taught the pluralizer a bunch of new words.</p>
<p>Some words I rejected:<br />
   German words (Büch -> Bücher, for example)<br />
   Kanji / Chinese (木 -> 木)</p>
<p>Someone also claimed that the plural of &#8220;singularity&#8221; is &#8220;not applicable&#8221;. Uh&#8230; funny, but my Physics background tells me that you can have more than one singularity. Black holes, anyone?</p>
<p>If you would like to feed the pluralizer, it <a href="http://apps.kuwamoto.org/pluralize/" target="_blank">can be found here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://kuwamoto.org/2008/01/07/the-pluralizer-weekend-results/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mob intelligence &#8211; Please feed the pluralizer</title>
		<link>http://kuwamoto.org/2008/01/04/mob-intelligence-please-feed-the-pluralizer/</link>
		<comments>http://kuwamoto.org/2008/01/04/mob-intelligence-please-feed-the-pluralizer/#comments</comments>
		<pubDate>Fri, 04 Jan 2008 21:40:48 +0000</pubDate>
		<dc:creator>sho</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://kuwamoto.org/2008/01/04/mob-intelligence-please-feed-the-pluralizer/</guid>
		<description><![CDATA[I&#8217;ve always been very interested in how groups of people come together to create a body of information. Now a few weeks ago, I posted some code to pluralize nouns. It&#8217;s based on the Ruby on Rails code, and it has been tweaked to include more words and ported to PHP and ActionScript. It occured [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always been very interested in how groups of people come together to create a body of information. </p>
<p>Now a few weeks ago, I posted some code to pluralize nouns. It&#8217;s based on the Ruby on Rails code, and it has been tweaked to include more words and ported to PHP and ActionScript.</p>
<p>It occured to me that maybe people out there could help make the pluralizer smarter. So I&#8217;ve written a small app that lets you input words to test out the pluralizer. If the pluralizer gets any of the words wrong, just type in the new word and the code will correct itself.</p>
<p><a href="http://apps.kuwamoto.org/pluralize/" target="_blank"><img src="http://kuwamoto.org/wp-content/uploads/2008/01/pluralizer.png" alt="pluralizer thumbnail" border="0" /></a></p>
<p>Will this work? I don&#8217;t know. It all depends on whether enough people find it interesting to teach the pluralizer new words. It also depends on people not screwing it up with junk words.</p>
<p><a href="http://apps.kuwamoto.org/pluralize/" target="_blank">Try it out</a> and LMK what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://kuwamoto.org/2008/01/04/mob-intelligence-please-feed-the-pluralizer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Improved pluralizing in PHP, ActionScript, and RoR</title>
		<link>http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/</link>
		<comments>http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 19:55:36 +0000</pubDate>
		<dc:creator>sho</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/</guid>
		<description><![CDATA[I found a number of issues with the pluralizing code I posted the other day, and I felt compelled to fix it. While I was at it, I ported the code to ActionScript 3 and then back to Ruby, so you can use the pluralizer in your Flex or RoR apps. BTW, for those of [...]]]></description>
			<content:encoded><![CDATA[<p>I found a number of issues with the pluralizing code I posted the other day, and I felt compelled to fix it. While I was at it, I ported the code to ActionScript 3 and then back to Ruby, so you can use the pluralizer in your Flex or RoR apps. </p>
<p>BTW, for those of you who love language and know regular expressions (this means you, lori and nj!), please help me spot any problems with these rules.</p>
<p>The reason it was important for me to get these pluralization rules rights is that I am using these for human-readable strings. In my project, users get to name things however they want and it is nice to be able to pluralize (or singularize) these words when communicating with the user.</p>
<p>(As a side note, I think the Ruby on Rails idea of auto-pluralizing table names to be a bit bizarre. I like having magic frameworks that do all the work for me, but I want my frameworks to have predictable behavior!! I mean&#8230; how bad would it be if the table that holds the &#8220;person&#8221; object was called &#8220;person&#8221; instead of &#8220;people&#8221;?)</p>
<p>Thanks again to the <a href="http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/inflections.rb">Rails team</a> for getting this ball rolling, and to <a href="http://www.eval.ca/articles/php-pluralize">Paul Osman</a> for the original PHP version of this code.</p>
<p>On to the code. All code below is covered under the MIT license.</p>
<p>PHP:</p>
<pre>// Thanks to http://www.eval.ca/articles/php-pluralize (MIT license)
//           http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/inflections.rb (MIT license)
//           http://www.fortunecity.com/bally/durrus/153/gramch13.html
//           http://www2.gsu.edu/~wwwesl/egw/crump.htm
//
// Changes (12/17/07)
//   Major changes
//   --
//   Fixed irregular noun algorithm to use regular expressions just like the original Ruby source.
//       (this allows for things like fireman -> firemen
//   Fixed the order of the singular array, which was backwards.
//
//   Minor changes
//   --
//   Removed incorrect pluralization rule for /([^aeiouy]|qu)ies$/ => $1y
//   Expanded on the list of exceptions for *o -> *oes, and removed rule for buffalo -> buffaloes
//   Removed dangerous singularization rule for /([^f])ves$/ => $1fe
//   Added more specific rules for singularizing lives, wives, knives, sheaves, loaves, and leaves and thieves
//   Added exception to /(us)es$/ => $1 rule for houses => house and blouses => blouse
//   Added excpetions for feet, geese and teeth
//   Added rule for deer -> deer

// Changes:
//   Removed rule for virus -> viri
//   Added rule for potato -> potatoes
//   Added rule for *us -> *uses

class Inflect
{
    static $plural = array(
        '/(quiz)$/i'               => "$1zes",
        '/^(ox)$/i'                => "$1en",
        '/([m|l])ouse$/i'          => "$1ice",
        '/(matr|vert|ind)ix|ex$/i' => "$1ices",
        '/(x|ch|ss|sh)$/i'         => "$1es",
        '/([^aeiouy]|qu)y$/i'      => "$1ies",
        '/(hive)$/i'               => "$1s",
        '/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
        '/(shea|lea|loa|thie)f$/i' => "$1ves",
        '/sis$/i'                  => "ses",
        '/([ti])um$/i'             => "$1a",
        '/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
        '/(bu)s$/i'                => "$1ses",
        '/(alias)$/i'              => "$1es",
        '/(octop)us$/i'            => "$1i",
        '/(ax|test)is$/i'          => "$1es",
        '/(us)$/i'                 => "$1es",
        '/s$/i'                    => "s",
        '/$/'                      => "s"
    );

    static $singular = array(
        '/(quiz)zes$/i'             => "$1",
        '/(matr)ices$/i'            => "$1ix",
        '/(vert|ind)ices$/i'        => "$1ex",
        '/^(ox)en$/i'               => "$1",
        '/(alias)es$/i'             => "$1",
        '/(octop|vir)i$/i'          => "$1us",
        '/(cris|ax|test)es$/i'      => "$1is",
        '/(shoe)s$/i'               => "$1",
        '/(o)es$/i'                 => "$1",
        '/(bus)es$/i'               => "$1",
        '/([m|l])ice$/i'            => "$1ouse",
        '/(x|ch|ss|sh)es$/i'        => "$1",
        '/(m)ovies$/i'              => "$1ovie",
        '/(s)eries$/i'              => "$1eries",
        '/([^aeiouy]|qu)ies$/i'     => "$1y",
        '/([lr])ves$/i'             => "$1f",
        '/(tive)s$/i'               => "$1",
        '/(hive)s$/i'               => "$1",
        '/(li|wi|kni)ves$/i'        => "$1fe",
        '/(shea|loa|lea|thie)ves$/i'=> "$1f",
        '/(^analy)ses$/i'           => "$1sis",
        '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i'  => "$1$2sis",
        '/([ti])a$/i'               => "$1um",
        '/(n)ews$/i'                => "$1ews",
        '/(h|bl)ouses$/i'           => "$1ouse",
        '/(corpse)s$/i'             => "$1",
        '/(us)es$/i'                => "$1",
        '/s$/i'                     => ""
    );

    static $irregular = array(
        'move'   => 'moves',
        'foot'   => 'feet',
        'goose'  => 'geese',
        'sex'    => 'sexes',
        'child'  => 'children',
        'man'    => 'men',
        'tooth'  => 'teeth',
        'person' => 'people'
    );

    static $uncountable = array(
        'sheep',
        'fish',
        'deer',
        'series',
        'species',
        'money',
        'rice',
        'information',
        'equipment'
    );

    public static function pluralize( $string )
    {
        // save some time in the case that singular and plural are the same
        if ( in_array( strtolower( $string ), self::$uncountable ) )
            return $string;

        // check for irregular singular forms
        foreach ( self::$irregular as $pattern => $result )
        {
            $pattern = '/' . $pattern . '$/i';

            if ( preg_match( $pattern, $string ) )
                return preg_replace( $pattern, $result, $string);
        }

        // check for matches using regular expressions
        foreach ( self::$plural as $pattern => $result )
        {
            if ( preg_match( $pattern, $string ) )
                return preg_replace( $pattern, $result, $string );
        }

        return $string;
    }

    public static function singularize( $string )
    {
        // save some time in the case that singular and plural are the same
        if ( in_array( strtolower( $string ), self::$uncountable ) )
            return $string;

        // check for irregular plural forms
        foreach ( self::$irregular as $result => $pattern )
        {
            $pattern = '/' . $pattern . '$/i';

            if ( preg_match( $pattern, $string ) )
                return preg_replace( $pattern, $result, $string);
        }

        // check for matches using regular expressions
        foreach ( self::$singular as $pattern => $result )
        {
            if ( preg_match( $pattern, $string ) )
                return preg_replace( $pattern, $result, $string );
        }

        return $string;
    }

    public static function pluralize_if($count, $string)
    {
        if ($count == 1)
            return "1 $string";
        else
            return $count . " " . self::pluralize($string);
    }
}</pre>
<p>ActionScript:</p>
<pre>package
{
public class Inflect
{
    private static var plural : Array = [
        [/(quiz)$/i,                     "$1zes"],
        [/^(ox)$/i,                      "$1en"],
        [/([m|l])ouse$/i,                "$1ice"],
        [/(matr|vert|ind)ix|ex$/i,       "$1ices"],
        [/(x|ch|ss|sh)$/i,               "$1es"],
        [/([^aeiouy]|qu)y$/i,            "$1ies"],
        [/(hive)$/i,                     "$1s"],
        [/(?:([^f])fe|([lr])f)$/i,       "$1$2ves"],
        [/(shea|lea|loa|thie)f$/i,       "$1ves"],
        [/sis$/i,                        "ses"],
        [/([ti])um$/i,                   "$1a"],
        [/(tomat|potat|ech|her|vet)o$/i, "$1oes"],
        [/(bu)s$/i,                      "$1ses"],
        [/(alias|status)$/i,             "$1es"],
        [/(octop)us$/i,                  "$1i"],
        [/(ax|test)is$/i,                "$1es"],
        [/(us)$/i,                       "$1es"],
        [/s$/i,                          "s"],
        [/$/i,                           "s"]
    ];

    private static var singular : Array = [
        [/(quiz)zes$/i,             "$1"],
        [/(matr)ices$/i,            "$1ix"],
        [/(vert|ind)ices$/i,        "$1ex"],
        [/^(ox)en$/i,               "$1"],
        [/(alias|status)es$/i,      "$1"],
        [/(octop|vir)i$/i,          "$1us"],
        [/(cris|ax|test)es$/i,      "$1is"],
        [/(shoe)s$/i,               "$1"],
        [/(o)es$/i,                 "$1"],
        [/(bus)es$/i,               "$1"],
        [/([m|l])ice$/i,            "$1ouse"],
        [/(x|ch|ss|sh)es$/i,        "$1"],
        [/(m)ovies$/i,              "$1ovie"],
        [/(s)eries$/i,              "$1eries"],
        [/([^aeiouy]|qu)ies$/i,     "$1y"],
        [/([lr])ves$/i,             "$1f"],
        [/(tive)s$/i,               "$1"],
        [/(hive)s$/i,               "$1"],
        [/(li|wi|kni)ves$/i,        "$1fe"],
        [/(shea|loa|lea|thie)ves$/i,"$1f"],
        [/(^analy)ses$/i,           "$1sis"],
        [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i,  "$1$2sis"],
        [/([ti])a$/i,               "$1um"],
        [/(n)ews$/i,                "$1ews"],
        [/(h|bl)ouses$/i,           "$1ouse"],
        [/(corpse)s$/i,             "$1"],
        [/(us)es$/i,                "$1"],
        [/s$/i,                     ""]
    ];

    private static var irregular : Array = [
        ['move'   , 'moves'],
        ['foot'   , 'feet'],
        ['goose'  , 'geese'],
        ['sex'    , 'sexes'],
        ['child'  , 'children'],
        ['man'    , 'men'],
        ['tooth'  , 'teeth'],
        ['person' , 'people']
    ];

    private static var uncountable : Array = [
        'sheep',
        'fish',
        'deer',
        'series',
        'species',
        'money',
        'rice',
        'information',
        'equipment'
    ];

    public static function pluralize( string : String ) : String
    {
        var pattern : RegExp;
        var result : String;

        // save some time in the case that singular and plural are the same
        if (uncountable.indexOf(string.toLowerCase()) != -1)
          return string;

        // check for irregular singular forms
        var item : Array;
        for each ( item in irregular )
        {
            pattern = new RegExp(item[0] + "$", "i");
            result = item[1];

            if (pattern.test(string))
            {
                return string.replace(pattern, result);
            }
        }

        // check for matches using regular expressions
        for each ( item in plural)
        {
            pattern = item[0];
            result = item[1];

            if (pattern.test(string))
            {
                return string.replace(pattern, result);
            }
        }

        return string;
    }

    public static function singularize( string : String ) : String
    {
        var pattern : RegExp;
        var result : String

        // save some time in the case that singular and plural are the same
        if (uncountable.indexOf(string.toLowerCase()) != -1)
            return string;

        // check for irregular singular forms
        var item : Array;
        for each ( item in irregular )
        {
            pattern = new RegExp(item[1] + "$", "i");
            result = item[0];

            if (pattern.test(string))
            {
                return string.replace(pattern, result);
            }
       }

       // check for matches using regular expressions
       for each ( item in singular)
       {
            pattern = item[0];
            result = item[1];

            if (pattern.test(string))
            {
                return string.replace(pattern, result);
            }
       }

       return string;

    }

    public static function pluralizeIf(count : int, string : String) : String
    {
        if (count == 1)
            return "1 " + string;
        else
            return count.toString() + " " + pluralize(string);
    }
}
}</pre>
<p>Ruby on Rails (use this to replace your Inflect.rb):</p>
<pre>Inflector.inflections do |inflect|
    inflect.plural(/$/, 's')
    inflect.plural(/s$/i, 's')
    inflect.plural(/(us)$/i, '\\1es')
    inflect.plural(/(ax|test)is$/i, '\\1es')
    inflect.plural(/(octop)us$/i, '\\1i')
    inflect.plural(/(alias)$/i, '\\1es')
    inflect.plural(/(bu)s$/i, '\\1ses')
    inflect.plural(/(tomat|potat|ech|her|vet)o$/i, '\\1oes')
    inflect.plural(/([ti])um$/i, '\\1a')
    inflect.plural(/sis$/i, 'ses')
    inflect.plural(/(shea|lea|loa|thie)f$/i, '\\1ves')
    inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\\1\\2ves')
    inflect.plural(/(hive)$/i, '\\1s')
    inflect.plural(/([^aeiouy]|qu)y$/i, '\\1ies')
    inflect.plural(/(x|ch|ss|sh)$/i, '\\1es')
    inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\\1ices')
    inflect.plural(/([m|l])ouse$/i, '\\1ice')
    inflect.plural(/^(ox)$/i, '\\1en')
    inflect.plural(/(quiz)$/i, '\\1zes')

    inflect.singular(/s$/i, '')
    inflect.singular(/(us)es$/i, '\\1')
    inflect.singular(/(corpse)s$/i, '\\1')
    inflect.singular(/(h|bl)ouses$/i, '\\1ouse')
    inflect.singular(/(n)ews$/i, '\\1ews')
    inflect.singular(/([ti])a$/i, '\\1um')
    inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\\1\\2sis')
    inflect.singular(/(^analy)ses$/i, '\\1sis')
    inflect.singular(/(shea|loa|lea|thie)ves$/i, '\\1f')
    inflect.singular(/(li|wi|kni)ves$/i, '\\1fe')
    inflect.singular(/(hive)s$/i, '\\1')
    inflect.singular(/(tive)s$/i, '\\1')
    inflect.singular(/([lr])ves$/i, '\\1f')
    inflect.singular(/([^aeiouy]|qu)ies$/i, '\\1y')
    inflect.singular(/(s)eries$/i, '\\1eries')
    inflect.singular(/(m)ovies$/i, '\\1ovie')
    inflect.singular(/(x|ch|ss|sh)es$/i, '\\1')
    inflect.singular(/([m|l])ice$/i, '\\1ouse')
    inflect.singular(/(bus)es$/i, '\\1')
    inflect.singular(/(o)es$/i, '\\1')
    inflect.singular(/(shoe)s$/i, '\\1')
    inflect.singular(/(cris|ax|test)es$/i, '\\1is')
    inflect.singular(/(octop|vir)i$/i, '\\1us')
    inflect.singular(/(alias)es$/i, '\\1')
    inflect.singular(/^(ox)en/i, '\\1')
    inflect.singular(/(vert|ind)ices$/i, '\\1ex')
    inflect.singular(/(matr)ices$/i, '\\1ix')
    inflect.singular(/(quiz)zes$/i, '\\1')

    inflect.irregular('person', 'people')
    inflect.irregular('tooth', 'teeth')
    inflect.irregular('man', 'men')
    inflect.irregular('child', 'children')
    inflect.irregular('sex', 'sexes')
    inflect.irregular('goose', 'geese')
    inflect.irregular('foot', 'feet')
    inflect.irregular('move', 'moves')

    inflect.uncountable(%w(equipment information rice money species series deer fish sheep))
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Update: Avoid ints in ActionScript</title>
		<link>http://kuwamoto.org/2006/06/15/update-avoid-ints-in-actionscript/</link>
		<comments>http://kuwamoto.org/2006/06/15/update-avoid-ints-in-actionscript/#comments</comments>
		<pubDate>Thu, 15 Jun 2006 21:07:07 +0000</pubDate>
		<dc:creator>sho</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/update-avoid-ints-in-actionscript/</guid>
		<description><![CDATA[After my post, Grant Skinner did some experiments around the discrepancy between Number and int performance. He originally posted a basic test, but has since expanded his testcases to show how different mathematical operations perform. You can see his results here.]]></description>
			<content:encoded><![CDATA[<p>After my post, Grant Skinner did some experiments around the discrepancy between Number and int performance. He originally posted a basic test, but has since expanded his testcases to show how different mathematical operations perform.</p>
<p>You can <a href="http://www.gskinner.com/blog/archives/2006/06/types_in_as3_in.html">see his results here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kuwamoto.org/2006/06/15/update-avoid-ints-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Avoid ints in ActionScript</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/</link>
		<comments>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comments</comments>
		<pubDate>Thu, 15 Jun 2006 17:31:06 +0000</pubDate>
		<dc:creator>sho</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/</guid>
		<description><![CDATA[The more I play with Flex, the more I learn, and the more I learn about ints, the less I want to use them. I&#8217;ve concluded that I&#8217;m going to stop using ints unless I really need them. Reason 1: Numbers may actually be faster than ints Surprising, but true. ECMAScript Edition 4 is designed [...]]]></description>
			<content:encoded><![CDATA[<p>The more I play with Flex, the more I learn, and the more I learn about ints, the less I want to use them. I&#8217;ve concluded that I&#8217;m going to stop using ints unless I really need them.</p>
<h3>Reason 1: Numbers may actually be faster than ints</h3>
<p>Surprising, but true. ECMAScript Edition 4 is designed to be a language that is as compatible as possible with earlier versions of ECMAScript. As it turns out, this makes it difficult to ensure that math works &#8220;correctly&#8221; in seemingly innocuous cases.</p>
<pre>public function timingTest() : void
{
	var intTime : Number;
	var numberTime : Number;

	var i : int;
	var j : int = 0;

	intTime = (new Date()).time;
	for (i=0; i<10000000; i++)
		j = (j + 15) / 7;

	intTime = (new Date()).time - intTime;

	var n : Number;
	var m : Number = 0;

	numberTime = (new Date()).time;
	for (n=0; n<10000000; n++)
		m = (m + 15) / 7;

	numberTime = (new Date()).time - numberTime;

	var message : String =
		"int version: " + intTime + "ms\n" +
		"Number version: " + numberTime + "ms";

	Alert.show(message);
}</pre>
<p>Which version do you think wins? On my machine, the int version takes 331ms, while the Number version takes 291ms. Why is this? Let's look at the following expression:</p>
<pre>j = (j + 15) / 7;</pre>
<p>What happens if you start with the value j = 2^31 - 1? In some languages, you would run into overflow issues as soon as you add 15 to it. ECMAScript, however, has a looser concept of numbers. The system is supposed to move smoothly from ints to doubles as needed. Because of this, <strong>virtually all math is done internally as Number, not as int.</strong></p>
<p>Given that everything is being done as a Number anyway, the extra cost of converting from int to Number and back again takes even more time, which is why the int version is slower.</p>
<p>There is a second counterinuitive reason for using Number over int, which is that Number actually lets you store integral values more precisely than ints do...<br />
<span id="more-81"></span></p>
<h2 class="separator">~</h2>
<h3>Reason 2: Numbers have more bits</h3>
<p>This is a surprisng fact about numbers that is obvious in hindsight. Let me tell you how I ran into it.</p>
<p>The Date object has a property called time that gives the number of milliseconds since midnight, Jan 1, 1970. Because it is an integral value, and because there are no longs in ActionScript, I just assumed that the return type was really an int (or possibly a uint):</p>
<pre>var time : uint = (new Date()).time;</pre>
<p>What's the bug? A quick <a href="http://everything2.com/index.pl?node_id=1423055">back of the envelope calculation</a> will tell you that there have been more than 2^32 milliseconds since Jan 1, 1970, which results in an overflow. Stupid mistake.</p>
<p>But it makes you wonder... If you can't fit that many bits into an int, and if ActionScript doesn't have the concept of a long, why can you do this without overflow or losing precision:</p>
<pre>var time : Number = (new Date()).time;</pre>
<p>Underneath the hood, a Number is equivalent to one of the following numeric types:</p>
<ul>
<li>int</li>
<li>uint</li>
<li>IEEE double</li>
</ul>
<p>I always shy away from using doubles for integer math, because you are never guaranteed that it will keep all of your digits. In my mind, a double can store bigger ranges of numbers than ints (because of the exponent) at the cost of less precision.</p>
<p>As it turns out, in the case of ActionScript, a double can store bigger ranges of numbers (because of the exponent) <strong>and</strong> keep more precision than any of the integral values. This is because ActionScript doesn't have a 64 bit integer type.</p>
<p>The IEEE double format looks like this:</p>
<pre>  S EEEEEEEEEEE FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  0 1        11 12                                                63</pre>
<p>There is one sign bit, 11 bits for the exponent, and 52 bits for the integral portion of the number. Thus, you can store numbers that are quite a bit larger than will fit into an int without losing any precision.</p>
<h3>When would you ever want to use an int?</h3>
<p>There are legitimate uses for ints.</p>
<ol>
<li>When you want to save memory. (although this should only impact you when you have tons and tons and tons of numbers to store)</li>
<li>When you want to force an integral value (e.g., var i: int = j / 2)</li>
<li>To reduce the chance of bugs as you map integral values from client side to server side. (e.g., int fields in value objects).</li>
</ol>
<p>That having been said, I'm going to stick to Numbers for most of my code from now on.</p>
]]></content:encoded>
			<wfw:commentRss>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>AS3 &#8212; on the lack of private and protected constructors</title>
		<link>http://kuwamoto.org/2006/04/05/as3-on-the-lack-of-private-and-protected-constructors/</link>
		<comments>http://kuwamoto.org/2006/04/05/as3-on-the-lack-of-private-and-protected-constructors/#comments</comments>
		<pubDate>Wed, 05 Apr 2006 16:03:49 +0000</pubDate>
		<dc:creator>sho</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://kuwamoto.org/?p=54</guid>
		<description><![CDATA[As I was talking about using objects as enums, someone made a comment about the lack of private and protected constructors. I know that the this is a sore spot, so I thought I&#8217;d explain my view of how we got here, and give my two cents. The problem is that unlike what we did [...]]]></description>
			<content:encoded><![CDATA[<p>As I was talking about using objects as enums, someone made a comment about the lack of private and protected constructors. I know that the this is a sore spot, so I thought I&#8217;d explain my view of how we got here, and give my two cents. </p>
<p>The problem is that unlike what we did with ActionScript 2, we are working tightly with people from Mozilla and others to standardize on ECMAScript edition 4. The ECMAScript standard is not final, but we are adhering as closely as we can to the spec as it develops.<br />
<span id="more-54"></span></p>
<h2 class="separator">~</h2>
<p>Private and protected constructors is not something that the group has been able to tackle yet, and it is a nontrivial change to the language that requires lots of careful thought.</p>
<p>Ideally, we would have been able to think through all the ramifications of private and protected constructors and work with other ECMA members to make sure the design was sound, but that just wasn&#8217;t possible in the amount of time we had.</p>
<p>Given this, there were three choices:</p>
<p>1) Delay ActionScipt 3 (and Flex, etc.) significantly.<br />
2) Just &#8220;go with&#8221; a quick and dirty implementation of private and protected constructors for AS3 and risk compatibility problems down the road once the ECMA spec becomes final.<br />
3) Don&#8217;t allow private and protected constructors for now.</p>
<p>We went with option (3). </p>
<p>The main uses of private and protected constructors are singletons and abstract base classes. In both of these cases, I agree that the lack of real private and protected constructors is a pain. There are hackarounds which will work for now, but I am eagerly awaiting the day when we don&#8217;t have to use them anymore.</p>
<p>As for the specific use case described in the previous post (using objects as enumerations), the main thing that private/protected constructors allows you to do is to ensure that the set of &#8220;enum&#8221; values cannot be extended later by someone else.</p>
<p>I personally like having this level of control, but I am willing to live without it. It &#8220;feels better&#8221; to create a system where no one can add any more values to the enumeration, but in practice, </p>
<p>a) If people create new values for the enumeration and try to use them with code that wasn&#8217;t expecting those values, it will obviously break. So who is going to do this?</p>
<p>b) If people create new values for the enumeration and use them only within their own code&#8230; well&#8230; it kind of breaks the spirit of what an enumeration is, but it might be just fine.</p>
<p>Here&#8217;s a concrete example of scenario (b). Imagine that I create a text control with three values for alignment: LEFT, RIGHT, and CENTER. Someone subclasses this control, and wants to create a fourth enumeration value: JUSTIFY. This new value would obviously only be useful for this person&#8217;s subclass, but who am I to say that he/she shouldn&#8217;t create it?  </p>
<p>Like I said, I would ideally like the option of controlling what can and can&#8217;t get extended, but in the case of enumerations, I&#8217;m ok with not having that control for now.</p>
]]></content:encoded>
			<wfw:commentRss>http://kuwamoto.org/2006/04/05/as3-on-the-lack-of-private-and-protected-constructors/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

