<?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>Jozef Chúťka&#039;s blog &#187; shorten</title>
	<atom:link href="http://blog.yoz.sk/tag/shorten/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.yoz.sk</link>
	<description>My life, my work</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:40:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Game of life (update)</title>
		<link>http://blog.yoz.sk/2010/03/game-of-life/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=game-of-life</link>
		<comments>http://blog.yoz.sk/2010/03/game-of-life/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 14:38:01 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[fork]]></category>
		<category><![CDATA[game of life]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[pixel bender]]></category>
		<category><![CDATA[shorten]]></category>
		<category><![CDATA[Sprite]]></category>
		<category><![CDATA[with]]></category>
		<category><![CDATA[wonderfl]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=1296</guid>
		<description><![CDATA[The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is the best-known example of a cellular automaton. The &#8220;game&#8221; is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input from humans. One [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/gameoflige.jpg" alt="" title="gameoflige" width="200" height="100" class="alignleft size-full wp-image-1300" /></p>
<p><a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life">The Game of Life</a>, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is the best-known example of a cellular automaton. The &#8220;game&#8221; is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input from humans. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.</p>
<p><span id="more-1296"></span></p>
<h3 style="clear:both;">Shortest code</h3>
<p>While I like ActionScript the most, I decided to make a version in flash. It is also fine moment to introduce <a href="http://wonderfl.net">wonderfl</a> project. Wonderfl is a service where you can build Flash(swf) online. Wonderfl is online, free and social. After registration you are able to write and build your own codes, follow people, favourtie theirs work or even fork other user&#8217;s code (improve, change).</p>
<div style="text-align:center;width:465px;"><iframe title="Game of life - wonderfl build flash online" scrolling="no" src="http://wonderfl.net/blogparts/4c19f6a10cbe43c198cd98286b34fa4069f3e2f1" width="465" height="490" style="border:1px black solid;"></iframe><a href="http://wonderfl.net/code/4c19f6a10cbe43c198cd98286b34fa4069f3e2f1" title="Game of life - wonderfl build flash online">Game of life &#8211; wonderfl build flash online</a></div>
<p>My aim was to make the code shortest possible, so you may find it mess, hell yeah it is <img src='http://blog.yoz.sk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Try to fork <a href="http://wonderfl.net/code/4c19f6a10cbe43c198cd98286b34fa4069f3e2f1">my code on wonderfl</a> to achieve shortest possible. Use some <a href="http://blog.yoz.sk/2010/03/common-tricks-in-actionscript-code-shortening/">tricks to make your code shorter</a>.</p>
<pre class="brush: as3; title: ; notranslate">// Conway's Game of Life http://en.wikipedia.org/wiki/Conway's_Game_of_Life
// field 38x38 px

package{
    import flash.display.Sprite;

    public class GameOfLife extends Sprite{
        public function GameOfLife(){
            with(x)
            for each(i in w=h=38,a=new Array(w*h),b=[63,99,101,127,128,135,136,
                149,150,164,168,173,174,187,188,191,192,201,207,211,212,229,230,
                239,243,245,246,251,253,277,283,291,316,320,355,356])
                a[i]=1;
            addEventListener(&quot;enterFrame&quot;,function():void{
                with(graphics){
                for(clear(),c=[i=0];i&lt;w*h;i++)
                    X=i%w,Y=int(i/w),j=X-1,k=X+1,l=Y-1,m=Y+1,
                    n=int(j&gt;-1&amp;&amp;l&gt;-1?a[i-37]:0)
                    +int(l&gt;-1?a[i-w]:0)
                    +int(k&lt;w&amp;&amp;l&gt;-1?a[i-39]:0)
                    +int(j&gt;-1?a[i-1]:0)
                    +int(k&lt;w?a[i+1]:0)
                    +int(j&gt;-1&amp;&amp;m&lt;h?a[i+37]:0)
                    +int(m&lt;h?a[i+w]:0)
                    +int(k&lt;w&amp;&amp;m&lt;h?a[i+39]:0),
                    c[i]=a[i]?(n==2||n==3)?1:0:int(n==3),
                    a[i]?drawRect(X*10,Y*10,10,10):beginFill(0);
                    a=c;
                }
            });
        }
    }
}</pre>
<h3>Fastest code</h3>
<p>Here is my attempt for the fastest code, I have used Pixel Bender to achieve best performance, it seems pretty smooth considering, it is capable of 12.000.000 pixels testing in 1 second. You can <a href="http://blog.yoz.sk/examples/gameOfLife/gameoflife.pbk">download gameoflife.pbk file from here</a>. My first try was to use image1 input type, but image1 was not working correctly with sampleNearest() method &#8211; that seems to be the fastest sampler method in pixel bender. Then I switched to image4 and with sampleNearest() it resulted in much better performance compared to image1 and sample(). Other code optimizations were done to aviod as much if statements as possible.</p>
<div style="text-align:center;width:465px;"><iframe title="Fastest Game Of Life with Pixel Bender - wonderfl build flash online" scrolling="no" src="http://wonderfl.net/blogparts/iubp" width="465" height="490" style="border:1px black solid;"></iframe><a href="http://wonderfl.net/c/iubp" title="Fastest Game Of Life with Pixel Bender - wonderfl build flash online">Fastest Game Of Life with Pixel Bender &#8211; wonderfl build flash online</a></div>
<p>Where to go next</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life">onway&#8217;s Game of Life</a></li>
<li><a href="http://www.bitstorm.org/gameoflife/">John Conway&#8217;s Game of Life</a></li>
<li><a href="http://williamjacobson.com/weblog/actionscript-3-vs-pixel-bender-on-cellual-automata/">Game of life in Pixel Bender</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/03/game-of-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>URLShorten Class (update)</title>
		<link>http://blog.yoz.sk/2010/02/urlshorten-class/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=urlshorten-class</link>
		<comments>http://blog.yoz.sk/2010/02/urlshorten-class/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 10:52:18 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[shorten]]></category>
		<category><![CDATA[URLLoader]]></category>
		<category><![CDATA[URLLoaderDynamic]]></category>
		<category><![CDATA[URLRequest]]></category>
		<category><![CDATA[URLShorten]]></category>
		<category><![CDATA[URLShortenEvent]]></category>
		<category><![CDATA[URLVariables]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=989</guid>
		<description><![CDATA[URL shortening is a technique where a provider makes a web page available under a very short URL in addition to the original address. There are hundreds of provider out there, but only a few of them provides public api and crossdomain.xml. I put toghether some of those that work and URLShorten class was created [...]]]></description>
			<content:encoded><![CDATA[<p>URL shortening is a technique where a provider makes a web page available under a very short URL in addition to the original address. There are hundreds of provider out there, but only a few of them provides public api and crossdomain.xml. I put toghether some of those that work and URLShorten class was created <img src='http://blog.yoz.sk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  feel free to use it and enjoy </p>
<p>Update: <a href="http://jdem.cz">jdem.cz</a>, <a href="http://tinyurl.com">tinyurl.com</a>, <a href="http://bit.ly">bit.ly</a>, <a href="http://tr.im">tr.im</a>, <a href="http://is.gd">is.gd</a> providers implemented.</p>
<p><span id="more-989"></span></p>
<p>Application (view source enabled), please use your own api key for bit.ly</p>
<p><iframe width="100%" height="200" src="http://blog.yoz.sk/examples/URLShorten/"></iframe></p>
<p>I have just discovered tr.im has not crossdomain.xml, please do not us it while it throws errors. I have just asked them and few more providers to create crossdomain files, will see&#8230;</p>
<p>There are two methods you can handle resuls:</p>
<pre class="brush: as3; title: ; notranslate">// 1. use callback function
URLShorten.jdemcz(long_url.text, jdemczResult);

// ...callback function gets one parameter = short url
private function jdemczResult(url:String):void
{
    jdemcz = url;
}

// 2. addEventDispatcher to have bigger control
var dispatcher:EventDispatcher = URLShorten.tinyurlcom(long_url.text);
dispatcher.addEventListener(URLShortenEvent.COMPLETE, tinyurlcomComplete);

// ...dispatched event.url = short url
private function tinyurlcomComplete(event:URLShortenEvent):void
{
    tinyurl = event.url;
}</pre>
<p><a href="http://classes.yoz.sk/sk/yoz/net/URLShorten.as">URLShorten class</a></p>
<pre class="brush: as3; title: ; notranslate">package sk.yoz.net
{
    import com.adobe.serialization.json.JSON;

    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.net.URLVariables;

    import sk.yoz.events.URLShortenEvent;

    public class URLShorten extends Object
    {
        public function URLShorten()
        {
            super();
        }

        private static function prepare(request:URLRequest,
            variables:URLVariables, callback:Function,
            completeHandler:Function=null):URLLoaderDynamic
        {
            var loader:URLLoaderDynamic = new URLLoaderDynamic();
            var handler:Function = completeHandler || complete;

            request.data = variables;
            loader.callback = callback;
            loader.load(request);
            loader.addEventListener(Event.COMPLETE, handler);
            return loader;
        }

        private static function complete(event:Event):void
        {
            var loader:URLLoaderDynamic = URLLoaderDynamic(event.target);
            var url:String = String(loader.data);

            loader.removeEventListener(Event.COMPLETE, complete);
            dispatch(url, loader)
        }

        private static function dispatch(url:String, loader:URLLoaderDynamic)
            :void
        {
            var callback:Function = loader.callback;
            var type:String = URLShortenEvent.COMPLETE;

            if(callback != null)
                callback(url);
            loader.dispatchEvent(new URLShortenEvent(type, false, false, url));
        }

        public static function jdemcz(longUrl:String, callback:Function=null)
            :URLLoaderDynamic
        {
            var apiUrl:String = &quot;http://www.jdem.cz/get&quot;;
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();

            variables.url = longUrl;
            return prepare(request, variables, callback);
        }

        public static function tinyurlcom(longUrl:String,
            callback:Function=null):URLLoaderDynamic
        {
            var apiUrl:String = &quot;http://tinyurl.com/api-create.php&quot;;
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();

            variables.url = longUrl;
            return prepare(request, variables, callback);
        }

        public static function bitly(longUrl:String, login:String,
            apiKey:String, version:String=&quot;2.0.1&quot;, callback:Function=null)
            :URLLoaderDynamic
        {
            var apiUrl:String = &quot;http://api.bit.ly/shorten&quot;;
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();

            variables.version = version;
            variables.longUrl = longUrl;
            variables.login = login;
            variables.apiKey = apiKey;
            return prepare(request, variables, callback, bitlyComplete);
        }

        private static function bitlyComplete(event:Event):void
        {
            var loader:URLLoaderDynamic = URLLoaderDynamic(event.target);
            var result:String = String(loader.data);
            var data:Object = JSON.decode(result);
            var url:String;

            loader.removeEventListener(Event.COMPLETE, bitlyComplete);
            for each(var resultItem:Object in data.results)
                url = resultItem.shortUrl;
            dispatch(url, loader);
        }

        public static function trim(longUrl:String,
            callback:Function=null):URLLoaderDynamic
        {
            var apiUrl:String = &quot;http://api.tr.im/v1/trim_simple&quot;;
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();

            variables.url = longUrl;
            return prepare(request, variables, callback);
        }

        public static function isgd(longUrl:String,
            callback:Function=null):URLLoaderDynamic
        {
            var apiUrl:String = &quot;http://is.gd/api.php&quot;;
            var request:URLRequest = new URLRequest(apiUrl);
            var variables:URLVariables = new URLVariables();

            variables.longurl = longUrl;
            return prepare(request, variables, callback);
        }
    }
}</pre>
<p>we need loader to hold callback function reference, <a href="http://classes.yoz.sk/sk/yoz/net/URLLoaderDynamic.as">URLLoaderDynamic class</a> pretty much fits this needs</p>
<pre class="brush: as3; title: ; notranslate">package sk.yoz.net
{
    import flash.net.URLRequest;
    import flash.net.URLLoader;

    import sk.yoz.net.URLLoaderDynamic;

    public dynamic class URLLoaderDynamic extends URLLoader
    {
        public function URLLoaderDynamic(request:URLRequest=null)
        {
            super(request);
        }

    }
}</pre>
<p>custom <a href="http://classes.yoz.sk/sk/yoz/events/URLShortenEvent.as">URLShortenEvent class</a> is dispatched after url is returned</p>
<pre class="brush: as3; title: ; notranslate">package sk.yoz.events
{
    import flash.events.Event;

    public class URLShortenEvent extends Event
    {
        public static const COMPLETE:String = &quot;URLShortenEventCOMPLETE&quot;;

        private var _url:String;

        public function URLShortenEvent(type:String, bubbles:Boolean=false,
            cancelable:Boolean=false, url:String=&quot;&quot;)
        {
            super(type, bubbles, cancelable);
            _url = url;
        }

        public function get url():String
        {
            return _url;
        }
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/02/urlshorten-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

