<?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>THE MOCK-TURTLE&#039;S STORY</title>
	<atom:link href="http://oitar.biz/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://oitar.biz/blog</link>
	<description>Flex, Python and other smart words</description>
	<lastBuildDate>Fri, 30 Sep 2011 17:49:26 +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>Meet ListCollectionMap</title>
		<link>http://oitar.biz/blog/2010/06/04/meet-listcollectionmap/</link>
		<comments>http://oitar.biz/blog/2010/06/04/meet-listcollectionmap/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 15:23:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[as3-collection-utils]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[ListCollectionMap]]></category>
		<category><![CDATA[ListCollectionView]]></category>

		<guid isPermaLink="false">http://oitar.biz/blog/?p=43</guid>
		<description><![CDATA[The original Flex&#8217;s ListCollectionView is a brilliant tool that brings together flex data binding magic and some functional programming abilities. ListCollectionView instances can filter data, sort data and can be joined into chains. So, it seems quite natural to have &#8220;map&#8221; &#8211; the most powerfull tool of traditional &#8220;sort/grep/map&#8221; triade &#8211; in databinding toolbox. Unfortunatelly, [...]]]></description>
			<content:encoded><![CDATA[<p>The original Flex&#8217;s ListCollectionView is a brilliant tool that brings together flex data binding magic and some functional programming abilities. ListCollectionView instances can filter data, sort data and can be joined into chains.</p>
<p>So, it seems quite natural to have &#8220;map&#8221; &#8211; the most powerfull tool of traditional &#8220;sort/grep/map&#8221; triade &#8211; in databinding toolbox. Unfortunatelly, the original ListCollectionView has only &#8220;sort&#8221; and &#8220;filter&#8221; features. Here is an approach to fill this gap:</p>
<p>The <a href="http://github.com/persky/as3-collection-utils">ListCollectionMap</a> class is a variation of ListCollectionView, and can be used in data binding chains mentioned earlier.<br />
It doesn&#8217;t know how to filter and sort things, instead it maps data using the mapping function passed in constructor.</p>
<p>Here is the example:</p>
<pre name="code" class="js">
			public var dataIntegers:ArrayCollection
                        = new ArrayCollection([1, 2, 3]); 

			public var mapIntegers:ListCollectionMap
                        = new ListCollectionMap(incrementMapping, dataIntegers); 

			public function incrementMapping(i:int):int
			{
				return i + 1
			}
</pre>
<p>The <em>mapIntegers</em> collection initially contains [2, 3, 4] and remaps its elements once the source elements are changed. E.g. <em>dataIntegers.addItem(5)</em> will cause the update resulting in adding &#8217;6&#8242; to <em>mapIntegers</em>.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_example1_202460890"
			class="flashmovie"
			width="520"
			height="320">
	<param name="movie" value="/articles/LCM/example1.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/articles/LCM/example1.swf"
			name="fm_example1_202460890"
			width="520"
			height="320">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Lets now look at the more interesting example, one-to-many mapping:</p>
<pre name="code" class="js">
			public var dataObjects:ArrayCollection
                        = new ArrayCollection([{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}]); 

			public var mapObjects:ListCollectionMap
                        = new ListCollectionMap(split, dataObjects); 

			public function split(o:Object):Object
			{
				return ListCollectionMapResult.fromArray([{c: o.a}, {c: o.b}])
			}
</pre>
<p>The mapping function will produce a pair of result items for each source item. They are regular, independent items of mapObjects: [ {c: 1}, {c: 2}, {c: 3}, {c: 4}, {c: 5}, {c: 6} ], but both resultant items are affected whenever corresponding source item is changed. Note, that we have to use a special return value here: ListCollectionMapResult. If the mapping function returns array it means that this array (and not its items) is the corresponding resultant item.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_example2_694555375"
			class="flashmovie"
			width="520"
			height="160">
	<param name="movie" value="/articles/LCM/example2.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/articles/LCM/example2.swf"
			name="fm_example2_694555375"
			width="520"
			height="160">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>At this point the idea of ListCollectionMap is clear. The last example is about using of ListCollectionMap and ListCollectionView together. Lets take the ArrayCollection (which is also a ListCollectionView itself) with the initial integer sequence from 10 to one. Lets then wrap it with ListCollectionView that will filter out all even values, pass it to ListCollectionMap, that multiplies each value by 2 and at last add ListCollectionView that will sort values. E.g. in Perl it would sound like:</p>
<pre name="code" class="js">
sort { $a <=> $b } map { $_ * 2 } grep { $_ % 2 } (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
</pre>
<p>In AS3 it will be a bit longer: <img src='http://oitar.biz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre name="code" class="js">
			public var dataIntegers:ArrayCollection
                        = new ArrayCollection([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); 

			public var filteringView:ListCollectionView;

			public var multMap:ListCollectionMap; 

			public var sortingView:ListCollectionView;

			public function filterFunction(i:int):Boolean
			{
				return i % 2 ? true : false
			}

			public function mapFunction(i:int):int
			{
				return i * 2
			}

			filteringView = new ListCollectionView(dataIntegers)
			filteringView.filterFunction = filterFunction
			filteringView.refresh()

			multMap = new ListCollectionMap(mapFunction, filteringView)

			sortingView = new ListCollectionView(multMap)
			sortingView.sort = new Sort()
			sortingView.refresh()
</pre>
<p>Both perl and as3 code will produce the sequence (2, 6, 10, 14, 18) as a result. Again, the sortingView content is affected whenever something is added, deleted, updated or moved in <em>dataIntegers </em>collection.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_example3_396344437"
			class="flashmovie"
			width="520"
			height="160">
	<param name="movie" value="/articles/LCM/example3.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/articles/LCM/example3.swf"
			name="fm_example3_396344437"
			width="520"
			height="160">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Also note, that the magic of ListCollectionView and ListCollectionMap is not for free: each instance of these classes uses a lot of additional memory under the hood. The ListCollectionView stores the index of filtered or sorted objects while ListCollectionMap has the full index of source items inside to be able to keep track on changes.</p>
<p>The source code and library can be downloaded <a href="http://github.com/persky/as3-collection-utils">here</a>. </p>
<h3 class="bsuite_related_bypageviews">People who looked at this item also looked at&#8230;</h3>
<ul class="bsuite_related">
<li><a href='http://oitar.biz/blog/about/'>About</a></li>
</ul>
<h3 class="bsuite_related">Related items</h3>
<ul class="bsuite_related">
<li><a href='http://oitar.biz/blog/2009/11/17/pretty-remote-class-registration-syntax-for-pyamf/'>Pretty remote class registration syntax for PyAMF</a></li>
<li><a href='http://oitar.biz/blog/about/'>About</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://oitar.biz/blog/2010/06/04/meet-listcollectionmap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pretty remote class registration syntax for PyAMF</title>
		<link>http://oitar.biz/blog/2009/11/17/pretty-remote-class-registration-syntax-for-pyamf/</link>
		<comments>http://oitar.biz/blog/2009/11/17/pretty-remote-class-registration-syntax-for-pyamf/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 23:33:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[PyAMF]]></category>
		<category><![CDATA[Python Class Decorators]]></category>
		<category><![CDATA[RemoteObject]]></category>
		<category><![CDATA[syntactic sugar]]></category>

		<guid isPermaLink="false">http://oitar.biz/blog/?p=4</guid>
		<description><![CDATA[Going to transmit an object of some class between your Flex application and Python server (using PyAMF) you first have to register a class alias in both frameworks for proper serialization into AMF and back. In AS3 you will probably write something like: package model { [RemoteClass(alias="model.MyClass")] public class MyClass { public var a:uint public [...]]]></description>
			<content:encoded><![CDATA[<p>Going to transmit an object of some class between your Flex application and Python server (using PyAMF) you first have to register a class alias in both frameworks for proper serialization into AMF and back.</p>
<p>In AS3 you will probably write something like:</p>
<pre name="code" class="js">
package model
{
	[RemoteClass(alias="model.MyClass")]
	public class MyClass { 

		public var a:uint
		public var b:String

	}
}
</pre>
<p>while the corresponding class on server side and its registration will look like:</p>
<pre name="code" class="python">

class MyClass:
    def __init__(self, *args, **kwargs):
        self.a = args[0]
        self.b = args[1]

pyamf.register_class(MyClass, "model.MyClass")
</pre>
<p>it seems tempting to have a syntax, for registering server side class in similar way as it is done in Flex:</p>
<pre name="code" class="python">
@RemoteClass(alias="model.MyClass")
class MyClass:
    def __init__(self, *args, **kwargs):
        self.a = args[0]
        self.b = args[1]
</pre>
<p>The magic is done with a <a href="http://www.python.org/dev/peps/pep-3129/">Class Decorator</a>, a quite new feature, presented in Python 2.6. </p>
<p>Here is the class decorator performing registration:</p>
<pre name="code" class="python">
class RemoteClass(object):

    def __init__(self, alias):
        self.alias = alias

    def __call__(self, klass):
        pyamf.register_class(klass, self.alias)
        return klass
</pre>
<p>Happy coding <img src='http://oitar.biz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3 class="bsuite_related_bypageviews">People who looked at this item also looked at&#8230;</h3>
<ul class="bsuite_related">
<li><a href='http://oitar.biz/blog/about/'>About</a></li>
</ul>
<h3 class="bsuite_related">Related items</h3>
<ul class="bsuite_related">
<li><a href='http://oitar.biz/blog/2010/06/04/meet-listcollectionmap/'>Meet ListCollectionMap</a></li>
<li><a href='http://oitar.biz/blog/about/'>About</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://oitar.biz/blog/2009/11/17/pretty-remote-class-registration-syntax-for-pyamf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

