<?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>[MeIsProgrammer] &#187; Scala</title>
	<atom:link href="http://just-thor.com/tag/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://just-thor.com</link>
	<description>all play and no work makes me a dull boy</description>
	<lastBuildDate>Sat, 03 Jul 2010 18:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Java 4-ever video</title>
		<link>http://just-thor.com/2010/06/29/java-4-ever-video/</link>
		<comments>http://just-thor.com/2010/06/29/java-4-ever-video/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 04:41:57 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=148</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/fzza-ZbEY70&amp;hl=en_US&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/fzza-ZbEY70&amp;hl=en_US&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2010/06/29/java-4-ever-video/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala reading content from the Web</title>
		<link>http://just-thor.com/2009/10/16/scala-reading-content-from-the-web/</link>
		<comments>http://just-thor.com/2009/10/16/scala-reading-content-from-the-web/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 10:54:03 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=84</guid>
		<description><![CDATA[I use scala.io.Source in my previous post, and now I am going to show another usage of this util. Let&#8217;s write a simple RSS feed parser that reads feed from [...]]]></description>
			<content:encoded><![CDATA[<p>I use scala.io.Source in my previous <a href="http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/">post</a>, and now I am going to show another usage of this util. Let&#8217;s write a simple RSS feed parser that reads feed from my website and parse it.</p>
<p>Here&#8217;s the code of it.</p>
<p><pre><pre class="brush: scala">
package com.justthor.reader

import scala.io.Source
import scala.xml._

object SimpleFeedReader {
&nbsp;&nbsp;def main(args : Array[String]) : Unit = {
&nbsp;&nbsp;&nbsp;&nbsp;val url = &quot;http://just-thor.com/feed/&quot;
&nbsp;&nbsp;&nbsp;&nbsp;val data = XML.loadString( Source.fromURL(url).mkString )
&nbsp;&nbsp;&nbsp;&nbsp;data \ &quot;channel&quot; \ &quot;item&quot; foreach(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a =&gt; println((a \ &quot;title&quot; text) + &quot; == &quot;&nbsp;&nbsp;+ (a \ &quot;link&quot; text))
&nbsp;&nbsp;&nbsp;&nbsp;)
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>Ok,let&#8217;s go thru the code line by line.</p>
<p>At line 09, again we use Source to load content from url,by calling the method <strong>fromURL</strong>(quite obvious,right?).The <strong>mkString </strong>will create a String out of the content. Then, we use <strong>XML.loadString</strong> to parse the String into an XML object. </p>
<p>At line 10, we traverse the XML object with something like XQuery.<br />
The RSS xml format is something like this<br />
<pre><pre class="brush: xml">
&lt;channel&gt;
&lt;link&gt;...&lt;/link&gt;
&lt;description&gt;...&lt;/description&gt;
&lt;!-- item 1--&gt;
&lt;item&gt;
&nbsp;&nbsp;&lt;title&gt;&lt;/title&gt;
&nbsp;&nbsp;&lt;link&gt;&lt;/link&gt;
&lt;/item&gt;
&lt;!-- item 2--&gt;
&lt;item&gt;
&nbsp;&nbsp;&lt;title&gt;&lt;/title&gt;
&nbsp;&nbsp;&lt;link&gt;&lt;/link&gt;
&lt;/item&gt;
&lt;!-- and the rest goes here --&gt;
&lt;/channel&gt;
</pre></pre></p>
<p>So when we write <pre class="brush: scala">data \ &quot;channel&quot; \ &quot;item&quot;</pre>, we mean traverse to channel node, then item node of it.It will return a collection of item nodes, and then we loop it. At line 11, we will print the text within the title node and the link node.</p>
<p>Here&#8217;s the output of the result<br />
<pre><pre class="brush: text">
File Reading in Java and Scala == http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/
PHP communicate with Java using JSON – Unicode Version == http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/
Scala And JDBC == http://just-thor.com/2009/09/27/scala-and-jdbc/
A mistake or fate? == http://just-thor.com/2009/09/17/a-mistake-or-fate/
Women are weird == http://just-thor.com/2009/09/13/women-are-weird/
20 must visit sites for Java developer == http://just-thor.com/2009/09/12/20-must-visit-sites-for-java-developer/
Why do I practice pair programming? == http://just-thor.com/2009/09/08/why-do-i-practice-pair-programming/
Installing Scala on Windows == http://just-thor.com/2009/09/02/installing-scala-on-window/
Logging for different requested URI in Apache HTTP Server == http://just-thor.com/2009/09/01/logging-for-different-requested-uri-in-apache-http-server/
Say hello to Java and Scala == http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/
</pre></pre></p>
<p>And now we have a very simple yet functional RSS parser.</p>
<p>Happy coding, <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/10/16/scala-reading-content-from-the-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Reading in Java and Scala</title>
		<link>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/</link>
		<comments>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 16:46:47 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=75</guid>
		<description><![CDATA[Today we are going to look at reading files in Java and how to do the same thing in Scala. This is normally how we read files in Java.First we [...]]]></description>
			<content:encoded><![CDATA[<p>Today we are going to look at reading files in Java and how to do the same thing in Scala.</p>
<p>This is normally how we read files in Java.First we open a reading stream, then read the content and process it,and at last we close the stream.</p>
<p><pre><pre class="brush: java">
package com.justthor;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SampleFileReader {

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param args
&nbsp;&nbsp; */
&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader in; 
&nbsp;&nbsp;&nbsp;&nbsp;try {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open file
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in = new BufferedReader(new FileReader(&quot;test.txt&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String str;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Start reading
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((str = in.readLine()) != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Print the content
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(str);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Close the stream
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>With some third party libraries, such as <a href="http://commons.apache.org/io/index.html">Apache Common Io</a>, we can simplify the code to something like this.</p>
<p><pre><pre class="brush: java">
package com.justthor;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class SampleFileReader {

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param args
&nbsp;&nbsp; */
&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;try {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;String&gt; contents = FileUtils.readLines(new File(&quot;test.txt&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( String content : contents ) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(content);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// TODO Auto-generated catch block
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>Well, it does look a bit simple now compare to the previous version. But we only manage to type 1 or 2 lines less than the previous version. This is due to we are force to handle the checked IOException. What if we try to achieve the same thing using Scala?</p>
<p><pre><pre class="brush: scala">
package com.justthor

import scala.io.Source

object SampleFileReader {
 def main(args : Array[String]) = {
&nbsp;&nbsp;Source fromFile(&quot;test.txt&quot;) getLines foreach { println }
 }
}
</pre></pre></p>
<p>Its much simple, right? Scala provides an util for IO named <strong>Source</strong>. This util read the file and get the contents by calling <strong>getLines</strong> method. This <strong>getLines</strong> method returns a <strong>Iterator[String]</strong>. And from there we can process the content by looping the returned iterator. We can use <strong>Source</strong> util to do more than reading files. I will provide more examples in the next few posts.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scala And JDBC</title>
		<link>http://just-thor.com/2009/09/27/scala-and-jdbc/</link>
		<comments>http://just-thor.com/2009/09/27/scala-and-jdbc/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 10:00:24 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=68</guid>
		<description><![CDATA[I was writing some small programs yesterday trying to read data from one database and input to another database.Hence,I tried to use Scala to do that.I&#8217;ve never write scala to [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing some small programs yesterday trying to read data from one database and input to another database.Hence,I tried to use Scala to do that.I&#8217;ve never write scala to connect to a database using JDBC before,but I am quite surprise with the simplicity of the code.It merely took me 10 minutes to finish writing the code.</p>
<p>Here&#8217;s the code<br />
<pre><pre class="brush: scala">
// Import JDBC package from standard Java SDK
import java.sql.{Connection, DriverManager, ResultSet};

class DataReader {
&nbsp;&nbsp;// Load the driver
&nbsp;&nbsp;Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance;

&nbsp;&nbsp;val connection = DriverManager getConnection &quot;jdbc:mysql://localhost:3306/mail?user=root&amp;amp;password=&amp;amp;characterEncoding=UTF8&quot;

&nbsp;&nbsp;def startReadingData(){
&nbsp;&nbsp;&nbsp;&nbsp;try{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Create statement for readonly
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val statement = conn createStatement (ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val rs = statement executeQuery &quot;select * from mail_queues order by id&quot;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Looping resultset
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (rs next) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;println (rs getLong &quot;id&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statement close
&nbsp;&nbsp;&nbsp;&nbsp;}catch {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case e =&amp;gt; e printStackTrace
&nbsp;&nbsp;&nbsp;&nbsp;}finally {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection close
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}

}
</pre></pre></p>
<p>Ok, you maybe wondering,why are so many dots(.) and parentheses missing?In Scala,when you are invoking one function with no or one parameter, you can omit the dot and parentheses. And Scala doesn&#8217;t have checked exception, which means you can write the above code without enclosed it with try&#8230;catch clause.I included it to make sure the DB connection is properly closed after use.</p>
<p>Happy coding.:)</p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/09/27/scala-and-jdbc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing Scala on Windows</title>
		<link>http://just-thor.com/2009/09/02/installing-scala-on-window/</link>
		<comments>http://just-thor.com/2009/09/02/installing-scala-on-window/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 06:42:51 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=47</guid>
		<description><![CDATA[Here&#8217;s some simple guide for you to install Scala in Windows environment. 1.Make sure you have Java installed. Fire up a command line window and type &#8220;java -version&#8221;, if you [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s some simple guide for you to install Scala in Windows environment.</p>
<p>1.Make sure you have Java installed. Fire up a command line window and type &#8220;java -version&#8221;, if you have installed Java, you should be able to see the Java version message displayed.</p>
<p>2.Download Scala from <a title="Download Scala" href="http://www.scala-lang.org/downloads" target="_blank">http://www.scala-lang.org/downloads</a> . The latest stable version is 2.7.5 as this post is written.</p>
<p>3.Unzip the file and put in, let&#8217;s say &#8220;c:\dev\scala&#8221;.</p>
<p>4.Add a new environment variable named &#8220;SCALA_HOME&#8221; and the value is &#8220;c:\dev\scala&#8221;.</p>
<p>5.In your &#8220;PATH&#8221; environment variable, append the following at the end of the line &#8220;%SCALA_HOME%\bin&#8221;.</p>
<p>6.Fire up another command line window and type &#8220;scala&#8221;. You should be able to see the following message.<br />
<pre><pre>Welcome to Scala version 2.7.5.final (Java HotSpot(TM) Client VM, Java 1.6.0_14).
Type in expressions to have them evaluated.
Type :help for more information.
scala&amp;gt;</pre></pre><br />
Type &#8220;:quit&#8221; to quit the Scala interactive shell and we are done.</p>
<p>Happy coding.</p>
<p> <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/09/02/installing-scala-on-window/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Say hello to Java and Scala</title>
		<link>http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/</link>
		<comments>http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 14:09:39 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=35</guid>
		<description><![CDATA[Let&#8217;s write a legendary &#8220;Hello world&#8221; program in both Java and Scala language. This is how we do in Java. public class Hello { public static void main(String[] args) { [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s write a legendary &#8220;Hello world&#8221; program in both Java and Scala language.</p>
<p>This is how we do in Java.<br />
<pre><pre class="brush: java">
public class Hello {
 public static void main(String[] args) {
&nbsp;&nbsp;System.out.println(&quot;Hello world&quot;);
 }
}</pre></pre><br />
and this is how we do in Scala<br />
<pre><pre class="brush: scala">object Hello {
 def main(args : Array[String]) = println(&quot;Hello world&quot;)
}</pre></pre><br />
So when you execute both code,they actually return the same result.What about printing arguments instead?</p>
<p>Java style<br />
<pre><pre class="brush: java">public class HelloArgument {
 public static void main(String[] args) {
&nbsp;&nbsp;for (String arg : args) {
&nbsp;&nbsp; System.out.println(&quot;Hello, &quot; + arg);
&nbsp;&nbsp;}
 }
}</pre></pre></p>
<p>Scala style<br />
<pre><pre class="brush: scala">object HelloArgument {
 def main(args : Array[String]) = args.foreach(arg =&amp;gt; println(&quot;Hello, &quot; + arg))
}</pre></pre></p>
<p>Pretty impressive,huh?Comparing Java,we are actually writing less and more readable code in Scala,and the results are the same.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
