<?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>Solution Hacker</title>
	<atom:link href="http://www.solutionhacker.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solutionhacker.com</link>
	<description>This blog provides solutions for enterpreneurs!</description>
	<lastBuildDate>Thu, 18 Apr 2013 23:09:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Ad Network vs Ad Exchange</title>
		<link>http://www.solutionhacker.com/ad-network-vs-ad-exchange/</link>
		<comments>http://www.solutionhacker.com/ad-network-vs-ad-exchange/#comments</comments>
		<pubDate>Tue, 12 Mar 2013 23:58:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1419</guid>
		<description><![CDATA[The video from OpenX gives you a good comparison between ad network and ad exchange. Among them, I found some of them quite important:
<ol>
	<li><strong>Transparency</strong> - publisher sees who is winning their inventory whereas advertiser can bid at the impression level based on the value of the impression they see.</li>
	<li><strong>Reach</strong> - I assume the exchange has much large audience reach than an individual ad network unless the ad network has its own unique audience reach.</li>
	<li><strong>Bid war</strong> - Giving good transparent, advertiser is willing to bid more that ends up higher publisher pay check.</li>
	<li><strong>Data becomes more important</strong> - If buyer uses RTB to bid on the impression, they need to develop their own data intelligence in order to value the impression and put the price on it.</li>
</ol>
<iframe src="http://www.youtube.com/embed/MBhnB-sWqy8" height="315" width="420" allowfullscreen="" frameborder="0"></iframe>

It is quite tempting for publishers and advertisers to jump into an exchange and see if they can make more money. However, there are some concerns I have:
<ol>
	<li>Does the exchange handle the frauds?</li>
	<li>Do they support different pricing models other than CPM?</li>
	<li>Do the rule based bidders need to rely on the audience segmentation info provided by the exchange?</li>
</ol>]]></description>
				<content:encoded><![CDATA[<p>The video from OpenX gives you a good comparison between ad network and ad exchange. Among them, I found some of them quite important:</p>
<ol>
<li><strong>Transparency</strong> &#8211; publisher sees who is winning their inventory whereas advertiser can bid at the impression level based on the value of the impression they see.</li>
<li><strong>Reach</strong> &#8211; I assume the exchange has much large audience reach than an individual ad network unless the ad network has its own unique audience reach.</li>
<li><strong>Bid war</strong> &#8211; Giving good transparent, advertiser is willing to bid more that ends up higher publisher pay check.</li>
<li><strong>Data becomes more important</strong> &#8211; If buyer uses RTB to bid on the impression, they need to develop their own data intelligence in order to value the impression and put the price on it.</li>
</ol>
<p><iframe src="http://www.youtube.com/embed/MBhnB-sWqy8" height="315" width="420" allowfullscreen="" frameborder="0"></iframe></p>
<p>It is quite tempting for publishers and advertisers to jump into an exchange and see if they can make more money. However, there are some concerns I have:</p>
<ol>
<li>Does the exchange handle the frauds?</li>
<li>Do they support different pricing models other than CPM?</li>
<li>Do the rule based bidders need to rely on the audience segmentation info provided by the exchange?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/ad-network-vs-ad-exchange/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Big Data Not So Cheap?</title>
		<link>http://www.solutionhacker.com/big-data-not-so-cheap/</link>
		<comments>http://www.solutionhacker.com/big-data-not-so-cheap/#comments</comments>
		<pubDate>Mon, 11 Feb 2013 05:24:33 +0000</pubDate>
		<dc:creator>Raymond Hon</dc:creator>
				<category><![CDATA[Big Data Solution]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1409</guid>
		<description><![CDATA[Today I come across a video from <strong>Pete Warden</strong>. It is really fascinating!! A powerful message from his talk I like so much is <strong>"You can employ 100 servers from Amazon for 1 hour in $10 bucks"</strong>. With such a low bar to jump in, a savvy tech guru now can do so much on the Net if he or she possesses a good knowledge in leveraging data and computing power available. Not only that, he also provides us a powerful toolkit called <a href="http://www.datasciencetoolkit.org/">Data Science Toolkit</a> to kick start our projects. Everyone should give thanks to such a wonderful guy for his contribution of "Open Big Data" movement. On the other hand, it is shameful for Facebook to start a legal case against him. If they don't like any info being leaked out, they should put their efforts to protect their users instead. Enough said, enjoy the show below:

<iframe style="border: 0; outline: 0;" src="http://cdn.livestream.com/embed/gigaombigdata?layout=4&#38;clip=pla_b6c19059-7812-4c56-81cf-e8738cd5c5da&#38;height=340&#38;width=560&#38;autoplay=false" height="340" width="560" frameborder="0" scrolling="no"></iframe>
<h2 style="text-align: left;">Reference</h2>
<ol>
	<li>He published a handbook that listed out many popular data source APIs. Some of them could be outdated but many of them are still good. However, I found a <a href="http://goldenbe.com/sites/default/files/Data_Source_Handbook.pdf">link</a> to access this book online.</li>
	<li>He published a glossary that listed out some NoSQL technologies. If you are interested, you can see this <a href="https://dl.dropbox.com/u/34441746/books/Big%20Data%20Glossary.pdf">link</a>.</li>
</ol>]]></description>
				<content:encoded><![CDATA[<p>Today I come across a video from <strong>Pete Warden</strong>. It is really fascinating!! A powerful message from his talk I like so much is <strong>&#8220;You can employ 100 servers from Amazon for 1 hour in $10 bucks&#8221;</strong>. With such a low bar to jump in, a savvy tech guru now can do so much on the Net if he or she possesses a good knowledge in leveraging data and computing power available. Not only that, he also provides us a powerful toolkit called <a href="http://www.datasciencetoolkit.org/">Data Science Toolkit</a> to kick start our projects. Everyone should give thanks to such a wonderful guy for his contribution of &#8220;Open Big Data&#8221; movement. On the other hand, it is shameful for Facebook to start a legal case against him. If they don&#8217;t like any info being leaked out, they should put their efforts to protect their users instead. Enough said, enjoy the show below:</p>
<p><iframe style="border: 0; outline: 0;" src="http://cdn.livestream.com/embed/gigaombigdata?layout=4&amp;clip=pla_b6c19059-7812-4c56-81cf-e8738cd5c5da&amp;height=340&amp;width=560&amp;autoplay=false" height="340" width="560" frameborder="0" scrolling="no"></iframe></p>
<h2 style="text-align: left;">Reference</h2>
<ol>
<li>He published a handbook that listed out many popular data source APIs. Some of them could be outdated but many of them are still good. However, I found a <a href="http://goldenbe.com/sites/default/files/Data_Source_Handbook.pdf">link</a> to access this book online.</li>
<li>He published a glossary that listed out some NoSQL technologies. If you are interested, you can see this <a href="https://dl.dropbox.com/u/34441746/books/Big%20Data%20Glossary.pdf">link</a>.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/big-data-not-so-cheap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unleash the true power of YQL</title>
		<link>http://www.solutionhacker.com/unleash-the-true-power-of-yql/</link>
		<comments>http://www.solutionhacker.com/unleash-the-true-power-of-yql/#comments</comments>
		<pubDate>Sun, 16 Dec 2012 07:30:52 +0000</pubDate>
		<dc:creator>Raymond Hon</dc:creator>
				<category><![CDATA[Backend]]></category>
		<category><![CDATA[Data Collection]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1398</guid>
		<description><![CDATA[<h2>What is YQL</h2>
It is a <strong>SQL like language</strong> created by Yahoo that harvests the information from the Net like how you interface the relational database. Underneath the hood, someone has done the job to bind the data sources and expose it as open table. As many has shared their works, more open tables are created for your usage. Apart from solving integration headaches, its async processing plus parallelism provides you a highly performed data hub. With the nice abstraction provided by YQL, we now can easily and quickly tap into the data from various sources for our app.
<h2>Taste the power of YQL</h2>
There are tons of information in the Net that tells people how to use the YQL. So, I don't plan to repeat it here. If you are interested in learning it, you can read the YQL guide <a href="http://developer.yahoo.com/yql/guide/">here</a>. If you are impatient like me, you may want to see it in action first before diving in. Below is a great presentation from Nagesh Susarla.
<div><object width="576" height="324" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashVars" value="vid=23267890&#38;" /><param name="allowfullscreen" value="true" /><param name="wmode" value="transparent" /><param name="src" value="http://d.yimg.com/m/up/ypp/default/player.swf" /><param name="flashvars" value="vid=23267890&#38;" /><embed width="576" height="324" type="application/x-shockwave-flash" src="http://d.yimg.com/m/up/ypp/default/player.swf" flashVars="vid=23267890&#38;" allowfullscreen="true" wmode="transparent" flashvars="vid=23267890&#38;" /></object></div>
<div></div>]]></description>
				<content:encoded><![CDATA[<h2>What is YQL</h2>
<p>It is a <strong>SQL like language</strong> created by Yahoo that harvests the information from the Net like how you interface the relational database. Underneath the hood, someone has done the job to bind the data sources and expose it as open table. As many has shared their works, more open tables are created for your usage. Apart from solving integration headaches, its async processing plus parallelism provides you a highly performed data hub. With the nice abstraction provided by YQL, we now can easily and quickly tap into the data from various sources for our app.</p>
<h2>Taste the power of YQL</h2>
<p>There are tons of information in the Net that tells people how to use the YQL. So, I don&#8217;t plan to repeat it here. If you are interested in learning it, you can read the YQL guide <a href="http://developer.yahoo.com/yql/guide/">here</a>. If you are impatient like me, you may want to see it in action first before diving in. Below is a great presentation from Nagesh Susarla.</p>
<div><object width="576" height="324" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="flashVars" value="vid=23267890&amp;" /><param name="allowfullscreen" value="true" /><param name="wmode" value="transparent" /><param name="src" value="http://d.yimg.com/m/up/ypp/default/player.swf" /><param name="flashvars" value="vid=23267890&amp;" /><embed width="576" height="324" type="application/x-shockwave-flash" src="http://d.yimg.com/m/up/ypp/default/player.swf" flashVars="vid=23267890&amp;" allowfullscreen="true" wmode="transparent" flashvars="vid=23267890&amp;" /></object></div>
<div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/unleash-the-true-power-of-yql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design Robust Restful API</title>
		<link>http://www.solutionhacker.com/design-robust-restful-api/</link>
		<comments>http://www.solutionhacker.com/design-robust-restful-api/#comments</comments>
		<pubDate>Thu, 20 Sep 2012 06:07:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1378</guid>
		<description><![CDATA[This article is to summarize some key design decisions related to exposing some in-house services through <strong>REST API</strong> to the public. It involves <strong>url path design</strong> based on "resource oriented model", <strong>version control</strong>, <strong>authentication and authorization</strong> and <strong>asynchronous call handling</strong>.

<!--more-->
<h3>URL Path Design</h3>
<span style="text-decoration: underline;"><strong>Check out this video first:</strong></span>

[ylwm_vimeo]17785736[/ylwm_vimeo]
<ul>
	<li><strong>Use resource oriented model - </strong>Every <strong>resource</strong> type should be a Noun that is normally represented as a <strong>collection</strong>. On the other hand, <strong>HTTP verbs</strong> (GET, PUT, POST, DELETE) will be used to manipulate the resources.
<ul>
	<li><strong>GET</strong>: read (cacheable)</li>
	<li><strong>PUT</strong>: modify (caller provides id)</li>
	<li><strong>POST</strong>: create the resource and use to call method as RPC.</li>
	<li><strong>DELETE</strong>: remove</li>
</ul>
</li>
	<li>Each resource should have 2 apis only: one for the collection in plural form, one for a particular entity.</li>
	<li>The collection one can have ?search=... for locate the set of entities you want.</li>
	<li>No verb should be in the url path.</li>
	<li><strong>Complex variation</strong> should use ?xxxx to take care of. Don't complicate your url.</li>
	<li><strong>Pagination</strong> prefers to use ?offset=50&#38;limit=200</li>
	<li><strong>Field extraction</strong>: field extraction uses ?fields=xxx,yyy</li>
	<li><strong>Formatting</strong>: take advantage of file extension like dogs.json</li>
	<li>For input parameters, put everything into the URL and not using the <strong>HTTP headers</strong>, which is used for <strong>OAuth headers</strong>.</li>
	<li><strong>Error handling</strong>: Use HTTP error code to indicate error in the server side. (ie. 200 = succeed, 400=application error and 500 = wrong request, is used if the API). A human readable error message, together with the hint to fix that, should be sent back in the HTTP body response.</li>
	<li>Operations can be <strong>sync</strong> vs <strong>async</strong>.</li>
	<li>In GET operation, by default the container only return the <strong>URL reference</strong> of its immediate children. An optional parameter "<strong>expand</strong>" can be used to request the actual representation of all children and descendant.</li>
	<li>API should only expose the function semantics but nothing about its implementation details, which allows the implementation to continuously evolve without breaking the client interface. And a good API should focus to do one thing well, rather than multiple things of different purposes. Each API must be self-contained and not relying on any specific call sequence to work correctly.</li>
</ul>
<span style="text-decoration: underline;"><strong>Example:</strong></span>
<ol>
	<li>List all persons. GET /persons</li>
	<li>Find a person with a particular id. GET /persons/123</li>
	<li>Get partial fields. GET /persons/123?fields=(name,age)</li>
	<li>Find a person's particular friend. GET/persons/123/friends/456</li>
	<li>Find all persons named John. GET /persons/search?q=(name,eq,John)</li>
	<li>Find all dogs whose master is John. GET /persons/search?q=(name,eq,John)/dogs</li>
	<li>Create a person with a server assigned id. POST /persons?name=Dave&#38;age=10</li>
	<li>Create a person with a client assigned id. PUT /persons/123?name=Dave&#38;age=10</li>
	<li>Ask the person to perform an action. POST /persons/123/action/travel?location=Euro</li>
	<li>Remove a person. DELETE /persons/123</li>
	<li>Return a page of result. GET /persons/search?q=(name,eq,John)&#38;offset=1&#38;limit=25</li>
</ol>
<h3>Async call handling</h3>
In case when the operation takes a long time to complete, an asynchronous mode should be used. In a polling approach, a transient transaction object is return immediately to the caller. The caller can then use GET request to poll for the result of the operation. We can also use a notification approach. In this case, the caller pass along a callback URI when making the request. The server will invoke the callback URI to POST the result when it is done.
<h3>Versioning</h3>
<ul>
	<li>If you follow the minimal API design approach, the newer version is usually about <strong>adding parameters</strong> to your original API rather than removing parameters.</li>
	<li>Backward compatibility via using the same URL. (e.g. http://xyz.com/v1/path/...). On the implementation side, you only have the implementation that takes the latest version API parameters as input. In other words, you are prepared to receive request of the older version as well as the latest version. But you <strong>substitute the default value of the parameters of the newer version that is missing in the older version.</strong> And then send this request (with all the parameters filled) to the latest implementation.</li>
	<li>For incompatible change, use a <strong>different URL endpoint</strong> for the new version (e.g. http://xyz.com/v2/path/...). You also keep the corresponding implementation (v1 and v2) behind those endpoints. Depends on your decision whether to keep supporting the older version, you may want to introduce a deprecation process. Unfortunately there is no standard way to indicate an API will be deprecated in the response. One possible way is to put a flag in the HTTP header of the response to indicate when the API will be deprecated.</li>
	<li>Most people put <strong>versioning info as part of url</strong> like (e.g. http://xyz.com/v1/path/...). But it is debatable as others see versioning shouldn't be part of url instead you can play trick in <strong>content type</strong> to specify what version of content you want.</li>
</ul>
<div><span style="text-decoration: underline;"><strong>Reference</strong></span></div>
<div>
<ol>
	<li>http://thereisnorightway.blogspot.com/2011/02/versioning-and-types-in-resthttp-api.html</li>
</ol>
</div>
<h3>Authentication and Authorization</h3>
Authentication call which must the first call to make and precede any other application API calls. As far as API security, <strong>App level key</strong>, with <strong>OAuth2.0</strong> protocol should be used for authentication and authorization purpose.]]></description>
				<content:encoded><![CDATA[<p>This article is to summarize some key design decisions related to exposing some in-house services through <strong>REST API</strong> to the public. It involves <strong>url path design</strong> based on &#8220;resource oriented model&#8221;, <strong>version control</strong>, <strong>authentication and authorization</strong> and <strong>asynchronous call handling</strong>.</p>
<p><span id="more-1378"></span></p>
<h3>URL Path Design</h3>
<p><span style="text-decoration: underline;"><strong>Check out this video first:</strong></span></p>
<p><iframe src="http://player.vimeo.com/video/17785736" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
<ul>
<li><strong>Use resource oriented model - </strong>Every <strong>resource</strong> type should be a Noun that is normally represented as a <strong>collection</strong>. On the other hand, <strong>HTTP verbs</strong> (GET, PUT, POST, DELETE) will be used to manipulate the resources.
<ul>
<li><strong>GET</strong>: read (cacheable)</li>
<li><strong>PUT</strong>: modify (caller provides id)</li>
<li><strong>POST</strong>: create the resource and use to call method as RPC.</li>
<li><strong>DELETE</strong>: remove</li>
</ul>
</li>
<li>Each resource should have 2 apis only: one for the collection in plural form, one for a particular entity.</li>
<li>The collection one can have ?search=&#8230; for locate the set of entities you want.</li>
<li>No verb should be in the url path.</li>
<li><strong>Complex variation</strong> should use ?xxxx to take care of. Don&#8217;t complicate your url.</li>
<li><strong>Pagination</strong> prefers to use ?offset=50&amp;limit=200</li>
<li><strong>Field extraction</strong>: field extraction uses ?fields=xxx,yyy</li>
<li><strong>Formatting</strong>: take advantage of file extension like dogs.json</li>
<li>For input parameters, put everything into the URL and not using the <strong>HTTP headers</strong>, which is used for <strong>OAuth headers</strong>.</li>
<li><strong>Error handling</strong>: Use HTTP error code to indicate error in the server side. (ie. 200 = succeed, 400=application error and 500 = wrong request, is used if the API). A human readable error message, together with the hint to fix that, should be sent back in the HTTP body response.</li>
<li>Operations can be <strong>sync</strong> vs <strong>async</strong>.</li>
<li>In GET operation, by default the container only return the <strong>URL reference</strong> of its immediate children. An optional parameter &#8220;<strong>expand</strong>&#8221; can be used to request the actual representation of all children and descendant.</li>
<li>API should only expose the function semantics but nothing about its implementation details, which allows the implementation to continuously evolve without breaking the client interface. And a good API should focus to do one thing well, rather than multiple things of different purposes. Each API must be self-contained and not relying on any specific call sequence to work correctly.</li>
</ul>
<p><span style="text-decoration: underline;"><strong>Example:</strong></span></p>
<ol>
<li>List all persons. GET /persons</li>
<li>Find a person with a particular id. GET /persons/123</li>
<li>Get partial fields. GET /persons/123?fields=(name,age)</li>
<li>Find a person&#8217;s particular friend. GET/persons/123/friends/456</li>
<li>Find all persons named John. GET /persons/search?q=(name,eq,John)</li>
<li>Find all dogs whose master is John. GET /persons/search?q=(name,eq,John)/dogs</li>
<li>Create a person with a server assigned id. POST /persons?name=Dave&amp;age=10</li>
<li>Create a person with a client assigned id. PUT /persons/123?name=Dave&amp;age=10</li>
<li>Ask the person to perform an action. POST /persons/123/action/travel?location=Euro</li>
<li>Remove a person. DELETE /persons/123</li>
<li>Return a page of result. GET /persons/search?q=(name,eq,John)&amp;offset=1&amp;limit=25</li>
</ol>
<h3>Async call handling</h3>
<p>In case when the operation takes a long time to complete, an asynchronous mode should be used. In a polling approach, a transient transaction object is return immediately to the caller. The caller can then use GET request to poll for the result of the operation. We can also use a notification approach. In this case, the caller pass along a callback URI when making the request. The server will invoke the callback URI to POST the result when it is done.</p>
<h3>Versioning</h3>
<ul>
<li>If you follow the minimal API design approach, the newer version is usually about <strong>adding parameters</strong> to your original API rather than removing parameters.</li>
<li>Backward compatibility via using the same URL. (e.g. http://xyz.com/v1/path/&#8230;). On the implementation side, you only have the implementation that takes the latest version API parameters as input. In other words, you are prepared to receive request of the older version as well as the latest version. But you <strong>substitute the default value of the parameters of the newer version that is missing in the older version.</strong> And then send this request (with all the parameters filled) to the latest implementation.</li>
<li>For incompatible change, use a <strong>different URL endpoint</strong> for the new version (e.g. http://xyz.com/v2/path/&#8230;). You also keep the corresponding implementation (v1 and v2) behind those endpoints. Depends on your decision whether to keep supporting the older version, you may want to introduce a deprecation process. Unfortunately there is no standard way to indicate an API will be deprecated in the response. One possible way is to put a flag in the HTTP header of the response to indicate when the API will be deprecated.</li>
<li>Most people put <strong>versioning info as part of url</strong> like (e.g. http://xyz.com/v1/path/&#8230;). But it is debatable as others see versioning shouldn&#8217;t be part of url instead you can play trick in <strong>content type</strong> to specify what version of content you want.</li>
</ul>
<div><span style="text-decoration: underline;"><strong>Reference</strong></span></div>
<div>
<ol>
<li>http://thereisnorightway.blogspot.com/2011/02/versioning-and-types-in-resthttp-api.html</li>
</ol>
</div>
<h3>Authentication and Authorization</h3>
<p>Authentication call which must the first call to make and precede any other application API calls. As far as API security, <strong>App level key</strong>, with <strong>OAuth2.0</strong> protocol should be used for authentication and authorization purpose.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/design-robust-restful-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Modern principles for building large distributed system</title>
		<link>http://www.solutionhacker.com/modern-principles-for-building-large-distributed-system/</link>
		<comments>http://www.solutionhacker.com/modern-principles-for-building-large-distributed-system/#comments</comments>
		<pubDate>Wed, 19 Sep 2012 20:31:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Big Data Solution]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1364</guid>
		<description><![CDATA[<h3>CAP Theorem</h3>
Consistency, Availability and Partition - CAP. You can at most get 2 out of 3.
<h3>BASE</h3>
In large deployments and high throughput scenarios, ACID breaks down because it requires actions to be “all or nothing”, effectively creating bottlenecks while clients wait for transaction handles. If you’ve done any distributed or concurrent computing, you know that <strong>contention for shared resources</strong> is a primary cause of failure, including problems like deadlock, starvation and race conditions. For the sake of reducing single points of failure (bottlenecks), Some distributed systems like Riak implements <strong>eventual consistency</strong> — where storage operations are accepted immediately, and then propagated across the cluster in an <strong>asynchronous</strong> fashion. This makes it nearly always available for writes and reads.
<ul>
	<li><strong>B</strong>asic <strong>A</strong>vailability</li>
	<li><strong>S</strong>oft-state</li>
	<li><strong>E</strong>ventual consistency (Rather than requiring consistency after every transaction, it is enough for the database to eventually be in a consistent state).</li>
</ul>
Comparing to <strong>ACID</strong>, BASE system is forfeiting C and I for Availability.
<h3>Vector Clock for Conflict Resolution</h3>
Since we are doing Eventual Consistency for availability, we need to deal with inconsistency issue. To deal with inconsistency, Riak tags each datum with a vector clock that internally reveals the datum’s lineage (who modified what version). When there are conflicts — that is, two parallel versions of the same datum — Riak returns you both versions so that your application can decide how to resolve it. The idea here is to perform conflict resolution during the read time rrather than in an SQL database with this kind of conflict, your transaction might fail and rollback, forcing you to resolve it and retry anyway.
<h3>Load Distribution over Consistent Hashing</h3>
<strong>Traditional hashing</strong> hashes the key and mod the number of machines to identify which machine to store the key. (ie <em>hash(k)</em> mod <em>N)</em>. This works well until you add or remove cache machines, for then <em>N</em> changes and every object need to rehash with the new N. Before you reshuffle the cache objects to the new machine, you may experience cache misses. On top of that, the reshuffle process could be very slow if the data is big. To solve this problem, here comes the <strong>consistent hashing</strong>. The idea of consistent hashing is to create a <strong>64 bits ring</strong> and each point in the ring represents either the key or machine. When a key is hashed, a point in the 64bit ring is identified. Then, you can locate the machine clockwise to have it handled the resource. For better resource allocation, you can create more than 1 point per machine based on its capacity (ie. <strong>virtual nodes</strong>) and have them spread across the ring for more uniformed resource allocation.
<ol>
	<li>http://weblogs.java.net/blog/tomwhite/archive/2007/11/consistent_hash.html</li>
	<li>http://www.tomkleinpeter.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/</li>
</ol>
<h3>Replica Synchronization with Merkle Tree</h3>
<h3>Robust failure recovery</h3>
If a node goes down in your cluster, its replicas will take over for it until it comes back, a feature known as <strong>hinted handoff</strong>. If it doesn’t come back — as happens all too often on EC2 — you can add a new node and the cluster will rebalance.
<h3>Reference</h3>
<ul>
	<li>http://seancribbs.com/tech/2010/02/06/why-riak-should-power-your-next-rails-app/</li>
</ul>]]></description>
				<content:encoded><![CDATA[<h3>CAP Theorem</h3>
<p>Consistency, Availability and Partition &#8211; CAP. You can at most get 2 out of 3.</p>
<h3>BASE</h3>
<p>In large deployments and high throughput scenarios, ACID breaks down because it requires actions to be “all or nothing”, effectively creating bottlenecks while clients wait for transaction handles. If you’ve done any distributed or concurrent computing, you know that <strong>contention for shared resources</strong> is a primary cause of failure, including problems like deadlock, starvation and race conditions. For the sake of reducing single points of failure (bottlenecks), Some distributed systems like Riak implements <strong>eventual consistency</strong> — where storage operations are accepted immediately, and then propagated across the cluster in an <strong>asynchronous</strong> fashion. This makes it nearly always available for writes and reads.</p>
<ul>
<li><strong>B</strong>asic <strong>A</strong>vailability</li>
<li><strong>S</strong>oft-state</li>
<li><strong>E</strong>ventual consistency (Rather than requiring consistency after every transaction, it is enough for the database to eventually be in a consistent state).</li>
</ul>
<p>Comparing to <strong>ACID</strong>, BASE system is forfeiting C and I for Availability.</p>
<h3>Vector Clock for Conflict Resolution</h3>
<p>Since we are doing Eventual Consistency for availability, we need to deal with inconsistency issue. To deal with inconsistency, Riak tags each datum with a vector clock that internally reveals the datum’s lineage (who modified what version). When there are conflicts — that is, two parallel versions of the same datum — Riak returns you both versions so that your application can decide how to resolve it. The idea here is to perform conflict resolution during the read time rrather than in an SQL database with this kind of conflict, your transaction might fail and rollback, forcing you to resolve it and retry anyway.</p>
<h3>Load Distribution over Consistent Hashing</h3>
<p><strong>Traditional hashing</strong> hashes the key and mod the number of machines to identify which machine to store the key. (ie <em>hash(k)</em> mod <em>N)</em>. This works well until you add or remove cache machines, for then <em>N</em> changes and every object need to rehash with the new N. Before you reshuffle the cache objects to the new machine, you may experience cache misses. On top of that, the reshuffle process could be very slow if the data is big. To solve this problem, here comes the <strong>consistent hashing</strong>. The idea of consistent hashing is to create a <strong>64 bits ring</strong> and each point in the ring represents either the key or machine. When a key is hashed, a point in the 64bit ring is identified. Then, you can locate the machine clockwise to have it handled the resource. For better resource allocation, you can create more than 1 point per machine based on its capacity (ie. <strong>virtual nodes</strong>) and have them spread across the ring for more uniformed resource allocation.</p>
<ol>
<li>http://weblogs.java.net/blog/tomwhite/archive/2007/11/consistent_hash.html</li>
<li>http://www.tomkleinpeter.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/</li>
</ol>
<h3>Replica Synchronization with Merkle Tree</h3>
<h3>Robust failure recovery</h3>
<p>If a node goes down in your cluster, its replicas will take over for it until it comes back, a feature known as <strong>hinted handoff</strong>. If it doesn’t come back — as happens all too often on EC2 — you can add a new node and the cluster will rebalance.</p>
<h3>Reference</h3>
<ul>
<li>http://seancribbs.com/tech/2010/02/06/why-riak-should-power-your-next-rails-app/</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/modern-principles-for-building-large-distributed-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get search content for millions of keywords per day at low price?</title>
		<link>http://www.solutionhacker.com/how-can-i-get-search-content-from-1-million-of-keywords-per-day-at-a-reasonable-price/</link>
		<comments>http://www.solutionhacker.com/how-can-i-get-search-content-from-1-million-of-keywords-per-day-at-a-reasonable-price/#comments</comments>
		<pubDate>Tue, 18 Sep 2012 00:06:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Earn Money Online]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1350</guid>
		<description><![CDATA[Recently I have a project that requires me to analyze Google and Yahoo organic search content/ad copies over millions of keywords. Before digging down into deep semantic analysis or even machine learning, I first need to crawl the html content from each search engines in parallel. Initially, I thought it can be achieved easily. Then I came across roadblocks like search quota and rate limiting over a period of time from each of the search engine. To get around these, I looked into API call. However, it is very pricy. Google is charging $5/1000 queries while Yahoo Boss is cheaper but still @ $0.8/1000 queries. If I have 1 million query per day, we are talking about few thousands a day. I have no way to afford that!! What should I do?

<!--more-->
<h2>Can YQL or 80legs help?</h2>
I started thinking of virtually increase the search quota per day via doing that from different machines. However, if I need to rent boxes from Amazon, it could cause me hundreds a month to meet my need. Plus Google may figure that out and block my boxes. Then I need to find another boxes. It is doable but quite labor intensive to stay below the radar. Then I start thinking to leverage YQL as Yahoo has quite a lot of machines powering this. However, I found out Google and even Yahoo has put up robot.txt to cut YQL out. Same problem I faced from trying out 80legs.
<h2>Can IronWorker help?</h2>
I did more research and identify IronWorker that I just need to pay the processing time rather than renting a bunch of physical boxes. The price is reasonable and their cloud is big. But they have no guarantee how many boxes they can provision you to run your job. If they allocate few boxes for that, I may get them banned quickly. On the other hand, to use ironworker, I need to create a crawl script and upload it to account. It is not bad but I need to re-engineering my code to leverage this b/c crawl becomes a batch process rather than realtime request and response model.
<h2>Proxy farm is here to save?</h2>
I moved forward and found out how others getting around this. They are using proxies to hide their identities. I first tried out with public proxies. (ie. As for a poor engineer like me, I always tried to find the free solution first ^v^). My experience for public proxies are bad. Most of them are blocked, slow and unreliable. Then I tried to pay a little money for shared proxies. Since it is shared, you will be blocked if someone uses the proxy and leave the footprint for Google. From what I heard, Google is attacking the private proxy providers via banning at the subnet level rather than ip. So, my experience on shared proxies are still not meeting my reliability requirement. Then I start shopping for private proxy and would like to find out which providers actually have proxies across many subnets for the sake of reducing my chance to be blocked completely. Some providers look good but the rate they charge are like $4 per proxy per month. If I am using 5 threads to run my search queries, I may need 30 proxies to standby (NOTE: I adopt the 1:6 ratio to get myself under the radar). It is not that expensive but I may scale it to 100 proxies and it may end up costing me few hundreds a month. Eventually, I come across a great and cheap service called <a href="http://www.myprivateproxy.net/billing/aff.php?aff=352">myprivateproxy.net</a>. It is around $2 per private proxy per month and it has great feedbacks from the users and speed is extremely fast.
<p style="text-align: center;"><a href="http://www.myprivateproxy.net/billing/aff.php?aff=352"><img class="aligncenter size-full wp-image-1355" title="Screen shot 2012-09-17 at 5.35.56 PM" src="http://www.solutionhacker.com/wp-content/uploads/Screen-shot-2012-09-17-at-5.35.56-PM.png" alt="" width="179" height="89" /></a></p>
I am doing reliability test on it right now. I need to run it for a period of time to confirm if it is good or not. If it works well, I will have my company used this service as well. I will post my result for the next post. Stay tuned!!

PS. If you are are interest in MPP, you can use this promotion code I got "<strong>warrior</strong>". This code can save you 15% recurring charges. On top of 10% extra proxy as discount, the deal is really great!!]]></description>
				<content:encoded><![CDATA[<p>Recently I have a project that requires me to analyze Google and Yahoo organic search content/ad copies over millions of keywords. Before digging down into deep semantic analysis or even machine learning, I first need to crawl the html content from each search engines in parallel. Initially, I thought it can be achieved easily. Then I came across roadblocks like search quota and rate limiting over a period of time from each of the search engine. To get around these, I looked into API call. However, it is very pricy. Google is charging $5/1000 queries while Yahoo Boss is cheaper but still @ $0.8/1000 queries. If I have 1 million query per day, we are talking about few thousands a day. I have no way to afford that!! What should I do?</p>
<p><span id="more-1350"></span></p>
<h2>Can YQL or 80legs help?</h2>
<p>I started thinking of virtually increase the search quota per day via doing that from different machines. However, if I need to rent boxes from Amazon, it could cause me hundreds a month to meet my need. Plus Google may figure that out and block my boxes. Then I need to find another boxes. It is doable but quite labor intensive to stay below the radar. Then I start thinking to leverage YQL as Yahoo has quite a lot of machines powering this. However, I found out Google and even Yahoo has put up robot.txt to cut YQL out. Same problem I faced from trying out 80legs.</p>
<h2>Can IronWorker help?</h2>
<p>I did more research and identify IronWorker that I just need to pay the processing time rather than renting a bunch of physical boxes. The price is reasonable and their cloud is big. But they have no guarantee how many boxes they can provision you to run your job. If they allocate few boxes for that, I may get them banned quickly. On the other hand, to use ironworker, I need to create a crawl script and upload it to account. It is not bad but I need to re-engineering my code to leverage this b/c crawl becomes a batch process rather than realtime request and response model.</p>
<h2>Proxy farm is here to save?</h2>
<p>I moved forward and found out how others getting around this. They are using proxies to hide their identities. I first tried out with public proxies. (ie. As for a poor engineer like me, I always tried to find the free solution first ^v^). My experience for public proxies are bad. Most of them are blocked, slow and unreliable. Then I tried to pay a little money for shared proxies. Since it is shared, you will be blocked if someone uses the proxy and leave the footprint for Google. From what I heard, Google is attacking the private proxy providers via banning at the subnet level rather than ip. So, my experience on shared proxies are still not meeting my reliability requirement. Then I start shopping for private proxy and would like to find out which providers actually have proxies across many subnets for the sake of reducing my chance to be blocked completely. Some providers look good but the rate they charge are like $4 per proxy per month. If I am using 5 threads to run my search queries, I may need 30 proxies to standby (NOTE: I adopt the 1:6 ratio to get myself under the radar). It is not that expensive but I may scale it to 100 proxies and it may end up costing me few hundreds a month. Eventually, I come across a great and cheap service called <a href="http://www.myprivateproxy.net/billing/aff.php?aff=352">myprivateproxy.net</a>. It is around $2 per private proxy per month and it has great feedbacks from the users and speed is extremely fast.</p>
<p style="text-align: center;"><a href="http://www.myprivateproxy.net/billing/aff.php?aff=352"><img class="aligncenter size-full wp-image-1355" title="Screen shot 2012-09-17 at 5.35.56 PM" src="http://www.solutionhacker.com/wp-content/uploads/Screen-shot-2012-09-17-at-5.35.56-PM.png" alt="" width="179" height="89" /></a></p>
<p>I am doing reliability test on it right now. I need to run it for a period of time to confirm if it is good or not. If it works well, I will have my company used this service as well. I will post my result for the next post. Stay tuned!!</p>
<p>PS. If you are are interest in MPP, you can use this promotion code I got &#8220;<strong>warrior</strong>&#8220;. This code can save you 15% recurring charges. On top of 10% extra proxy as discount, the deal is really great!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/how-can-i-get-search-content-from-1-million-of-keywords-per-day-at-a-reasonable-price/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extract info from Apache Log</title>
		<link>http://www.solutionhacker.com/extract-info-from-apache-log/</link>
		<comments>http://www.solutionhacker.com/extract-info-from-apache-log/#comments</comments>
		<pubDate>Thu, 12 Jul 2012 08:39:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Big Data Solution]]></category>
		<category><![CDATA[Data Collection]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1339</guid>
		<description><![CDATA[This article is to show you how to extract information from the Apache access log. Since Apache is the most common web server, the Linux shell commands below should be quite useful.
<h3>Extract unique IP from Apache access log</h3>
<ul>
	<li>Location the apache access log file</li>
	<li>Then spit out one line to see the format:</li>
</ul>
[java]tail -n1 /var/log/httpd/access_log[/java]
<ul>
	<li>Find the IP address in that line and count which part it is. (first part is $1)</li>
</ul>
[java]cat /var/log/httpd/access_log &#124; awk '{print $1}' &#124; sort &#124; uniq -c &#62; /var/log/httpd/unique-ips.log[/java]
<ul>
	<li>To count how many unique IP</li>
</ul>
[java]cat  /var/log/httpd/unique-ips.log &#124; wc -l[/java]
<ul>
	<li>Once you get the IP lists, you can download the csv from maxmind and lookup its geo location. I have written a program to process this IP lists and figure out how many US traffics I am getting.</li>
</ul>
<h3>Identify bot traffics from Apache access log</h3>
<h3>Generate the hourly traffic report</h3>
<h3>Figure out who refers us the most traffic</h3>]]></description>
				<content:encoded><![CDATA[<p>This article is to show you how to extract information from the Apache access log. Since Apache is the most common web server, the Linux shell commands below should be quite useful.</p>
<h3>Extract unique IP from Apache access log</h3>
<ul>
<li>Location the apache access log file</li>
<li>Then spit out one line to see the format:</li>
</ul>
<pre class="brush: java; title: ; notranslate">tail -n1 /var/log/httpd/access_log</pre>
<ul>
<li>Find the IP address in that line and count which part it is. (first part is $1)</li>
</ul>
<pre class="brush: java; title: ; notranslate">cat /var/log/httpd/access_log | awk '{print $1}' | sort | uniq -c &gt; /var/log/httpd/unique-ips.log</pre>
<ul>
<li>To count how many unique IP</li>
</ul>
<pre class="brush: java; title: ; notranslate">cat  /var/log/httpd/unique-ips.log | wc -l</pre>
<ul>
<li>Once you get the IP lists, you can download the csv from maxmind and lookup its geo location. I have written a program to process this IP lists and figure out how many US traffics I am getting.</li>
</ul>
<h3>Identify bot traffics from Apache access log</h3>
<h3>Generate the hourly traffic report</h3>
<h3>Figure out who refers us the most traffic</h3>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/extract-info-from-apache-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Function Currying in Scala &#8211; Part 4</title>
		<link>http://www.solutionhacker.com/function-currying-in-scala-part-4/</link>
		<comments>http://www.solutionhacker.com/function-currying-in-scala-part-4/#comments</comments>
		<pubDate>Thu, 05 Jul 2012 01:50:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scale and Optimize]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1317</guid>
		<description><![CDATA[When I first read function currying, I had no idea what is the use of it. Why someone wants to break down its argument list into multiple () with one argument per each. Luckily, I finally came across an <a href="http://reminiscential.wordpress.com/2010/09/29/scala-use-function-currying/">article</a> that provides a great explanation of it. Given what you have learnt so far from my series, you should have good foundation to understand this article. I will try to walk through this example with you here and hope you will grasp it as well.
<h2>Review the example</h2>
[java]
@Test
def testRule1() {
  parserRule.apply(new CharSequence(&#34;someInput&#34;)) match {
    case Success(result, _) =&#62; {
      assertEquals(&#34;expected&#34;, result)
      /* other asserts if the result is a collection of something else */
      }
    case NoSuccess(msg, _) =&#62; fail(msg)
  }
}
[/java]

You can see some syntax rules are applied here. Let me go through with you one after one.
<ol>
	<li>testRule1() method has return type Unit, so '=' sign is not necessary. Since it is &#62; 1 statement in the function body, we still need to have the { }.</li>
	<li>parserRule must be an instance variable from class Parser.</li>
	<li>The return type of parserRule is instance of one of the case class "Success" or "NoSuccess" and they extend ParseResult abstract class. The return result will be used in the pattern matching like regular function chaining.</li>
	<li>"apply" method name can be omitted and author wants to factor out the routine for Success as a separate function.</li>
</ol>
The author creates a method tryMatch and refactor out the assertion routine as below:

[java]
def tryMatch[T](pr:ParseResult[T], f:(T)=&#62;Unit) {
  pr match {
    case Success(r, _) =&#62; f(r)
    case NoSuccess(msg, _) =&#62; fail(msg)
  }
}
[/java]

There are some interesting points I want to bring up:
<ol>
	<li>ParseResult has [T] next to it. I haven't talked about that in my series. It is <strong>generic</strong> in Scala way.</li>
	<li>The 2nd parameter is a function type that allows you to pass the function to handle the success case.</li>
</ol>
How to use the new function above to make your test cleaner and easier

[java]
@Test
def test1() {
  tryMatch(rule1(&#34;input&#34;), (result)=&#62;{assertEquals(&#34;expected&#34;,result)})
}
[/java]

According to pattern matching rules, the value in the ParseResult will assign to r if case Success is matched. And the r will be the "result" of the function literal passed in. So far so good?
<h2>Function Currying</h2>
OK, I finally get to point to talk about function currying. The example in this blog saying that if there are multiple statement in success case routine, it will look ugly if you try to encapsulate all of these inside the function literal like below:

[java]
@Test
def test2() {
  tryMatch(listRule(&#34;1,2,3,4&#34;), (result)=&#62;{assertEquals(1,result(0));assertEquals(2,result(1));assertEquals(3,result(2));assertEquals(4,result(3))})
}
[/java]

&#60;
So, the author suggested to use function currying as it will transform a method taking multiple parameters into a chain of singular parameter. Here is what he can achieve after the function currying:

[java]
def tryMatch[T](pr:ParseResult[T])(f:(T)=&#62;Unit) {
  pr match {
    case Success(r, _) =&#62; f(r)
    case NoSuccess(msg, _) =&#62; fail(msg)
  }
}

//now we can do this:
@Test
def testList() {
  tryMatch(listRule(&#34;1 2 3&#34;)) {
    result =&#62; {
      assertEquals(1, result(0))
      assertEquals(2, result(1))
      assertEquals(3, result(2))
    }
  }
}
[/java]

However, I don't really feel the power of function currying simply just convert () to {}. If the author can format the previous code a bit, he can achieve the one like below. To me, it is about the same in term of clarity.

[java]
def test2() {
  tryMatch(listRule(&#34;1,2,3,4&#34;),
     (result)=&#62;{
       assertEquals(1,result(0))
       assertEquals(2,result(1))
       assertEquals(3,result(2))
       assertEquals(4,result(3))
      }
   )
}
[/java]

I believe function currying should possess the power better than this example. The reason I picked this example because I like the refactoring process from the author. I have googled more and notice this <a href="http://panduwana.wordpress.com/2010/04/30/use-of-currying/">example</a> may be a good one. However, to be honest, I am still not quite clear about the pivot syntax. So, I googled more and found out this statement from this article, "... Sometimes it makes sense to let people apply some arguments to your function now and others later". Sounds like curried function allowing you to partially apply the parameter to the function.

[java]
scala&#62; def multiply(m: Int)(n: Int): Int = m * n
multiply: (m: Int)(n: Int)Int

scala&#62; multiply(2)(3)
res0: Int = 6

scala&#62; val timesTwo = multiply(2)(_)
timesTwo: (Int) =&#62; Int =

scala&#62; timesTwo(3)
res1: Int = 6
[/java]

Too bad that I still cannot figure out a real life example that we need to use curried function. If you know one, please comment this post.]]></description>
				<content:encoded><![CDATA[<p>When I first read function currying, I had no idea what is the use of it. Why someone wants to break down its argument list into multiple () with one argument per each. Luckily, I finally came across an <a href="http://reminiscential.wordpress.com/2010/09/29/scala-use-function-currying/">article</a> that provides a great explanation of it. Given what you have learnt so far from my series, you should have good foundation to understand this article. I will try to walk through this example with you here and hope you will grasp it as well.</p>
<h2>Review the example</h2>
<pre class="brush: java; title: ; notranslate">
@Test
def testRule1() {
  parserRule.apply(new CharSequence(&quot;someInput&quot;)) match {
    case Success(result, _) =&gt; {
      assertEquals(&quot;expected&quot;, result)
      /* other asserts if the result is a collection of something else */
      }
    case NoSuccess(msg, _) =&gt; fail(msg)
  }
}
</pre>
<p>You can see some syntax rules are applied here. Let me go through with you one after one.</p>
<ol>
<li>testRule1() method has return type Unit, so &#8216;=&#8217; sign is not necessary. Since it is &gt; 1 statement in the function body, we still need to have the { }.</li>
<li>parserRule must be an instance variable from class Parser.</li>
<li>The return type of parserRule is instance of one of the case class &#8220;Success&#8221; or &#8220;NoSuccess&#8221; and they extend ParseResult abstract class. The return result will be used in the pattern matching like regular function chaining.</li>
<li>&#8220;apply&#8221; method name can be omitted and author wants to factor out the routine for Success as a separate function.</li>
</ol>
<p>The author creates a method tryMatch and refactor out the assertion routine as below:</p>
<pre class="brush: java; title: ; notranslate">
def tryMatch[T](pr:ParseResult[T], f:(T)=&gt;Unit) {
  pr match {
    case Success(r, _) =&gt; f(r)
    case NoSuccess(msg, _) =&gt; fail(msg)
  }
}
</pre>
<p>There are some interesting points I want to bring up:</p>
<ol>
<li>ParseResult has [T] next to it. I haven&#8217;t talked about that in my series. It is <strong>generic</strong> in Scala way.</li>
<li>The 2nd parameter is a function type that allows you to pass the function to handle the success case.</li>
</ol>
<p>How to use the new function above to make your test cleaner and easier</p>
<pre class="brush: java; title: ; notranslate">
@Test
def test1() {
  tryMatch(rule1(&quot;input&quot;), (result)=&gt;{assertEquals(&quot;expected&quot;,result)})
}
</pre>
<p>According to pattern matching rules, the value in the ParseResult will assign to r if case Success is matched. And the r will be the &#8220;result&#8221; of the function literal passed in. So far so good?</p>
<h2>Function Currying</h2>
<p>OK, I finally get to point to talk about function currying. The example in this blog saying that if there are multiple statement in success case routine, it will look ugly if you try to encapsulate all of these inside the function literal like below:</p>
<pre class="brush: java; title: ; notranslate">
@Test
def test2() {
  tryMatch(listRule(&quot;1,2,3,4&quot;), (result)=&gt;{assertEquals(1,result(0));assertEquals(2,result(1));assertEquals(3,result(2));assertEquals(4,result(3))})
}
</pre>
<p>&lt;<br />
So, the author suggested to use function currying as it will transform a method taking multiple parameters into a chain of singular parameter. Here is what he can achieve after the function currying:</p>
<pre class="brush: java; title: ; notranslate">
def tryMatch[T](pr:ParseResult[T])(f:(T)=&gt;Unit) {
  pr match {
    case Success(r, _) =&gt; f(r)
    case NoSuccess(msg, _) =&gt; fail(msg)
  }
}

//now we can do this:
@Test
def testList() {
  tryMatch(listRule(&quot;1 2 3&quot;)) {
    result =&gt; {
      assertEquals(1, result(0))
      assertEquals(2, result(1))
      assertEquals(3, result(2))
    }
  }
}
</pre>
<p>However, I don&#8217;t really feel the power of function currying simply just convert () to {}. If the author can format the previous code a bit, he can achieve the one like below. To me, it is about the same in term of clarity.</p>
<pre class="brush: java; title: ; notranslate">
def test2() {
  tryMatch(listRule(&quot;1,2,3,4&quot;),
     (result)=&gt;{
       assertEquals(1,result(0))
       assertEquals(2,result(1))
       assertEquals(3,result(2))
       assertEquals(4,result(3))
      }
   )
}
</pre>
<p>I believe function currying should possess the power better than this example. The reason I picked this example because I like the refactoring process from the author. I have googled more and notice this <a href="http://panduwana.wordpress.com/2010/04/30/use-of-currying/">example</a> may be a good one. However, to be honest, I am still not quite clear about the pivot syntax. So, I googled more and found out this statement from this article, &#8220;&#8230; Sometimes it makes sense to let people apply some arguments to your function now and others later&#8221;. Sounds like curried function allowing you to partially apply the parameter to the function.</p>
<pre class="brush: java; title: ; notranslate">
scala&gt; def multiply(m: Int)(n: Int): Int = m * n
multiply: (m: Int)(n: Int)Int

scala&gt; multiply(2)(3)
res0: Int = 6

scala&gt; val timesTwo = multiply(2)(_)
timesTwo: (Int) =&gt; Int =

scala&gt; timesTwo(3)
res1: Int = 6
</pre>
<p>Too bad that I still cannot figure out a real life example that we need to use curried function. If you know one, please comment this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/function-currying-in-scala-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Power of Scala &#8211; Part 3</title>
		<link>http://www.solutionhacker.com/power-of-scala-part-3/</link>
		<comments>http://www.solutionhacker.com/power-of-scala-part-3/#comments</comments>
		<pubDate>Wed, 04 Jul 2012 06:52:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scale and Optimize]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1295</guid>
		<description><![CDATA[Enough comparison between Scala and Java. The first two articles in the series just helps you to get familiar with Scala syntax but not telling you the actual power of Scala. In fact, I have no motivation to pick up Scala if it doesn't give me the power big enough to scarify my time with my family. From this onward, I will put down the key features in Scala that differentiate it from Java.
<h2>Pattern Matching</h2>
<h3>Extend the power of switch/cases</h3>
<ol>
	<li>Support more than simple primitive type switch/cases</li>
	<li>Support case class</li>
	<li>Type match and assignment</li>
</ol>
[box title="Simple switch/cases" color="#333333"]

//primitive
def checkPrime(number:Int) = number match {
case 1 =&#62; true
case 2 =&#62; true

case _ =&#62; false
}

// you can condense cases in one line and return other data type
def toYesOrNo(choice: Int): String = choice match {
case 1 &#124; 2 &#124; 3 =&#62; "yes"
case 0 =&#62; "no"
case _ =&#62; "error"
}

//you can compare different data types
def f(x: Any): String = x match {
case i:Int =&#62; "integer: " + i
case _:Double =&#62; "a double"
case s:String =&#62; "I want to say " + s
}

//just check type. If match, assign x with obj and assign it with type Color. Then return it and assign to cast variable.
var obj = performOperation()
var cast:Color = obj match {
case x:Color =&#62; x
case _ =&#62; null
}

//with this addon, you can make it looks very similar to formula definition:
def fact(n: Int): Int = n match {
case 0 =&#62; 1
case n =&#62; n * fact(n - 1)
}
[/box]
[box title="Example of case class" color="#333333"]
case class Number(value:Int)

def checkPrime(n:Number) = n match {
case Number(1) =&#62; true
case Number(2) =&#62; true
case Number(3) =&#62; true
case Number(5) =&#62; true
case Number(7) =&#62; true
case Number(_) =&#62; false
}

checkPrime(Number(12))
[/box]
<ol>
	<li>The first statement is the key to the whole thing: defining a new <em>case class</em> “Number” with a single property.</li>
	<li>Case classes are a special type of class in Scala which can be matched directly by pattern matching.</li>
	<li>You can also create a new instance of a case class without the <em>new</em> operator</li>
	<li>For each case, we’re actually creating a new instance of <em>Number, </em>each with a different <em>value.</em></li>
	<li>When Scala sees that the instances are the same type during comparison, it <em>introspects</em> the two instances and compares the property values.</li>
	<li>Case class supports inheritance.</li>
</ol>
<h3>How exception handling uses this?</h3>
Scala does not have checked exceptions and it does not <em>force</em> you to catch exceptions as well. Basically, it is up to you to catch it. To catch various exception, you don't need multiple catch block in Scala. See the example below. You can see it just uses one catch block with switch/cases to do the job.

[box title="Box title" color="#333333"]
import java.sql._
import java.net._

Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:testdb", "sa", "")
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO urls (url) VALUES (?)")
stmt.setObject(1, new URL("http://www.codecommit.com"))

stmt.executeUpdate()
stmt.close()
} catch {
case e:SQLException =&#62; println("Database error")
case e:MalformedURLException =&#62; println("Bad URL")

case e =&#62; {
println("Some other exception type:")
e.printStackTrace()
}
} finally {
conn.close()
}
[/box]]]></description>
				<content:encoded><![CDATA[<p>Enough comparison between Scala and Java. The first two articles in the series just helps you to get familiar with Scala syntax but not telling you the actual power of Scala. In fact, I have no motivation to pick up Scala if it doesn&#8217;t give me the power big enough to scarify my time with my family. From this onward, I will put down the key features in Scala that differentiate it from Java.</p>
<h2>Pattern Matching</h2>
<h3>Extend the power of switch/cases</h3>
<ol>
<li>Support more than simple primitive type switch/cases</li>
<li>Support case class</li>
<li>Type match and assignment</li>
</ol>
<div class="su-box" style="border:1px solid #292929">
<div class="su-box-title" style="background-color:#333333;border-top:1px solid #adadad;text-shadow:1px 1px 0 #0f0f0f">Simple switch/cases</div>
<div class="su-box-content">
<p>//primitive<br />
def checkPrime(number:Int) = number match {<br />
case 1 =&gt; true<br />
case 2 =&gt; true</p>
<p>case _ =&gt; false<br />
}</p>
<p>// you can condense cases in one line and return other data type<br />
def toYesOrNo(choice: Int): String = choice match {<br />
case 1 | 2 | 3 =&gt; &#8220;yes&#8221;<br />
case 0 =&gt; &#8220;no&#8221;<br />
case _ =&gt; &#8220;error&#8221;<br />
}</p>
<p>//you can compare different data types<br />
def f(x: Any): String = x match {<br />
case i:Int =&gt; &#8220;integer: &#8221; + i<br />
case _:Double =&gt; &#8220;a double&#8221;<br />
case s:String =&gt; &#8220;I want to say &#8221; + s<br />
}</p>
<p>//just check type. If match, assign x with obj and assign it with type Color. Then return it and assign to cast variable.<br />
var obj = performOperation()<br />
var cast:Color = obj match {<br />
case x:Color =&gt; x<br />
case _ =&gt; null<br />
}</p>
<p>//with this addon, you can make it looks very similar to formula definition:<br />
def fact(n: Int): Int = n match {<br />
case 0 =&gt; 1<br />
case n =&gt; n * fact(n &#8211; 1)<br />
}
</p></div>
</div>
<div class="su-box" style="border:1px solid #292929">
<div class="su-box-title" style="background-color:#333333;border-top:1px solid #adadad;text-shadow:1px 1px 0 #0f0f0f">Example of case class</div>
<div class="su-box-content">
case class Number(value:Int)</p>
<p>def checkPrime(n:Number) = n match {<br />
case Number(1) =&gt; true<br />
case Number(2) =&gt; true<br />
case Number(3) =&gt; true<br />
case Number(5) =&gt; true<br />
case Number(7) =&gt; true<br />
case Number(_) =&gt; false<br />
}</p>
<p>checkPrime(Number(12))
</p></div>
</div>
<ol>
<li>The first statement is the key to the whole thing: defining a new <em>case class</em> “Number” with a single property.</li>
<li>Case classes are a special type of class in Scala which can be matched directly by pattern matching.</li>
<li>You can also create a new instance of a case class without the <em>new</em> operator</li>
<li>For each case, we’re actually creating a new instance of <em>Number, </em>each with a different <em>value.</em></li>
<li>When Scala sees that the instances are the same type during comparison, it <em>introspects</em> the two instances and compares the property values.</li>
<li>Case class supports inheritance.</li>
</ol>
<h3>How exception handling uses this?</h3>
<p>Scala does not have checked exceptions and it does not <em>force</em> you to catch exceptions as well. Basically, it is up to you to catch it. To catch various exception, you don&#8217;t need multiple catch block in Scala. See the example below. You can see it just uses one catch block with switch/cases to do the job.</p>
<div class="su-box" style="border:1px solid #292929">
<div class="su-box-title" style="background-color:#333333;border-top:1px solid #adadad;text-shadow:1px 1px 0 #0f0f0f">Box title</div>
<div class="su-box-content">
import java.sql._<br />
import java.net._</p>
<p>Connection conn = DriverManager.getConnection(&#8220;jdbc:hsqldb:mem:testdb&#8221;, &#8220;sa&#8221;, &#8220;&#8221;)<br />
try {<br />
PreparedStatement stmt = conn.prepareStatement(&#8220;INSERT INTO urls (url) VALUES (?)&#8221;)<br />
stmt.setObject(1, new URL(&#8220;http://www.codecommit.com&#8221;))</p>
<p>stmt.executeUpdate()<br />
stmt.close()<br />
} catch {<br />
case e:SQLException =&gt; println(&#8220;Database error&#8221;)<br />
case e:MalformedURLException =&gt; println(&#8220;Bad URL&#8221;)</p>
<p>case e =&gt; {<br />
println(&#8220;Some other exception type:&#8221;)<br />
e.printStackTrace()<br />
}<br />
} finally {<br />
conn.close()<br />
}
</p></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/power-of-scala-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Class in Scala &#8211; Part 2</title>
		<link>http://www.solutionhacker.com/class-in-scala-part-2/</link>
		<comments>http://www.solutionhacker.com/class-in-scala-part-2/#comments</comments>
		<pubDate>Tue, 03 Jul 2012 21:29:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Scale and Optimize]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=1264</guid>
		<description><![CDATA[<h2>Common knowledge in this space</h2>
When I first dealt with class, the main concept I learnt was that it encapsulates both related properties and behaviors. Then I started learning the constructor, access scope, mutability, type constraints, inheritance and etc. I would like Scala addressing those OO concepts as well or it could provide a better abstraction of this concept.
<h2>Define a regular class</h2>
First, let me take the example used by a blog as a starting point.
[box title="Regular class definition" color="#333333"]
<pre>//default primary constructor
class Person {
  private var name = "Daniel Spiewak" // private field that could be changed
  val ssn = 1234567890    // public constant field
  var age = 0             // public field

  // public method
  def firstName() = splitName()(0)   // public method

  // import with method scope and round() from math can only be used in this method.
  // visible to subclass and enclosing class
  protected def guessAge() = {
    import Math._
    round(random * 20)
  }

  //private method that can be accessed by object of the same class
  private def splitName() = name.split(" ")  

  //access-restricted to both the enclosing class <em>and</em> the enclosing package
  private[mypackage] def myMethod = "test" 

  //access-restricted to this instance but no others.
  private[this] def myMethod2 = "test2"
}</pre>
[/box]
<strong>Rules:</strong>
<ol>
	<li>The primary constructor for a class is coded in-line in the class definition. Parameters of the primary constructor turn into fields that are initialized with the construction parameters.</li>
	<li>The primary constructor can be made <strong>private</strong> by adding the access modifier private before the parameter list.</li>
	<li>Class parameters:
<ul>
	<li><strong>val</strong> to make them immutable instance value whereas</li>
	<li><strong>var</strong> to make them mutable.</li>
</ul>
</li>
	<li>Class parameters can be preceded by an access modifier such as <strong>private</strong> or <strong>protected</strong>. By default, it is <strong>public</strong> on both class and method.<strong></strong>
<ul>
	<li><strong>protected</strong> by default limits access to only subclasses, unlike Java which also allows access to other classes in the same package.</li>
	<li><strong>modifier[package] </strong>notation in Scala gives you more control of visibility.</li>
	<li>Scala has far fewer method modifiers than Java does, primarily because it doesn’t <em>need</em> so many.  For example, Scala supports the <em>final</em> modifier, but it doesn’t support <em>abstract, native </em>or<em> synchronized:</em></li>
</ul>
</li>
	<li>auto-generate get/set methods but in different forms. To generate the <strong>setXxx</strong> and <strong>getXxx</strong> as Java Bean,  you need to use annotation: <strong>@BeanProperty</strong>.
<ul>
	<li>If the field is private, the getter and setter are private. (NOTE: other object under the same class can access the private field. ie <strong>class-private</strong>)</li>
	<li>If the field is a val, only a getter is generated.</li>
	<li>If you don’t want any getter or setter, declare the field as <strong>private[this]</strong>. (Note: other object under the same class cannot access this. ie. <strong>object-private</strong>).</li>
</ul>
</li>
	<li>A class can have a companion "object" (ie. singleton) with the same name but both of them must be at the same source file. They can access each other private features.</li>
	<li>Scala doesn’t <em>force</em> you to declare the return type for your methods.  Once again the type inference mechanism can come into play and the return type will be inferred.  The exception to this is if the method can return at different points in the execution flow (so if it has an explicit return statement).  In this case, Scala forces you to declare the return type to ensure unambiguous behavior. This “returnless” form becomes extremely important when dealing with anonymous methods.</li>
	<li><a href="http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-3">Access scope difference between Java and Scala</a></li>
</ol>
<h2>An example demonstrate all modifiers in Scala</h2>
[box title="In Scala" color="#333333"]
<pre>abstract class Person {
  private var age = 0

  def firstName():String
  final def lastName() = "Spiewak"

  def incrementAge() = {
    synchronized {
      age += 1
    }
  }

  @native
  def hardDriveName():String
}</pre>
[/box]

[box title="In Java" color="#333333"]
<pre>public abstract class Person {
    private int age = 0;

    public abstract String firstName();

    public final String lastName() {
        return "Spiewak";
    }

    public synchronized void incrementAge() {
        age += 1;
    }

    public native String hardDriveAge();
}</pre>
[/box]

<strong>Rules</strong>
<ol>
	<li><strong>abstract</strong> is used in the class but not method level. No body method definition is considered to be abstract.</li>
	<li><strong>final</strong> can be used in the method level but not at the field level</li>
	<li><strong>native</strong> method needs to use annotation to specify</li>
	<li><strong>synchronized</strong> method no longer there in Scala. But you can use code block level synchronized to achieve the same. However, we should consider to move to Scala <strong>actor-based concurrency model</strong> instead.</li>
</ol>
<h2>Singleton</h2>
Scala doesn't support static member/methods. But you can use "object" to get the same capability. Basically, you can see object as singleton but there is no instance been created.

[box title="Companion object" color="#333333"]
<pre>    class MyString(val jString:String) {
      private var extraData = ""
      override def toString = jString+extraData
    }
    object MyString {
      def apply(base:String, extras:String) = {
        val s = new MyString(base)
        s.extraData = extras
        s
      }
      def apply(base:String) = new MyString(base)
    }
    println(MyString("hello"," world"))
    println(MyString("hello"))</pre>
[/box]]]></description>
				<content:encoded><![CDATA[<h2>Common knowledge in this space</h2>
<p>When I first dealt with class, the main concept I learnt was that it encapsulates both related properties and behaviors. Then I started learning the constructor, access scope, mutability, type constraints, inheritance and etc. I would like Scala addressing those OO concepts as well or it could provide a better abstraction of this concept.</p>
<h2>Define a regular class</h2>
<p>First, let me take the example used by a blog as a starting point.</p>
<div class="su-box" style="border:1px solid #292929">
<div class="su-box-title" style="background-color:#333333;border-top:1px solid #adadad;text-shadow:1px 1px 0 #0f0f0f">Regular class definition</div>
<div class="su-box-content">
<pre><pre>//default primary constructor
class Person {
&nbsp;&nbsp;private var name = &quot;Daniel Spiewak&quot; // private field that could be changed
&nbsp;&nbsp;val ssn = 1234567890&nbsp;&nbsp;&nbsp;&nbsp;// public constant field
&nbsp;&nbsp;var age = 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // public field

&nbsp;&nbsp;// public method
  def firstName() = splitName()(0)&nbsp;&nbsp; // public method

&nbsp;&nbsp;// import with method scope and round() from math can only be used in this method.
&nbsp;&nbsp;// visible to subclass and enclosing class
&nbsp;&nbsp;protected def guessAge() = {
&nbsp;&nbsp;&nbsp;&nbsp;import Math._
&nbsp;&nbsp;&nbsp;&nbsp;round(random * 20)
&nbsp;&nbsp;}

&nbsp;&nbsp;//private method that can be accessed by object of the same class
&nbsp;&nbsp;private def splitName() = name.split(&quot; &quot;)&nbsp;&nbsp;

&nbsp;&nbsp;//access-restricted to both the enclosing class &lt;em&gt;and&lt;/em&gt; the enclosing package
&nbsp;&nbsp;private[mypackage] def myMethod = &quot;test&quot; 

&nbsp;&nbsp;//access-restricted to this instance but no others.
&nbsp;&nbsp;private[this] def myMethod2 = &quot;test2&quot;
}</pre></pre>
</div>
</div>
<p><strong>Rules:</strong></p>
<ol>
<li>The primary constructor for a class is coded in-line in the class definition. Parameters of the primary constructor turn into fields that are initialized with the construction parameters.</li>
<li>The primary constructor can be made <strong>private</strong> by adding the access modifier private before the parameter list.</li>
<li>Class parameters:
<ul>
<li><strong>val</strong> to make them immutable instance value whereas</li>
<li><strong>var</strong> to make them mutable.</li>
</ul>
</li>
<li>Class parameters can be preceded by an access modifier such as <strong>private</strong> or <strong>protected</strong>. By default, it is <strong>public</strong> on both class and method.<strong></strong>
<ul>
<li><strong>protected</strong> by default limits access to only subclasses, unlike Java which also allows access to other classes in the same package.</li>
<li><strong>modifier[package] </strong>notation in Scala gives you more control of visibility.</li>
<li>Scala has far fewer method modifiers than Java does, primarily because it doesn’t <em>need</em> so many.  For example, Scala supports the <em>final</em> modifier, but it doesn’t support <em>abstract, native </em>or<em> synchronized:</em></li>
</ul>
</li>
<li>auto-generate get/set methods but in different forms. To generate the <strong>setXxx</strong> and <strong>getXxx</strong> as Java Bean,  you need to use annotation: <strong>@BeanProperty</strong>.
<ul>
<li>If the field is private, the getter and setter are private. (NOTE: other object under the same class can access the private field. ie <strong>class-private</strong>)</li>
<li>If the field is a val, only a getter is generated.</li>
<li>If you don’t want any getter or setter, declare the field as <strong>private[this]</strong>. (Note: other object under the same class cannot access this. ie. <strong>object-private</strong>).</li>
</ul>
</li>
<li>A class can have a companion &#8220;object&#8221; (ie. singleton) with the same name but both of them must be at the same source file. They can access each other private features.</li>
<li>Scala doesn’t <em>force</em> you to declare the return type for your methods.  Once again the type inference mechanism can come into play and the return type will be inferred.  The exception to this is if the method can return at different points in the execution flow (so if it has an explicit return statement).  In this case, Scala forces you to declare the return type to ensure unambiguous behavior. This “returnless” form becomes extremely important when dealing with anonymous methods.</li>
<li><a href="http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-3">Access scope difference between Java and Scala</a></li>
</ol>
<h2>An example demonstrate all modifiers in Scala</h2>
<div class="su-box" style="border:1px solid #292929">
<div class="su-box-title" style="background-color:#333333;border-top:1px solid #adadad;text-shadow:1px 1px 0 #0f0f0f">In Scala</div>
<div class="su-box-content">
<pre><pre>abstract class Person {
&nbsp;&nbsp;private var age = 0

&nbsp;&nbsp;def firstName():String
&nbsp;&nbsp;final def lastName() = &quot;Spiewak&quot;

&nbsp;&nbsp;def incrementAge() = {
&nbsp;&nbsp;&nbsp;&nbsp;synchronized {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age += 1
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}

&nbsp;&nbsp;@native
&nbsp;&nbsp;def hardDriveName():String
}</pre></pre>
</div>
</div>
<div class="su-box" style="border:1px solid #292929">
<div class="su-box-title" style="background-color:#333333;border-top:1px solid #adadad;text-shadow:1px 1px 0 #0f0f0f">In Java</div>
<div class="su-box-content">
<pre><pre>public abstract class Person {
&nbsp;&nbsp;&nbsp;&nbsp;private int age = 0;

&nbsp;&nbsp;&nbsp;&nbsp;public abstract String firstName();

&nbsp;&nbsp;&nbsp;&nbsp;public final String lastName() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &quot;Spiewak&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public synchronized void incrementAge() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age += 1;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public native String hardDriveAge();
}</pre></pre>
</div>
</div>
<p><strong>Rules</strong></p>
<ol>
<li><strong>abstract</strong> is used in the class but not method level. No body method definition is considered to be abstract.</li>
<li><strong>final</strong> can be used in the method level but not at the field level</li>
<li><strong>native</strong> method needs to use annotation to specify</li>
<li><strong>synchronized</strong> method no longer there in Scala. But you can use code block level synchronized to achieve the same. However, we should consider to move to Scala <strong>actor-based concurrency model</strong> instead.</li>
</ol>
<h2>Singleton</h2>
<p>Scala doesn&#8217;t support static member/methods. But you can use &#8220;object&#8221; to get the same capability. Basically, you can see object as singleton but there is no instance been created.</p>
<div class="su-box" style="border:1px solid #292929">
<div class="su-box-title" style="background-color:#333333;border-top:1px solid #adadad;text-shadow:1px 1px 0 #0f0f0f">Companion object</div>
<div class="su-box-content">
<pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;class MyString(val jString:String) {
&nbsp;&nbsp;&nbsp;&nbsp;  private var extraData = &quot;&quot;
&nbsp;&nbsp;&nbsp;&nbsp;  override def toString = jString+extraData
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;object MyString {
&nbsp;&nbsp;&nbsp;&nbsp;  def apply(base:String, extras:String) = {
&nbsp;&nbsp;&nbsp;&nbsp;    val s = new MyString(base)
&nbsp;&nbsp;&nbsp;&nbsp;    s.extraData = extras
&nbsp;&nbsp;&nbsp;&nbsp;    s
&nbsp;&nbsp;&nbsp;&nbsp;  }
&nbsp;&nbsp;&nbsp;&nbsp;  def apply(base:String) = new MyString(base)
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;println(MyString(&quot;hello&quot;,&quot; world&quot;))
&nbsp;&nbsp;&nbsp;&nbsp;println(MyString(&quot;hello&quot;))</pre></pre>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/class-in-scala-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
