<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;A0cMRXk7fip7ImA9WhRbGE4.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229</id><updated>2012-02-09T17:58:04.706-08:00</updated><category term="mobile" /><category term="setup" /><category term="Use cases" /><category term="activity" /><category term="design patterns" /><category term="GWT" /><category term="tools" /><category term="Javascript Native Interface" /><category term="swing" /><category term="WYSIWYG" /><category term="GWT Designer" /><category term="unit" /><category term="development" /><category term="signature" /><category term="dynaTrace" /><category term="annotations" /><category term="problem 3" /><category term="hosting" /><category term="tomcat" /><category term="open source" /><category term="service" /><category term="flashcards" /><category term="application development" /><category term="DataSource" /><category term="problem 9" /><category term="test" /><category term="Scala" /><category term="iphone" /><category term="plugin" /><category term="study" /><category term="software engineering" /><category term="functional" /><category term="eclipse" /><category term="code" /><category term="GWTServerServlet" /><category term="Spring" /><category term="sqllite" /><category term="database" /><category term="computer science" /><category term="instrument" /><category term="JNI" /><category term="ExtGWT" /><category term="Struts" /><category term="JSNI" /><category term="type" /><category term="java" /><category term="refactoring" /><category term="GoDaddy" /><category term="refactor" /><category term="security" /><category term="sequence" /><category term="programming" /><category term="StudyBlue" /><category term="jsp" /><category term="multithreading" /><category term="UML" /><category term="GwtRpcDataSource" /><category term="XML" /><category term="online flashcards" /><category term="Java Native Interface" /><category term="web services" /><category term="NetBeans" /><category term="Separation of Concerns" /><category term="inversion of control" /><category term="android" /><category term="integration" /><category term="paypal" /><category term="SmartGWT" /><category term="Object Oriented Analysis and Design" /><category term="interviewing" /><category term="euler" /><category term="shared" /><category term="2.0" /><category term="GWT RPC" /><category term="functional programming" /><category term="project euler" /><category term="Qt" /><category term="design" /><category term="good software design" /><category term="cards" /><category term="Chain of Responsibility" /><category term="problem 4" /><category term="code java" /><title>Ben Arciszewski Development</title><subtitle type="html">A Blog about developing with Java, Google Web Toolkit (GWT), Android, Scala and other technologies.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://benarchie.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>43</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/BenArciszewskiDevelopment" /><feedburner:info uri="benarciszewskidevelopment" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CkAHSHk7cSp7ImA9WhRUEEw.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-1546597770552996001</id><published>2012-01-19T14:05:00.000-08:00</published><updated>2012-01-19T14:05:39.709-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-19T14:05:39.709-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="functional" /><category scheme="http://www.blogger.com/atom/ns#" term="euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><title>Project Euler Problem 10 in Scala</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;This is 10 in a series.  The previous post is at, &lt;a href="http://benarchie.blogspot.com/2012/01/project-euler-problem-9-in-scala.html"&gt;Project Euler Problem 9 in Scala&lt;/a&gt;. &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Problem 10 on project Euler is:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.&lt;br /&gt;Find the sum of all the primes below two million.&lt;/span&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Problem 10 posed an interesting issue for me. &amp;nbsp;I came up with a solution I thought would work well right away, but I couldn't seem to get the correct answer. &amp;nbsp;After poring over my code and playing around, I finally found my issue - my isPrime function had an error in it, causing me to be off by 2. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;What is interesting about this scenario is that I've been using that same function for lots of different problems, but since the function was only broken for the number 2, it didn't matter in those. &amp;nbsp;If I had some sort of test that say, verified the function against a random set of prime numbers, I would have found the issue much sooner. This speaks to how important it is to test even seemingly working things whenever you can. &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Another by product of the issue is that I did a little more research on calculating prime numbers. &amp;nbsp;Wikipedia had a nice article about the&amp;nbsp;&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"&gt;Sieve of Eratosthenes&lt;/a&gt;. &amp;nbsp;This gave me a much better understanding of how it works. &amp;nbsp;While I am not changing my code (yet) to use a sieve, it was certainly valuable to learn more about how its done. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Well, I did manage to finish it, and here is my solution. &amp;nbsp;Enjoy:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem10 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; var holder : BigDecimal = new BigDecimal(new java.math.BigDecimal("0"));&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; (2 to 1999999).filter{next =&amp;gt; val prime = isPrime(next);if(prime){holder = holder + new java.math.BigDecimal(new Integer(next).toString())};prime}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; println(holder)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; /**&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* Brute force method. &amp;nbsp;Perhaps a better method&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* can be implemented here? &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;*/&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def isPrime(number: Long) : Boolean = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; if(number == 2)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return true&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; if(number%2==0 || number % 3==0)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return false;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; val sqrt:Int = Math ceil (Math sqrt number) &amp;nbsp;intValue;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; (true /: (3 to sqrt))((isPrime, next) =&amp;gt; {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if(number % next == 0)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return false&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; isPrime&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; })&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Now that I've completed #10, I get a Euler award. &amp;nbsp;I have become a 'Decathlete' for finishing 10 problems in a row. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;And with that, I think I will finish blogging about my Euler solutions. &amp;nbsp;I still plan on working through the problems, but I think I've worn out my Euler solution posts and will move on to some other interesting topics. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-1546597770552996001?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/5fi_DezVGV_J-L2UokoSr3Mtq5M/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5fi_DezVGV_J-L2UokoSr3Mtq5M/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/5fi_DezVGV_J-L2UokoSr3Mtq5M/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5fi_DezVGV_J-L2UokoSr3Mtq5M/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/wE4tdl6UFQI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/1546597770552996001/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2012/01/project-euler-problem-10-in-scala.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1546597770552996001?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1546597770552996001?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/wE4tdl6UFQI/project-euler-problem-10-in-scala.html" title="Project Euler Problem 10 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2012/01/project-euler-problem-10-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0UGQn09eCp7ImA9WhRVFUg.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-1626052557159584157</id><published>2012-01-14T08:40:00.000-08:00</published><updated>2012-01-14T08:40:23.360-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-14T08:40:23.360-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="study" /><category scheme="http://www.blogger.com/atom/ns#" term="StudyBlue" /><category scheme="http://www.blogger.com/atom/ns#" term="cards" /><category scheme="http://www.blogger.com/atom/ns#" term="online flashcards" /><category scheme="http://www.blogger.com/atom/ns#" term="iphone" /><category scheme="http://www.blogger.com/atom/ns#" term="flashcards" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><title>StudyBlue Flashcards nominated for Best App Ever Awards</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;I've been working for &lt;a href="http://www.studyblue.com/"&gt;StudybBlue&lt;/a&gt; for about 9 months now. &amp;nbsp;If you don't know what StudyBlue is, it's a fantastic place for students to create online flashcards, study those flashcards, create quizzes from them, and track their progress. &amp;nbsp;In other words, its a product that makes students more efficient and effective. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;a href="http://www.studyblue.com/"&gt;StudyBlue&lt;/a&gt; is more than a Web site, however. &amp;nbsp;It also produces native flashcard applications for &lt;a href="http://itunes.apple.com/ie/app/studyblue/id323887414"&gt;iPhone&lt;/a&gt; and &lt;a href="https://market.android.com/details?id=com.studyblue&amp;amp;feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5zdHVkeWJsdWUiXQ.."&gt;Android&lt;/a&gt;&amp;nbsp;to allow students to study their cards on the go. &amp;nbsp;The goal for our company is to create a total user experience and we invest a lot of time, effort and energy into our application designs. &amp;nbsp;Apparently, that hard work is starting to payoff. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;We were recently notified that we were nominated for the the &lt;a href="http://www.bestappever.com/v/hsed/2/com.studyblue/vote"&gt;Best App Ever Awards&lt;/a&gt;. &amp;nbsp;While its not the biggest award or the best known, the interesting thing about it is that it is a user-driven award. &amp;nbsp;It means that one of our users nominated our app and other users will be voting on it. &amp;nbsp;In my mind this is one of the highest&amp;nbsp;compliments &lt;a href="http://www.studyblue.com/"&gt;StudyBlue&lt;/a&gt;&amp;nbsp;could be paid. &amp;nbsp;After all, it doesn't really matter what anyone thinks but your users, and it seems that we're keeping at least some of ours pretty happy. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;If you are a student and use&amp;nbsp;&lt;/span&gt;&lt;a href="http://www.studyblue.com/" style="font-family: Arial, Helvetica, sans-serif;"&gt;StudyBlue&lt;/a&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;, please&amp;nbsp;&lt;/span&gt;&lt;a href="http://www.bestappever.com/v/hsed/2/com.studyblue/vote" style="font-family: Arial, Helvetica, sans-serif;"&gt;vote&lt;/a&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;for our app. &amp;nbsp;There would be nothing more satisfying than knowing that our users think we're the Best App Ever.&lt;/span&gt;
&lt;br /&gt;
&lt;a class="bae-nominate-btt" href="http://www.bestappever.com/v/hsed/2/com.studyblue" style="text-align: center;"&gt;Vote for STUDYBLUE Flashcards for Best High School Student App&lt;/a&gt;&lt;link href="http://www.bestappever.com/template/2011/vote-button.css" media="screen" rel="stylesheet"&gt;&lt;/link&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-1626052557159584157?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ZD0h32IKA10zqnVI0xKgoNUjDcI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ZD0h32IKA10zqnVI0xKgoNUjDcI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ZD0h32IKA10zqnVI0xKgoNUjDcI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ZD0h32IKA10zqnVI0xKgoNUjDcI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/JLHEYI8vCng" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/1626052557159584157/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2012/01/studyblue-flashcards-nominated-for-best.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1626052557159584157?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1626052557159584157?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/JLHEYI8vCng/studyblue-flashcards-nominated-for-best.html" title="StudyBlue Flashcards nominated for Best App Ever Awards" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2012/01/studyblue-flashcards-nominated-for-best.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEAHRXw9fCp7ImA9WhRVFUg.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-5590851814503849427</id><published>2012-01-14T07:58:00.000-08:00</published><updated>2012-01-14T07:58:54.264-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-14T07:58:54.264-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="problem 9" /><title>Project Euler Problem 9 in Scala</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;This is 9 in a series. &amp;nbsp;The previous post is at, &lt;a href="http://benarchie.blogspot.com/2012/01/project-euler-problem-8-in-scala.html"&gt;Project Euler Problem 8 in Scala&lt;/a&gt;.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;I've still been (slowly) working my way through the Project Euler Problems. &amp;nbsp;The next one is problem 9 which reads:&lt;/span&gt;&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;A Pythagorean triplet is a set of three natural numbers, a &amp;nbsp;b &amp;nbsp;c, for which,&lt;br /&gt;a^2 + b^2 = c^2&lt;br /&gt;For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.&lt;br /&gt;There exists exactly one Pythagorean triplet for which a + b + c = 1000.&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;Find the product abc.&lt;/span&gt;&lt;/blockquote&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;For this problem, I didn't use anything fancy. &amp;nbsp;Two for-comprehensions and basic math produced the solution in short order. &amp;nbsp;The only thing to mention about this solution is that it actually finds the solution twice since I'm iterating1 to 998 twice. I considered trying to optimize this, however, it runs plenty fast. &amp;nbsp;Since my goal in solving these isn't to come up with the fastest solution, but to come up with a solution that has no mutable state, I have met my goal. &amp;nbsp;Therefore, I present you the solution to problem 9:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem9 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; for (a &amp;lt;- 1 to 998) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; for (b &amp;lt;- 1 to 998) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; val c = 1000 - (a + b)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if ((a * a) + (b * b) == (c * c)) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; println("the triplet is " + a + " " + b + " " + c)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; println("the product is " + (a * b * c))&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;I think that's all for this one. &amp;nbsp;Problem 10 is in the hopper...&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-5590851814503849427?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/hB9g7hogXMstqF9pW183bVq-qs4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hB9g7hogXMstqF9pW183bVq-qs4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/hB9g7hogXMstqF9pW183bVq-qs4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hB9g7hogXMstqF9pW183bVq-qs4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/xcC2j6MYAhE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/5590851814503849427/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2012/01/project-euler-problem-9-in-scala.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/5590851814503849427?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/5590851814503849427?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/xcC2j6MYAhE/project-euler-problem-9-in-scala.html" title="Project Euler Problem 9 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2012/01/project-euler-problem-9-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0UESHg7eCp7ImA9WhRVEU8.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-1828402580993547721</id><published>2012-01-09T07:00:00.000-08:00</published><updated>2012-01-09T07:00:09.600-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-09T07:00:09.600-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="functional" /><category scheme="http://www.blogger.com/atom/ns#" term="development" /><title>Project Euler Problem 8 in Scala</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;This is 8 in a series. &amp;nbsp;The previous post is at,&amp;nbsp;&lt;/span&gt;&lt;a href="http://benarchie.blogspot.com/2011/12/project-euler-problem-7-in-scala.html" style="background-color: white; color: #336699; line-height: 16px; text-align: left;"&gt;Project Euler Problem 5 in Scala&lt;/a&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;.&lt;/span&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;&amp;nbsp;&lt;/span&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px; text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px; text-align: left;"&gt;So we saw problem 6 solved with a 'one-liner'. &amp;nbsp;Then we moved on to problem 7 and I had to have a while loop and control variables, which really disappointed me. Problem 8, however, was not only satisfying, but allowed me to explore even more features of Scala. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px; text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px; text-align: left;"&gt;Before I get into the actual problem, I want to comment on my language progress. &amp;nbsp;My 'native tongue' so-to-speak, is Java. &amp;nbsp;I use Java at work and have for 12 years. I expect it will be paying the bills for years to come. &amp;nbsp;We can debate the merits of Java as a language, and I'm sure every one reading this has an opinion about it, good or bad. &amp;nbsp;Regardless of opinion, I am most productive in Java. &amp;nbsp;Period. &amp;nbsp;Why? &amp;nbsp;Because I have used it so much, and am so familiar with the syntax and libraries that the language 'gets out of the way'. &amp;nbsp;I don't have to think about anything but what the code is intending to do. &amp;nbsp;This makes me very productive. &amp;nbsp;I'm finding that its taking a little longer to get to the point where I feel like the language has stepped back with Scala. &amp;nbsp;Part of it is the rather verbose API documentation, which, just seems to tell me too much and doesn't allow me to get right to 'how do I use this'. &amp;nbsp;Part of it is that Scala has a lot more power and expressiveness than Java, and that takes more time to learn. &amp;nbsp;At any rate, my Scala skills are slowing improving. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px; text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px; text-align: left;"&gt;Now, on to Problem 8:&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; text-align: left;"&gt;&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;blockquote style="line-height: 16px;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;Find the greatest product of five consecutive digits in the 1000-digit number.&lt;/span&gt;&lt;/blockquote&gt;
&lt;blockquote style="line-height: 16px;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;73167176531330624919225119674426574742355349194934&lt;br /&gt;&amp;nbsp;96983520312774506326239578318016984801869478851843&lt;br /&gt;&amp;nbsp;85861560789112949495459501737958331952853208805511&lt;br /&gt;&amp;nbsp;12540698747158523863050715693290963295227443043557&lt;br /&gt;&amp;nbsp;66896648950445244523161731856403098711121722383113&lt;br /&gt;&amp;nbsp;62229893423380308135336276614282806444486645238749&lt;br /&gt;&amp;nbsp;30358907296290491560440772390713810515859307960866&lt;br /&gt;&amp;nbsp;70172427121883998797908792274921901699720888093776&lt;br /&gt;&amp;nbsp;65727333001053367881220235421809751254540594752243&lt;br /&gt;&amp;nbsp;52584907711670556013604839586446706324415722155397&lt;br /&gt;&amp;nbsp;53697817977846174064955149290862569321978468622482&lt;br /&gt;&amp;nbsp;83972241375657056057490261407972968652414535100474&lt;br /&gt;&amp;nbsp;82166370484403199890008895243450658541227588666881&lt;br /&gt;&amp;nbsp;16427171479924442928230863465674813919123162824586&lt;br /&gt;&amp;nbsp;17866458359124566529476545682848912883142607690042&lt;br /&gt;&amp;nbsp;24219022671055626321111109370544217506941658960408&lt;br /&gt;&amp;nbsp;07198403850962455444362981230987879927244284909188&lt;br /&gt;&amp;nbsp;84580156166097919133875499200524063689912560717606&lt;br /&gt;&amp;nbsp;05886116467109405077541002256983155200055935729725&lt;br /&gt;&amp;nbsp;71636269561882670428252483600823257530420752963450&lt;/span&gt;&lt;/blockquote&gt;
&lt;div style="line-height: 16px;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;This problem is pretty straight forward. &amp;nbsp;Iterate the string literal and multiply sets of 5 numbers together. &amp;nbsp;Find the largest of those numbers and return it. &amp;nbsp;Now, in Java, this would take a couple of for loops, a String, possibly a string buffer, some variables to store the results, and an ugly System.out.println....&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 16px;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 16px;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;Not so in Scala. &amp;nbsp;The Collections facilities actually allowed me to solve this in one line, without any mutable state. &amp;nbsp;No, really, I mean it. &amp;nbsp;Check this out:&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: 16px;"&gt;
&lt;span style="color: #333333; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem8 {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; val totalDigits = """[omitted the big string literal here, but you can add it back if you want to run the code...]"""&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; println((for (i &amp;lt;- 5 to totalDigits.length()) yield totalDigits slice (i - 5, i)) collect {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; case i =&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; i.foldLeft(1)((product: Int, nextChar) =&amp;gt; {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (nextChar asDigit) * product&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; })&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; } max)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Okay, its a 'one-liner' but there is a lot going on there. &amp;nbsp;Let's deconstruct the statement. &amp;nbsp;First, we see that everything is wrapped in a 'println', which is nice shorthand in Scala for System.out.println. &amp;nbsp;The second thing we see is that the result of the statement 'max' is what will be printed. &amp;nbsp;max is a collection function. &amp;nbsp;A couple of things to note. &amp;nbsp;Since max takes no arguments, we drop the '()'. &amp;nbsp;Since Scala can infer its a method call, we can even omit the '.'. &amp;nbsp;Now let's look at the rest of the code, shall we?&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Let's go back to the beginning of the statement. &amp;nbsp;We are using the 'for' comprehension. &amp;nbsp;To be honest, the 'for' comprehension in Scala is much more powerful than the 'for' loop in other languages I've used. &amp;nbsp;You can see, we are going to iterate 5 to the length of the string. &amp;nbsp;For each iteration, we are going to yield the slice if i-5 and i. &amp;nbsp;slice is a nice string operator in Scala. &amp;nbsp;Essentially, we're getting 5 characters. &amp;nbsp;In turn we are going to collect the results of folding (There's that foldLeft again....) the 5 character Sequence. &amp;nbsp;The result of all of these will be a Sequence of Int values, on which we call 'max' function.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Boy, that was a mouthful. &amp;nbsp;It was cool, though, wasn't it? &amp;nbsp;It took a few minutes for me to weed through the API and figure out the syntax for this, but in the end, it is a succinct and stateless solution....&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;On to the next one...&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333; font-size: x-small;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-1828402580993547721?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/peBg42z0d8SKdDL9_8qhhQgPYEc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/peBg42z0d8SKdDL9_8qhhQgPYEc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/peBg42z0d8SKdDL9_8qhhQgPYEc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/peBg42z0d8SKdDL9_8qhhQgPYEc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/8U4TyHGbNRo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/1828402580993547721/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2012/01/project-euler-problem-8-in-scala.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1828402580993547721?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1828402580993547721?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/8U4TyHGbNRo/project-euler-problem-8-in-scala.html" title="Project Euler Problem 8 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2012/01/project-euler-problem-8-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0QESXo9fip7ImA9WhRWGUo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-160129080177886422</id><published>2012-01-05T07:00:00.000-08:00</published><updated>2012-01-07T13:21:48.466-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T13:21:48.466-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="functional" /><title>Project Euler Problem 7 in Scala</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;This is 7 in a series. &amp;nbsp;The previous post is at,&amp;nbsp;&lt;/span&gt;&lt;a href="http://benarchie.blogspot.com/2011/12/project-euler-problem-6-in-scala.html" style="background-color: white; color: #336699; line-height: 16px; text-align: left;"&gt;Project Euler Problem 6 in Scala&lt;/a&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;After a very successful solution to problem 6, we get to problem 7. &amp;nbsp;I couldn't figure out how to remove all mutable state from this solution. &amp;nbsp;The issue is that we have to find the 10,001st number which means that we do not know how long we have to iterate. &amp;nbsp;I used a while loop here, and it seems to work fine. &amp;nbsp;Perhaps someone has a solution without a while loop? &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;Here's Project Euler problem 7:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;blockquote class="tr_bq" style="line-height: 16px;"&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.&lt;br /&gt;&amp;nbsp;&lt;span style="background-color: white;"&gt;What is the 10,001st prime number?&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem7 {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;var counter = 1L;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;var number = new BigDecimal(new java.math.BigDecimal("0"));&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;var currentNum = 1l;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span class="Apple-tab-span" style="line-height: 16px; white-space: pre;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;while(counter &amp;lt;= 10001) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;if(isPrime(currentNum)) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;number = new BigDecimal(new java.math.BigDecimal(currentNum));&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;counter = counter + 1;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;currentNum = currentNum + 1;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;println("10001st prime " + number);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; /**&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* Brute force method. &amp;nbsp;Perhaps a better method&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* can be implemented here? &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;*/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def isPrime(number: Long) : Boolean = {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; if(number%2==0)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return false;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; val sqrt:Int = Math ceil (Math sqrt number) &amp;nbsp;intValue;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; (true /: (3 to sqrt))((isPrime, next) =&amp;gt; {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if(number % next == 0)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return false&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; isPrime&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; })&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; text-align: left;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="background-color: white; line-height: 16px;"&gt;}&lt;/span&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;You should note that I've cleaned up my isPrime method. &amp;nbsp;I noticed that it wasn't very functionally written, so I changed it to use a fold left (By the way, foldLeft is turning out to be one of the handiest Scala constructs on the planet.) &amp;nbsp;I am now very comfortable with the isPrime method, at least as a brute force solution.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-160129080177886422?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/flq9y5fScEoCkaXvkaRSkLUnZ_U/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/flq9y5fScEoCkaXvkaRSkLUnZ_U/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/flq9y5fScEoCkaXvkaRSkLUnZ_U/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/flq9y5fScEoCkaXvkaRSkLUnZ_U/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/AB09rIQC-ro" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/160129080177886422/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2012/01/project-euler-problem-7-in-scala.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/160129080177886422?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/160129080177886422?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/AB09rIQC-ro/project-euler-problem-7-in-scala.html" title="Project Euler Problem 7 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2012/01/project-euler-problem-7-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0QGQ3szeCp7ImA9WhRWGUo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-4935993560954671712</id><published>2011-12-31T13:26:00.000-08:00</published><updated>2012-01-07T13:22:02.580-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T13:22:02.580-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="functional" /><category scheme="http://www.blogger.com/atom/ns#" term="euler" /><title>Project Euler Problem 6 in Scala</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;This is 6 in a series. &amp;nbsp;The previous post is at,&amp;nbsp;&lt;/span&gt;&lt;a href="http://benarchie.blogspot.com/2011/12/project-euler-problem-5-in-scala.html" style="background-color: white; color: #336699; line-height: 16px; text-align: left;"&gt;Project Euler Problem 5 in Scala&lt;/a&gt;&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;.&lt;/span&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px; text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;Problem 6 was particularly satisfying for me. &amp;nbsp;You will see why when you see the code below. &amp;nbsp;I think the code deserves some discussion because it takes advantage of some pretty nice Scala features.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;Project Euler problem 6 reads:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;The sum of the squares of the first ten natural numbers is,&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px;"&gt;&amp;nbsp; 12 + 22 + ... + 102 = 385&lt;/span&gt;&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;The square of the sum of the first ten natural numbers is,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif; line-height: 16px;"&gt;(1 + 2 + ... + 10)2 = 552 = 3025&lt;/span&gt;&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is &amp;nbsp; 3025-385 = 2640.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;So reading this question, we want to come up with an algorithm that iterates all the numbers 1..100. &amp;nbsp;During each iteration, we want to capture the sum of the squares, and the sum of the number. &amp;nbsp;Finally, we want to square the sum of the number and subtract the two values. &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;Using some of Scala's syntactic sugar and its excellent functional handling we get:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;object Problem6 {&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;&amp;nbsp; &amp;nbsp; val results = ((0, 0) /: (1 to 100))((i, s) =&amp;gt; {&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; (i._1 + (s * s), i._2 + s)&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;&amp;nbsp; &amp;nbsp; })&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;&amp;nbsp; &amp;nbsp; println((results._2 * results._2) - results._1);&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;
&lt;span style="color: #333333; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;If we take out the object definition and main method call, you can see that this is a two-liner (I could make it a one-liner, but I think that would sacrifice readability). &amp;nbsp;Once again, I am using the foldeLeft operator (/:). &amp;nbsp;This time, however, I am folding left on a sequence of values instead of a single value. &amp;nbsp;Because of this, you see the (0,0) declaration. &amp;nbsp;Scala also infers this type inside the body of the function, so I am storing to values i._1 and i._2. &amp;nbsp;Finally, we square the sums and subtract the sum of the squares.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Note that I actually multiplied results._2 by itself. &amp;nbsp;the '^' operator in Scala seems to work differently than I expected, and was giving me weird results. &amp;nbsp;I might have to look into that, but for now, we have a very succinct solution to this problem.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-4935993560954671712?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/eP0NI5VV6kQcsPXT_p40cgns-_Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eP0NI5VV6kQcsPXT_p40cgns-_Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/eP0NI5VV6kQcsPXT_p40cgns-_Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eP0NI5VV6kQcsPXT_p40cgns-_Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/tnlchH6yrQY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/4935993560954671712/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/12/project-euler-problem-6-in-scala.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/4935993560954671712?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/4935993560954671712?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/tnlchH6yrQY/project-euler-problem-6-in-scala.html" title="Project Euler Problem 6 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/12/project-euler-problem-6-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0MNQ3Y-eyp7ImA9WhRWGUo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-800737761117438857</id><published>2011-12-27T08:04:00.000-08:00</published><updated>2012-01-07T13:24:52.853-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T13:24:52.853-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="functional" /><category scheme="http://www.blogger.com/atom/ns#" term="euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><title>Project Euler Problem 5 in Scala</title><content type="html">&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;This is 4 in a series. &amp;nbsp;The previous post is at,&amp;nbsp;&lt;a href="http://benarchie.blogspot.com/2011/12/project-euler-problem-4-in-scala.html"&gt;Project Euler Problem 4 in Scala&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #333333; line-height: 16px; text-align: left;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;Here we are, arriving at problem 5 from the &lt;/span&gt;&lt;a href="http://projecteuler.net/" style="line-height: 16px;"&gt;Project Euler&lt;/a&gt;&lt;span style="line-height: 16px;"&gt; site. &amp;nbsp;For those of you that may have stumbled in on these posts, and haven't read why I am doing this, you might want to go back to the beginning and find out here. &amp;nbsp;The long and short of it is, I'm using these&amp;nbsp;exercises&amp;nbsp;as a way to solve fun problems while learning a new language (Scala). &amp;nbsp;I've added a twist to try an help me think in a functional way - for each solution, I want my final version of code to have the least amount of mutable state possible.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;As it turns out, this has been a fun and enlightening exercise, and I'm only a few problems in! &amp;nbsp;If you've read any of my previous posts, you know that they were concentrating on turning my Java-like, OO style code into more functional style code without any mutable state. &amp;nbsp;I must be improving slightly, because this post is going to focus more on creating the right algorithm than on functional vs. OO style. &amp;nbsp;Without further&amp;nbsp;adieu, I present to you, Project Euler Problem 5:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="color: #333333;"&gt;&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style="line-height: 16px;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.&lt;br /&gt;What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?&lt;/span&gt;&lt;/span&gt;&lt;/blockquote&gt;
&lt;div style="line-height: 16px;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Well, my first solution, as always, was a brute-force way to solve the problem. &amp;nbsp;I will simply find my 'upper limit' value, which is the product of all the numbers 11 to 20 (A very large number), and iterate all values up to that number, checking if each number has a remainder when divided by any of the numbers 11 to 20. &amp;nbsp;Simple right? &amp;nbsp;It was simple, but it also takes 2 minutes to run on my Lenovo ThinkPad running an Intel Dual Core i7 CPU @ 2.67GHz. &amp;nbsp;Here's the implementation:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif; font-size: small; line-height: 16px;"&gt;
&lt;span style="color: #333333;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;object Problem5 {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; def divisible(number: BigDecimal): Boolean = {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; for (i &amp;lt;- 11 to 20 ) {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if (number.remainder(new BigDecimal(i)).longValue &amp;gt; 0)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return false&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; return true&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;var start = System.currentTimeMillis&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; val upperBound = (new BigDecimal(1) /: (11 to 20))(_ multiply new BigDecimal(_))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; println("Upper bound is " + upperBound)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; var i = 1&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; for (i &amp;lt;- 20 to upperBound.intValue() if divisible(new BigDecimal(i))) {&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;println("Smallest # divisible by 1 .. 20 is " + i);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;println("Run time seconds " + ((System.currentTimeMillis()-start)/1000));&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif; font-size: small;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;After I solved the problem, I started thinking about it more. &amp;nbsp;Iterating through every number seems wasteful, doesn't it? &amp;nbsp;After all, there are a lot of numbers that will not evenly divide by ANY number in that sequence. &amp;nbsp;So, I thought, I should really be iterating through a list of numbers that is the product of at least ONE of the numbers in the sequence. &amp;nbsp;If I selected, 15, for instance, &amp;nbsp;I could iterate through the list: (15, 30, 45, 60...). &amp;nbsp;Of course, If I think about that more, the most efficient number to use will be the highest number in the list, 20. &amp;nbsp;So, I created a new version of the code that iterates in increments of 20, and then checks to see if 11 to 19 are also divisible. &amp;nbsp;This runs on my machine in about 2 seconds, making this about 60 times faster:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif; font-size: small;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;object Problem5 {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; /*Let's skin this cat a different way...*/&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; for (i &amp;lt;- 1 to 320000000) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; val mot = i * 20;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; val divisibleByAll = fitsAll(mot)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if (divisibleByAll) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; println("Smallest is " + mot);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; def fitsAll(number: Int): Boolean = {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; for (i &amp;lt;- 11 to 19) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (number % i &amp;gt; 0)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return false;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return true&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="line-height: 16px;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="line-height: 16px;"&gt;I think I can further improve this code with some of Scala's features. &amp;nbsp;For instance, I think I can iterate the sequence 1 to 320000000 by increments of 20 without needing the variable mot. &amp;nbsp;Notince, though, that I do not have any mutable variables in the code - I declare 2 variables, but they are both val and not var, and so cannot be re-assigned. &amp;nbsp;I don't think this is 'perfect functional thinking', but I suspect, I'm slowly catching on.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style="font-family: Verdana, Arial, sans-serif; font-size: small;"&gt;
&lt;span style="color: #333333;"&gt;&lt;span style="line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-800737761117438857?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/hzTN2EzTbLabQ_R3ypWOn0e3dDM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hzTN2EzTbLabQ_R3ypWOn0e3dDM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/hzTN2EzTbLabQ_R3ypWOn0e3dDM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/hzTN2EzTbLabQ_R3ypWOn0e3dDM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/ifYyT6dLGxE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/800737761117438857/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/12/project-euler-problem-5-in-scala.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/800737761117438857?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/800737761117438857?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/ifYyT6dLGxE/project-euler-problem-5-in-scala.html" title="Project Euler Problem 5 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/12/project-euler-problem-5-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0QCR386fyp7ImA9WhRWGUo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-1706898371161535253</id><published>2011-12-23T12:42:00.000-08:00</published><updated>2012-01-07T13:22:46.117-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T13:22:46.117-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="problem 4" /><title>Project Euler Problem 4 in Scala</title><content type="html">This is 4 in a series. &amp;nbsp;The previous post is at,&amp;nbsp;&lt;a href="http://benarchie.blogspot.com/2011/12/project-euler-problem-3-in-scala.html"&gt;Project Euler Problem 3 in Scala&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
So I've already worked through 3 problems in Euler in hopes of using the exercises to help me learn some Scala. &amp;nbsp;In each of the previous solutions, I had a LOT of mutable state in my code. &amp;nbsp;As I get more comfortable with the language, however, I think I'm improving quite a bit. &amp;nbsp;Even so, it appears I still had a mutable variable in my first solution to this problem. &amp;nbsp;Problem 4 on the Project Euler site reads:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&amp;nbsp;A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 &amp;nbsp;99.&lt;br /&gt;
&amp;nbsp;Find the largest palindrome made from the product of two 3-digit numbers.&lt;/blockquote&gt;
This doesn't seem too difficult to solve. &amp;nbsp;We can cast our integer values to strings, reverse them, and compare them to determine if they are a palindrome, and iterating all the 3 digit numbers, 100-999 is easy. &amp;nbsp;So, here's my first solution:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;object Problem4 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = { &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;var highestPalendrome = 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; for(i &amp;lt;- 100 to 999) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;for(y &amp;lt;- 100 to 999) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;var product = i * y;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;var productStr = product toString;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;if(productStr.reverse equals(productStr)) {&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;println(productStr)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;highestPalendrome = highestPalendrome max product;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; println("Highest is " + highestPalendrome);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;In this solution I iterate the numbers and store the highest palendrome as I go. &amp;nbsp;Of course, the purpose of doing these exercises is to try and code without mutable state, so I had to get rid of that pesky highestPalendrome variable. &amp;nbsp;To do this, I took advantage of the Scala fold left facilities. &amp;nbsp;Turns out, this is a handy way to iterate a list and record results. &amp;nbsp;Here's version 2:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem4 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = { &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;val highestPalendrome = (0 /: (100 to 999))((highest, next)=&amp;gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;(0 /: (100 to 999))((tot2, nex2)=&amp;gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;val product = next*nex2;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;if(product.toString.reverse.equals(product.toString)) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;next*nex2&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;} else {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt; &amp;nbsp;tot2&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}) max highest&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;})&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; println("Highest is " + highestPalendrome);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
You'll notice that this is essentially 1 statement, if you remove the main method and println. &amp;nbsp;You should also notice that I'm using some of Scala's syntactic goodness to make the code easier to read - the end of that statement, 'max highest' would be much uglier in Java - .max(highest). &lt;br /&gt;
&lt;br /&gt;
Well, on to #5. &amp;nbsp;Maybe this time, I won't need a second iteration of the code...&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-1706898371161535253?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zZ5TjOhu7b288eSFdT1Tep59knU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zZ5TjOhu7b288eSFdT1Tep59knU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zZ5TjOhu7b288eSFdT1Tep59knU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zZ5TjOhu7b288eSFdT1Tep59knU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/X3YlW8evnsU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/1706898371161535253/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/12/project-euler-problem-4-in-scala.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1706898371161535253?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1706898371161535253?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/X3YlW8evnsU/project-euler-problem-4-in-scala.html" title="Project Euler Problem 4 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/12/project-euler-problem-4-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0MGQXY6cCp7ImA9WhRWGUo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-368448184948088559</id><published>2011-12-23T12:28:00.000-08:00</published><updated>2012-01-07T13:23:40.818-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T13:23:40.818-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="euler" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="problem 3" /><title>Project Euler Problem 3 in Scala</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;This is 3 in a series. &amp;nbsp;The previous post is at,&amp;nbsp;&lt;a href="http://benarchie.blogspot.com/2011/10/project-euler-problem-2-in-scala.html"&gt;Project Euler Problem 2 in Scala&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;I've been busy again this fall, but now that I have a couple of days off over the holiday, I thought I'd play around with some more Scala. &amp;nbsp;It is pretty slow going learning the language at this pace, but the day job calls...&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Problem 3 on the Euler site states:&lt;/span&gt;&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;The prime factors of 13195 are 5, 7, 13 and 29. &amp;nbsp;What is the largest prime factor of the number 600851475143 ?&lt;/span&gt;&lt;/blockquote&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;For this solution, I reused the isPrime method from an earlier problem. &amp;nbsp;Essentially I end up looping through all the values up to the square root of the number and record the primes. &amp;nbsp;The largest prime is the last one.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Here's my first solution:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem3 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; val number : Long = &amp;nbsp;600851475143L;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = { &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;val sqrt = Math.sqrt(number);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; var largest = 0L;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;var current : Long = 2L;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;while(current &amp;lt; sqrt.toLong) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;if(number % current == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;if(isPrime(current)) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;largest = current;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;current = current + 1L;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;println("Largest " + largest);&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; *//**&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* Brute force method. &amp;nbsp;Perhaps a better method&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* can be implemented here? &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;*//*&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def isPrime(number: Long) : Boolean = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; var half = Math.sqrt(number)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; var current = 2;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; while(current &amp;lt; half) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if(number % current == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return false;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; current = current+1;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; return true;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;After looking at my first solution to this problem, you can see that I have mutable state AGAIN. &amp;nbsp;Something about my Object Oriented brain just can't seem to get into the stateless-state-of-mind. &amp;nbsp;This is interesting for another reason, though. Mutable state, in general, isn't a particularly good thing. &amp;nbsp;That is, if you can solve a programming problem without mutable state, it is better than the solution with mutable state. &amp;nbsp;It shouldn't matter if you are using OO languages or Functional ones. &amp;nbsp;It seems the industries years of OO teachings have led to we engineers sort of ignoring this fact. &amp;nbsp;I guess mutable state isn't as clear in an OO language if you have good Encapsulation in your code, but its still something we should be paying attention to. &amp;nbsp;Luckily, I was able to refactor my solution. &amp;nbsp;Here's a better version:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem3 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; val number : Long = &amp;nbsp;600851475143L;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = { &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;println("Largest is " + largestPrime(2L, Math.sqrt(number), 2L));&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; @tailrec def largestPrime(initialPrime : Long, limit : Double, currentNumber : Long) : Long = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; if(currentNumber &amp;gt;= limit.toLong) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; initialPrime&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; } else {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if(isPrime(currentNumber) &amp;amp;&amp;amp; number % currentNumber == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; largestPrime(currentNumber, limit, &amp;nbsp;currentNumber + 1);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; } else {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; largestPrime(initialPrime, limit, &amp;nbsp;currentNumber + 1);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;/**&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* Brute force method. &amp;nbsp;Perhaps a better method&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;* can be implemented here? &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;*/&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;def isPrime(number: Long) : Boolean = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; var root = Math sqrt number&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; var current = 2;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; while(current &amp;lt; root) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if(number % current == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return false;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; current = current+1;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; return true;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Here we have a new solution where all our mutable state has been removed. &amp;nbsp;I start by declaring the number for which we are trying to find the largest prime. &amp;nbsp;It is declared as a val, though, so we are guaranteed that the value can't change. &amp;nbsp;This is different than any other language I've used, but it seems a handy facility. &amp;nbsp;Next, I call the recursive largestPrime method, which uses Tail recursion to iterate the list. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Of course, I still think there is a better way to determine if a number is prime or not, but this method is pretty efficient the way it is.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;That's my solution for now. &amp;nbsp;Stay tuned for Problem 4....&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-368448184948088559?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/3z01jrr3pHP_JJ8WTGVZSLKm7bo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/3z01jrr3pHP_JJ8WTGVZSLKm7bo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/3z01jrr3pHP_JJ8WTGVZSLKm7bo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/3z01jrr3pHP_JJ8WTGVZSLKm7bo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/bT8MEvBtgcw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/368448184948088559/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/12/project-euler-problem-3-in-scala.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/368448184948088559?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/368448184948088559?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/bT8MEvBtgcw/project-euler-problem-3-in-scala.html" title="Project Euler Problem 3 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/12/project-euler-problem-3-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IFQn0yfCp7ImA9WhRWGUo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-8142238891074770379</id><published>2011-10-21T15:09:00.000-07:00</published><updated>2012-01-07T13:25:13.394-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T13:25:13.394-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><title>Project Euler Problem 2 in Scala</title><content type="html">This is 2 in a series. &amp;nbsp;The Previous post is at, &lt;a href="http://benarchie.blogspot.com/2011/10/project-euler-and-scala-problem-1.html"&gt;Project Euler and Scala Problem 1&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Here is the second problem on the Project Euler(PE) site.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:&lt;br /&gt;
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...&lt;br /&gt;
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.&lt;/blockquote&gt;
&lt;br /&gt;
&lt;br /&gt;
Once again, I was able to solve this pretty easily, but I had lots of mutable state in my code. &amp;nbsp;Here was my first solution:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem2 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;val fourMillion : Integer = 4000000;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;var first : Integer = 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;var second : Integer = 1;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;def nextFib() : Integer = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;var returnValue:Integer = prev1+prev2;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return returnValue;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;var sum : Long = 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;def main(args: Array[String]): Unit = {&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;var next:Integer = 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;while( (next &amp;lt;= fourMillion) ) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;next = nextFib();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;if(next % 2 == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;sum = sum + next;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;print(sum)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;You will see that I have 3 mutable variables in there - &amp;nbsp;first, second, and sum. &amp;nbsp;I also have a rather ugly while loop. &amp;nbsp;I'd like to get rid of the iteration here and use recursion instead. &amp;nbsp;While doing so, I can also get rid of the mutable state. &amp;nbsp;Here is the rewritten code:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem2 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;//Tail recursive...&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;def fibList(currentList : Array[Int], limit : Int, previous : Int) : Array[Int] = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;if(previous &amp;gt; limit) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="background-color: transparent; font-family: 'Courier New', Courier, monospace;"&gt;return currentList;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: transparent; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;return fibList((currentList :+ previous), limit, (currentList.last + previous));&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;def main(args: Array[String]): Unit = {&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;print((0 /: fibList(Array[Int](0,1), 4000000, 1))((total, next)=&amp;gt; {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(next % 2 == 0) {&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;next + total&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; } else {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt; &amp;nbsp;total&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; &amp;nbsp; } &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt; }))&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;I hope you notice the comment in there - Tail Recursive. &amp;nbsp;This is a very interesting feature of Scala, and most other functional languages. &amp;nbsp;You see, the big concern in Java when using recursion is that you run out of stack space for all your recursions. &amp;nbsp;In Scala, the compiler can optimize this code and get rid of the stack if you don't need to keep state in your method. &amp;nbsp;I've carefully constructed the fibList here so that it can be optimized. &amp;nbsp;This way, we can't fill up the stack. &amp;nbsp;go ahead, change 4000000 to 4000000000. &amp;nbsp;While you may overflow the int values, the recursive function won't blow up the memory. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;This feature of the language is really quite interesting, and from my understanding not a small feat to pull off given the current bytecode standard. &amp;nbsp;This could allow for some very long-running calculations that might otherwise have caused memory problems if done other ways. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: transparent; font-family: inherit;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: transparent; font-family: inherit;"&gt;That's all for this problem. &amp;nbsp;On to #3... &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: inherit;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-8142238891074770379?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/HI0e_kcALBJ-NA_r4Y7skWO_dKo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HI0e_kcALBJ-NA_r4Y7skWO_dKo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/HI0e_kcALBJ-NA_r4Y7skWO_dKo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HI0e_kcALBJ-NA_r4Y7skWO_dKo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/SsUMuIk6OTM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/8142238891074770379/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/10/project-euler-problem-2-in-scala.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/8142238891074770379?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/8142238891074770379?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/SsUMuIk6OTM/project-euler-problem-2-in-scala.html" title="Project Euler Problem 2 in Scala" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/10/project-euler-problem-2-in-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IASHw5eCp7ImA9WhRWGUo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-9162144704694222146</id><published>2011-10-15T08:56:00.000-07:00</published><updated>2012-01-07T13:25:49.220-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T13:25:49.220-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="project euler" /><category scheme="http://www.blogger.com/atom/ns#" term="functional" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><title>Project Euler and Scala problem 1</title><content type="html">&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;I recently found a Web site called, &lt;a href="http://www.projecteuler.net/"&gt;Project Euler&lt;/a&gt;&amp;nbsp;(PE). &amp;nbsp;Its a site full of problems that are intended to be solved by coding. &amp;nbsp;As I am also experimenting with&lt;a href="http://www.scala-lang.org/"&gt; Scala&lt;/a&gt;, I figured this would be a fun way to keep my skills sharp solving problems while learning the nuances of another language. &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;What I am finding is very interesting. &amp;nbsp;First, my 'functional' skills need work. &amp;nbsp;I'm attempting to solve each problem with no mutable state, but as you will see, my first iteration usually has some sort of mutable state and it takes me a little bit to refactor it. &amp;nbsp;Part of this is due to the fact that I spend my day job programming in an OO language, part of it is due to learning Scala, but part of it is just that I need more practice in the functional way of thinking. &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;I am fortunate in that I have two friends who are also interested in PE and who have started working through the problems as well, so we swap notes after we solve the solutions, which further helps understanding. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;I thought I would blog about the problems as I work through them. &amp;nbsp;The format I have settled on this this:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style="background-color: transparent; font-family: Arial, Helvetica, sans-serif;"&gt;I will present my first, rough, solution to the problem, along with a brief summary of the PEdocumentation.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background-color: transparent; font-family: Arial, Helvetica, sans-serif;"&gt;I will present a refactored version of the same problem that makes the code much more functional and maintainable.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background-color: transparent; font-family: Arial, Helvetica, sans-serif;"&gt;If the PE documentation reveals a math or programming concept that would improve the code, another version of the algorithm may be posted.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;span style="background-color: transparent; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;span style="background-color: transparent; font-family: Arial, Helvetica, sans-serif;"&gt;It's my hope that presenting my progress in this fashion is both interesting and revealing for other programmers. I work primarily in Java, so this might also be a good way for Java programmers to see how things translate to Scala. &amp;nbsp;Of course I am not a Scala expert, so I'm sure there will be lots of comments with room for improvement. &amp;nbsp;At least, that's my hope.&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: transparent; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
Here is PE's first problem:&lt;/span&gt;&lt;br /&gt;
&lt;div style="background-color: white;"&gt;
&lt;blockquote&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.&lt;/span&gt;&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&lt;/span&gt;&lt;/blockquote&gt;
&lt;/div&gt;
&lt;div style="background-color: white;"&gt;
&lt;span style="background-color: white; font-family: Arial, Helvetica, sans-serif;"&gt;Well, this seems simple enough. &amp;nbsp;Simply iterate all the numbers to 1000, testing if they are divisible by 3 or 5 along the way. &amp;nbsp;The first version of my code looks like this:&lt;/span&gt;&lt;/div&gt;
&lt;div style="background-color: white;"&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="background-color: white;"&gt;
&lt;/div&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem1 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = { &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; var sum = 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; for( i &amp;lt;- Iterator.range(0,1000) if i % 3 == 0 || i % 5 == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; sum += i;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; print(sum);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;There's nothing inherently wrong with the code above. &amp;nbsp;Except I have that mutable variable, sum, &amp;nbsp;in there. &amp;nbsp;So, what's a more functional way to think about this? &amp;nbsp;Thanks to the inspiration from my co-worker, Sean, we have a more functional version of the code above:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem1 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; val count = (1 to 999);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; val sum = (0 /: count)((total, next) =&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (next % 3 == 0 || next % 5 == 0) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; total + next;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } else {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; total;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; });&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; println(sum);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;In this version, we've made the sum immutable (by using val instead of var), so it can only be assigned once. &amp;nbsp;We've also used the left-fold operator, \:, to iterate the range. &amp;nbsp;Finally, we've converted the contents of the for loop to a function block that gets executed on each 'fold'. &amp;nbsp;This version looks pretty good, but after reviewing the PE docs, we pick up another little math trick. &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;Instead of iterating the numbers, we can take the sum of all the multiples of 3, sum of all the multiples of 5 and then subtract all the multiples of 15. &amp;nbsp;There's also a proof that demonstrates how to find the sum (I won't reprint it here, if you are interested, unlock the problem on the PE site).&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;The final version of our code looks like:&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;object Problem1 {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def main(args: Array[String]): Unit = {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; println(sumDivisibleBy(999,3) + sumDivisibleBy(999, 5) - sumDivisibleBy(999, 15));&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; def sumDivisibleBy(limit:Int, divisor:Int) : Int = &amp;nbsp;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;val p = limit/divisor;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;divisor * (p*(p+1))/2;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;So in the end, we don't iterate the range at all. &amp;nbsp;This solution would also scale well (what if we wanted to add all the multiples of 3 and five for all numbers under 1 million...)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, Helvetica, sans-serif;"&gt;So there it is. A brief walk through my process on Project Euler problem 1. Let me know what you think.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-9162144704694222146?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/U-HZeVEevU1Vl8fzOO4fMiMFz14/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/U-HZeVEevU1Vl8fzOO4fMiMFz14/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/U-HZeVEevU1Vl8fzOO4fMiMFz14/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/U-HZeVEevU1Vl8fzOO4fMiMFz14/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/kLHjgfSeneg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/9162144704694222146/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/10/project-euler-and-scala-problem-1.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/9162144704694222146?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/9162144704694222146?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/kLHjgfSeneg/project-euler-and-scala-problem-1.html" title="Project Euler and Scala problem 1" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/10/project-euler-and-scala-problem-1.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUMBSXg9eCp7ImA9WhdTGUw.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-600351608078988855</id><published>2011-07-17T08:44:00.000-07:00</published><updated>2011-07-17T08:44:18.660-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-17T08:44:18.660-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="GWT" /><category scheme="http://www.blogger.com/atom/ns#" term="signature" /><category scheme="http://www.blogger.com/atom/ns#" term="type" /><category scheme="http://www.blogger.com/atom/ns#" term="Java Native Interface" /><category scheme="http://www.blogger.com/atom/ns#" term="code" /><category scheme="http://www.blogger.com/atom/ns#" term="Javascript Native Interface" /><category scheme="http://www.blogger.com/atom/ns#" term="JSNI" /><category scheme="http://www.blogger.com/atom/ns#" term="JNI" /><title>GWT JSNI Reference</title><content type="html">I've recently had to go through the GWT JSNI interface, and I found the documentation a little lacking. &amp;nbsp;While the official JSNI page has nice examples and clearly shows how things integrate, it leaves out some reference information that would be helpful. &amp;nbsp;My guess is that they didn't want to duplicate the JNI reference, but its still a little frustrating to have to look up the type codes in a separate area. &amp;nbsp;And the type codes aren't exactly intuitive, I mean, who would guess that 'Z' is the representation for boolean?&lt;br /&gt;
&lt;br /&gt;
So, here is a little cheat sheet for those of you looking for the GWT JSNI type codes. &amp;nbsp;These are, of course, the same codes that can be found in the JNI reference. &lt;br /&gt;
&lt;br /&gt;
&lt;table class="wikitable"&gt;&lt;tbody&gt;
&lt;tr&gt; &lt;th&gt;Native Type&lt;/th&gt; &lt;th&gt;Java Language Type&lt;/th&gt; &lt;th&gt;Description&lt;/th&gt; &lt;th&gt;Type signature&lt;/th&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;unsigned char&lt;/td&gt; &lt;td&gt;jboolean&lt;/td&gt; &lt;td&gt;unsigned 8 bits&lt;/td&gt; &lt;td&gt;Z&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;signed char&lt;/td&gt; &lt;td&gt;jbyte&lt;/td&gt; &lt;td&gt;signed 8 bits&lt;/td&gt; &lt;td&gt;B&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;unsigned short&lt;/td&gt; &lt;td&gt;jchar&lt;/td&gt; &lt;td&gt;unsigned 16 bits&lt;/td&gt; &lt;td&gt;C&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;short&lt;/td&gt; &lt;td&gt;jshort&lt;/td&gt; &lt;td&gt;signed 16 bits&lt;/td&gt; &lt;td&gt;S&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;long&lt;/td&gt; &lt;td&gt;jint&lt;/td&gt; &lt;td&gt;signed 32 bits&lt;/td&gt; &lt;td&gt;I&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;&lt;br /&gt;
long long&lt;br /&gt;
&lt;br /&gt;
__int64&lt;/td&gt; &lt;td&gt;jlong&lt;/td&gt; &lt;td&gt;signed 64 bits&lt;/td&gt; &lt;td&gt;J&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;float&lt;/td&gt; &lt;td&gt;jfloat&lt;/td&gt; &lt;td&gt;32 bits&lt;/td&gt; &lt;td&gt;F&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;double&lt;/td&gt; &lt;td&gt;jdouble&lt;/td&gt; &lt;td&gt;64 bits&lt;/td&gt; &lt;td&gt;D&lt;/td&gt; &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
Table taken from&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Java_Native_Interface"&gt;Wikipedia&lt;/a&gt;, 5/7/2011&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In addition to the primitive types, any object can be referenced using its fully qualified name like this:&lt;br /&gt;
&lt;br /&gt;
LFully.Qualified.Name;&lt;br /&gt;
&lt;br /&gt;
For example, to access a string, you use: &amp;nbsp;Ljava.lang.String;&lt;br /&gt;
&lt;br /&gt;
To reference an array of the same type, prepend [ like this:&lt;br /&gt;
&lt;br /&gt;
[Ljava.langString;&lt;br /&gt;
&lt;br /&gt;
So that's my quick cheat sheet. &amp;nbsp;Here are a couple of additional references that help:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html"&gt;Coding Basics - Javascript Native Interface (JSNI)&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class="webkit-line-content"&gt;&lt;a href="http://download.oracle.com/javase/6/docs/technotes/guides/jni/"&gt;JDK 6 Java Native Interface-related APIs &amp;amp; Developer Guides&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-600351608078988855?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/x2aQTVvFGPst74G3lQ3BNlbCeVs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/x2aQTVvFGPst74G3lQ3BNlbCeVs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/x2aQTVvFGPst74G3lQ3BNlbCeVs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/x2aQTVvFGPst74G3lQ3BNlbCeVs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/YQKHi9C1ZYQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/600351608078988855/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/07/gwt-jsni-reference.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/600351608078988855?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/600351608078988855?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/YQKHi9C1ZYQ/gwt-jsni-reference.html" title="GWT JSNI Reference" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/07/gwt-jsni-reference.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUQEQHcyfCp7ImA9WhZXEE4.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-1460387202439583881</id><published>2011-04-28T17:35:00.000-07:00</published><updated>2011-04-28T17:35:01.994-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-28T17:35:01.994-07:00</app:edited><title>Master of Software Engineering</title><content type="html">Finally, I am an MSE.  That's, Master of Software Engineering. I just finished presenting my capstone project at &lt;a href="http://www.carrollu.edu/"&gt;Carroll University&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
A lot of people don't know what Software Engineering is.  Is it related to Development?  Is it really an Engineering discipline?  I'd like to take this time to talk a little bit about what I think a Software Engineer is.  &lt;br /&gt;
&lt;br /&gt;
Let's start with the official definition of 'Software Engineer'.  Any search these days starts with Wikipedia, and in fact, Wikipedia has a nice listing for 'Software Engineering', with lots of references.  Here is the first paragraph:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Software engineering (SE) is a profession dedicated to designing, implementing, and modifying software so that it is of higher quality, more affordable, maintainable, and faster to build. It is a "systematic approach to the analysis, design, assessment, implementation, test, maintenance and re-engineering of a software by applying engineering to the software".[1] The term software engineering first appeared in the 1968 NATO Software Engineering Conference, and was meant to provoke thought regarding the perceived "software crisis" at the time.[2][3] Since the field is still relatively young compared to its sister fields of engineering, there is still much debate around what software engineering actually is, and if it conforms to the classical definition of engineering.[4] The IEEE Computer Society's Software Engineering Body of Knowledge defines "software engineering" as the application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software, and the study of these approaches; that is, the application of engineering to software.[5] It is the application of Engineering to software because it integrates significant mathematics, computer science and practices whose origins are in Engineering.[6]&lt;/blockquote&gt;Wikipedia, Dec 24, 2010&lt;br /&gt;
&lt;br /&gt;
If I were to put that rather lengthy description into my own short words, it would be: Software Engineering is a way to build better software by studying and applying a systematic, disciplined and quantifiable approach to the development process. &lt;br /&gt;
&lt;br /&gt;
You see, to me, the key characteristic is that while Software Engineering includes software development, it really goes beyond just writing code or applications. &amp;nbsp;It is the study and practice of the processes that will make writing applications better. &amp;nbsp;Better may not mean faster or cheaper but will always mean higher quality. Although, sometimes, it will mean faster and cheaper, but usually in the long run. &amp;nbsp;So Software Engineering really expands into all the different tasks surrounding a system - business definition, requirements gathering, analysis, design, testing, and support. &amp;nbsp;A Software engineer should know about all these phases in the life cycle and should be involved with all of them. &lt;br /&gt;
&lt;br /&gt;
But developing software is not exactly akin to designing a bridge or a car. &amp;nbsp;There's still a component of craftsmanship or artistry involved with software development. Methodologies can be used to improve estimates, quality, and outcomes, but there is also a large component that is more akin to writing a novel or creating a movie than to building a bridge or designing a car.&lt;br /&gt;
&lt;br /&gt;
It is in that part of the software development process where the &lt;i&gt;art &lt;/i&gt;of development presents itself. &amp;nbsp;Writing software is a creative and abstract task with many different ways to solve a problem. &amp;nbsp;In some cases, a single, best, solution can be found, but in so many others, there isn't one. &amp;nbsp;In that regard, some developers are artists, some developers really aren’t, and never will be.  The rest of us are aspiring to be one of the great ones.  We are competent technicians, writing our works, but not gifted with the ability to produce masterpieces. &amp;nbsp;Here is where what we learn and the techniques we apply from Software Engineering Study allow us to produce pieces of software that are in fact, solid and elegant. &amp;nbsp;We won't be writing the next Mona Lisa, but by following proper techniques, using the best practices, studying what works, and applying them consistently and rigorously, we can produce consistent, appealing work with high levels of quality. &amp;nbsp;If we work hard, people might one day also recognize us as an artist. &lt;br /&gt;
&lt;br /&gt;
Perhaps not a Michelangelo, but maybe a Bob Ross.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-1460387202439583881?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/9ROceatkxkFQxPqa49Uj9q8RIZs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9ROceatkxkFQxPqa49Uj9q8RIZs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/9ROceatkxkFQxPqa49Uj9q8RIZs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9ROceatkxkFQxPqa49Uj9q8RIZs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/U4qflqv9ihY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/1460387202439583881/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/04/master-of-software-engineering.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1460387202439583881?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1460387202439583881?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/U4qflqv9ihY/master-of-software-engineering.html" title="Master of Software Engineering" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/04/master-of-software-engineering.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DE8FQXg-fSp7ImA9WhZQFks.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-5161861258636875308</id><published>2011-04-24T10:40:00.000-07:00</published><updated>2011-04-24T10:40:10.655-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-24T10:40:10.655-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="development" /><category scheme="http://www.blogger.com/atom/ns#" term="swing" /><category scheme="http://www.blogger.com/atom/ns#" term="XML" /><title>My First Scala App</title><content type="html">I've been looking at learning another development language for some time. Over the years I've dabbled in PHP, Groovy, and even C/C++.  While I learned a lot in doing this, I never became an 'expert' in any of them.  For the most part, I used them for the project in which they were needed  or for the scenario where they were most appropriate and then went back to Java for my primary development platform.  &lt;br /&gt;
&lt;br /&gt;
The reason is that I'd become so familiar with the Java tool set, language, and available libraries.  I was (and am) most productive in that environment.  But Java is showing its age and limitations.  Its becoming clear that some of the most vexing problems facing developers will need a full range of tools to solve them, and while Java's OO support is good, its functional support is not.  I, therefore, decided that I needed to expand and really learn another language pretty well.  I decided that language is going to be Scala.&lt;br /&gt;
&lt;br /&gt;
Why Scala? Well, I'll go over that in a different post. This post is about my experience writing my first Scala app.  So, here it goes.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The Application&lt;/b&gt;&lt;br /&gt;
Since I am changing jobs (again), I decided I would leave something behind for my co-workers. &amp;nbsp;In our current work environment, we have a shared drive where server error logs are stored in 'pseudo' XML format. &amp;nbsp;It was always annoying to me that we had to navigate the file structure, open a text file, and hunt through the file to find an error we would be looking for. &amp;nbsp;It was also difficult to determine how often an error was&amp;nbsp;occurring. &amp;nbsp;Now, the easy way to solve this is to use the log4j XML appender and read the files with Apache Chainsaw, but this is a large company, and it decided to go with a proprietary format. &amp;nbsp;I decided to use Scala to write a little desktop app that the developers could use to view the log files in a tabular format, then select a row to see the stacktrace detail. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The Functionality&lt;/b&gt;&lt;br /&gt;
The application needed 3 essential things : a table to show rows, a TextArea to show the detail, and a FileChooser to select which log to view. &amp;nbsp;In the background it also needed the following processing capabilities: &amp;nbsp;read in a file, modify the text to make it valid XML, parse the XML tags. &amp;nbsp;That's about it.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The Results&lt;/b&gt;&lt;br /&gt;
Well, I whipped out the app pretty quickly. &amp;nbsp;It would have been much more quickly if I hadn't been learning Scala along the way. &amp;nbsp;There were some really nice things about Scala that I found out. &amp;nbsp;First, its swing wrapper classes are nice, and their pub/sub model is succinct. &amp;nbsp;Second, the XML processing is excellent. &amp;nbsp;The built in support for XML files allowed me to parse the logs in a couple of lines of code. &amp;nbsp;Finally, the Actor metaphor for multi threading was also very easy to use and implement. &lt;br /&gt;
&lt;br /&gt;
I had a few frustrations along the way as well. &amp;nbsp;The Eclipse plugin was nice, but it still doesn't have code-complete (One of the biggest productivity tools in my mind). &amp;nbsp;The code-complete is supposed to be in the plugin, but for some reason, doesn't seem to work for me. &amp;nbsp;I've tried on 3 separate computers. This meant that I was reading a lot of documentation. &amp;nbsp;Its also hard to read the documentation. &amp;nbsp;Since Scala has so much support for so many features, the docs can get a little complicated to read. &amp;nbsp;I'm sure as I become more familiar with the libraries and syntax this will get easier, but I can see how it would be daunting for a junior developer.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Conclusion&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
I'm by no means a Scala expert at this point, but I enjoyed the language enough, and it seems to have enough benefits that I'm looking forward to learning more. &amp;nbsp;Who knows, maybe one day Scala will be my primary language...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-5161861258636875308?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/sKOef5s0JU4YJqOU_ZS6ZtFEJMQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sKOef5s0JU4YJqOU_ZS6ZtFEJMQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/sKOef5s0JU4YJqOU_ZS6ZtFEJMQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sKOef5s0JU4YJqOU_ZS6ZtFEJMQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/x1RnOWKA0fg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/5161861258636875308/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/04/my-first-scala-app.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/5161861258636875308?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/5161861258636875308?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/x1RnOWKA0fg/my-first-scala-app.html" title="My First Scala App" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/04/my-first-scala-app.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUYFSHo9eSp7ImA9Wx9VGUs.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-4879323241408036783</id><published>2011-02-05T19:31:00.000-08:00</published><updated>2011-02-05T19:31:59.461-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-05T19:31:59.461-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="GWT" /><category scheme="http://www.blogger.com/atom/ns#" term="JNI" /><category scheme="http://www.blogger.com/atom/ns#" term="SmartGWT" /><title>GWT and SmartGWT - JNI and Unsatisfied Link Error</title><content type="html">GWT uses the Java JNI mechanism in a really unique way. &amp;nbsp;JNI was originally intended to work with native code on the computer where you had your JDK, like C++. &amp;nbsp;I've had limited exposure to the JNI interface - I've only had to use it for a serial communication component. &amp;nbsp;Even then, I was using a library that relied on JNI, so I didn't generate the code, I just had to make sure the native library was on my classpath.&lt;br /&gt;
&lt;br /&gt;
The Google Engineers applied this mechanism to Javascript, however. &amp;nbsp;If you want to write 'Native' Javascript in your GWT classes, its easy. &amp;nbsp;Just flag your method as native and put your javascript code, in comments, before your terminator. &amp;nbsp;Here's a sample from the &lt;a href="http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#writing"&gt;Google Web site&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Helvetica, Arial, sans-serif; font-size: x-small;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class="prettyprint" style="background-color: #fafafa; border-bottom-color: rgb(187, 187, 187); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(187, 187, 187); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(187, 187, 187); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(187, 187, 187); border-top-style: solid; border-top-width: 1px; color: #007000; font-family: monospace; font-size: 9pt; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 1em; overflow-x: visible; overflow-y: visible; padding-bottom: 0.99em; padding-left: 0.99em; padding-right: 0.99em; padding-top: 0.99em; white-space: pre-wrap; word-wrap: break-word;"&gt;&lt;span class="kwd" style="color: #000088;"&gt;public&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="kwd" style="color: #000088;"&gt;static&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="kwd" style="color: #000088;"&gt;native&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="kwd" style="color: #000088;"&gt;void&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; alert&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;(&lt;/span&gt;&lt;span class="typ" style="color: #660066;"&gt;String&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; msg&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;)&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="com" style="color: #880000;"&gt;/*-{
&amp;nbsp; $wnd.alert(msg);
}-*/&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Viola! &amp;nbsp;Instant native interface for your JavaScript CLIENT code. &amp;nbsp;That's right, just try running this method on the server. &amp;nbsp;While working on my current project, I ran into this issue accidently while trying to use a SmartGWT class. &amp;nbsp;I got a JNI error on the server because the VM couldn't find the native code. &amp;nbsp;Of course, there isn't any native code on the server. &amp;nbsp;In this case, the 'native' code is JavaScript.&lt;br /&gt;
&lt;br /&gt;
I was trying to pass a SmartGWT ListGridRecord to the server so I could map it to a persistent bean. &amp;nbsp;Of course, once I saw the error, I followed the inheritance hierarchy for ListGridRecord all the way up the chain, and what did I find at the top? &amp;nbsp;JsObject. &amp;nbsp;Of course, I didn't even have to go that far. &amp;nbsp;After looking at the Record code, it relies on static methods in JSOHelper, which uses a lot of native JavaScript&lt;br /&gt;
&lt;br /&gt;
In the end I had make transfer objects to use to send the data over GWT RPC. This was a good reminder, though, that while GWT looks like Java, what's being executed really isn't.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-4879323241408036783?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/CCMmTSubZ4xXRgUgmNGqrVcgXco/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CCMmTSubZ4xXRgUgmNGqrVcgXco/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/CCMmTSubZ4xXRgUgmNGqrVcgXco/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/CCMmTSubZ4xXRgUgmNGqrVcgXco/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/00ZHcsXmSc4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/4879323241408036783/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/02/gwt-and-smartgwt-jni-and-unsatisfied.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/4879323241408036783?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/4879323241408036783?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/00ZHcsXmSc4/gwt-and-smartgwt-jni-and-unsatisfied.html" title="GWT and SmartGWT - JNI and Unsatisfied Link Error" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/02/gwt-and-smartgwt-jni-and-unsatisfied.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkAFQX44cCp7ImA9Wx9VFE0.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-6567558202059934315</id><published>2011-01-30T08:25:00.000-08:00</published><updated>2011-01-30T08:25:10.038-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-30T08:25:10.038-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="DataSource" /><category scheme="http://www.blogger.com/atom/ns#" term="GWT" /><category scheme="http://www.blogger.com/atom/ns#" term="GwtRpcDataSource" /><category scheme="http://www.blogger.com/atom/ns#" term="GWT RPC" /><category scheme="http://www.blogger.com/atom/ns#" term="SmartGWT" /><title>SmartGWT and GWT RPC Integration</title><content type="html">I am currently finishing my Capstone project for my &lt;a href="http://www.carrollu.edu/gradprograms/softwareengineering/"&gt;MSE at Carroll University&lt;/a&gt;. While the application I am writing itself doesn't have a lot of twists in it, I decided to use this&amp;nbsp;opportunity&amp;nbsp;to experiment with some different technologies. &amp;nbsp;I'm using GWT, SmartGWT, and Hibernate in the application. &amp;nbsp;I've used Hibernate and GWT pretty extensively in my professional career, but the twists I've added for this development are to configure Hibernate using the JPA standard, and to use GWT Designer for mockups. &amp;nbsp;I've already posted about my experiences with &lt;a href="http://benarchie.blogspot.com/2010/12/gwt-designer.html"&gt;GWT Designer&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
I haven't really used SmartGWT before. &amp;nbsp;My experience has been with the&lt;a href="http://www.sencha.com/products/gwt/"&gt; ExtGWT&lt;/a&gt; library, which I loved. &amp;nbsp;I chose to use &lt;a href="http://code.google.com/p/smartgwt/"&gt;SmartGWT&lt;/a&gt; on this build because it is fully LGPL, while Ext is dual licensed. &amp;nbsp;While working with SmartGWT I've found some things that I was not expecting. &lt;br /&gt;
&lt;br /&gt;
One of the selling points of SmartGWT is that it has built in support for 'Data Binding' with Tables and the like. &amp;nbsp;This is done through DataSource objects. &amp;nbsp;The problem is, the published DataSource implementations don't work with GWT RPC. And the DataSource base class has dozens of methods (Just take a look at the&lt;a href="http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/data/DataSource.html"&gt; JavaDoc&lt;/a&gt;). &amp;nbsp;It really isn't clear how to define your own custom data source, or the work it will take to do so. &amp;nbsp;This was a sticking point for me because I really like the GWT RPC mechanism, and I had already come up with a nice security model, which I posted about&lt;a href="http://benarchie.blogspot.com/2010/12/gwt-simple-security-configuration.html"&gt; here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
It took a while, but I finally found a post about integrating GWT RPC on the &lt;a href="http://forums.smartclient.com/showthread.php?t=4814"&gt;SmartClient Forums.&lt;/a&gt;&amp;nbsp;&amp;nbsp;The post is a bit dated and says you need to compile from source, but that is not the case. &amp;nbsp;The version of SmartGWT I am using, 2.4 already has the clientCustom flag built in. &amp;nbsp;The developer who posted this also supplied 5 files, Including a GwtRpcDataSource abstract class, and a sample implementation. &amp;nbsp;The class comments well what you need to do to implement your datasource. With this setup, a new GWT data source can be defined by implementing 4 methods. &amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
I hope this post helps others find the solution much faster than I was able to.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-6567558202059934315?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/xeNXo2dISQ12D6tn7b3vPhxzgDU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xeNXo2dISQ12D6tn7b3vPhxzgDU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/xeNXo2dISQ12D6tn7b3vPhxzgDU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xeNXo2dISQ12D6tn7b3vPhxzgDU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/T39gogAx3NY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/6567558202059934315/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/01/smartgwt-and-gwt-rpc-integration.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/6567558202059934315?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/6567558202059934315?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/T39gogAx3NY/smartgwt-and-gwt-rpc-integration.html" title="SmartGWT and GWT RPC Integration" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/01/smartgwt-and-gwt-rpc-integration.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0cAQ3c9eSp7ImA9Wx9XFUw.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-9080973188375845740</id><published>2011-01-08T04:40:00.000-08:00</published><updated>2011-01-08T12:37:22.961-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-08T12:37:22.961-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dynaTrace" /><category scheme="http://www.blogger.com/atom/ns#" term="instrument" /><category scheme="http://www.blogger.com/atom/ns#" term="code" /><category scheme="http://www.blogger.com/atom/ns#" term="Spring" /><category scheme="http://www.blogger.com/atom/ns#" term="Struts" /><title>dynaTrace Enlightenment</title><content type="html">My company recently sent me for dynaTrace training. &amp;nbsp;dynaTrace is a tool that allows you to instrument your code in real time in order to measure and monitor performance. &amp;nbsp;I'm not going to go into detail about what it can do, you can read all about it on the&amp;nbsp;&lt;a href="http://www.dynatrace.com/"&gt;dynaTrace&amp;nbsp;websit&lt;/a&gt;&lt;a href="http://www.dynatrace.com/"&gt;e&lt;/a&gt;. &amp;nbsp;If your company has the money, however, I highly recommend it. &amp;nbsp;It is impressive to say the least.&lt;br /&gt;
&lt;br /&gt;
One of the things dynaTrace offers is the ability to see all the exceptions being thrown in your application, not just the the ones being&amp;nbsp;propagated&amp;nbsp;to your code. &amp;nbsp;This is important in our applications because we use third-party libraries, and we don't always know what they are doing. &amp;nbsp;In instrumenting the application we are working on, we found that Spring was throwing hundreds of thousands of exceptions and burying them. &amp;nbsp;Now each one of these is small in cost, and the application works as expected. &amp;nbsp;The cost of the exceptions, however, was starting to get expensive - one, two or even a few hundred probably wouldn't be cause for concern, but hundreds of thousands? &lt;br /&gt;
&lt;br /&gt;
So What was the cause?&lt;br /&gt;
&lt;br /&gt;
This application is also a Web application that uses Struts2. &amp;nbsp;The application was set to use annotation-based configuration. &amp;nbsp;But when we looked at the Exceptions, it appeared that Spring was ignoring the configuration and attempting to configure itself using other methods, throwing exceptions, and then trying the next one. Multiple exceptions occurred every time the application accessed a Spring managed bean.&lt;br /&gt;
&lt;br /&gt;
As it turns out, there is another flag in Struts 2 to set that says, "No we really, really want to use this configuration." &amp;nbsp;Once that flag was set, the exceptions disappeared.&lt;br /&gt;
&lt;br /&gt;
So What's the Point?&lt;br /&gt;
&lt;br /&gt;
In todays development environment, we are heavily reliant on 3rd party libraries in order to leverage previous code and get things done faster. &amp;nbsp;Many applications use dozens of libraries from many different sources. &amp;nbsp;In some ways, this is a necessary evil. &amp;nbsp;Could you imagine the time it would take to write a Web application completely from scratch? &amp;nbsp;I mean completely, no application container, no MVC, nothing. &amp;nbsp;Start with the sockets to listen to requests. &amp;nbsp;So, as engineers, we decide to use a component created from another source that matches the criteria we need. &amp;nbsp;We use application containers, M-V-C frameworks, JavaScript toolkits, Persistence mappers, Logging libraries.... The list goes on and on. &lt;br /&gt;
&lt;br /&gt;
While all of this is generally a good thing, sometimes its not because we don't truly know what's going on in our black boxes. &amp;nbsp;I think tools like dynaTrace are going to become more popular as companies look to improve quality while still enjoying the advantage of third party software. &lt;br /&gt;
&lt;br /&gt;
That's it for now, it's time to go figure out why the Struts taglibs are adding 500 ms to our page rendering times....&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-9080973188375845740?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/cFCJGmindHi1nyIQ7zBpzU-7tsk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cFCJGmindHi1nyIQ7zBpzU-7tsk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/cFCJGmindHi1nyIQ7zBpzU-7tsk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cFCJGmindHi1nyIQ7zBpzU-7tsk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/qMa-WiUQvJM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/9080973188375845740/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2011/01/dynatrace-enlightenment.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/9080973188375845740?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/9080973188375845740?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/qMa-WiUQvJM/dynatrace-enlightenment.html" title="dynaTrace Enlightenment" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2011/01/dynatrace-enlightenment.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQGR3szfip7ImA9Wx9QGE0.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-7074452510886648746</id><published>2010-12-31T07:12:00.000-08:00</published><updated>2010-12-31T07:12:06.586-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-31T07:12:06.586-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="GWT" /><category scheme="http://www.blogger.com/atom/ns#" term="GWT Designer" /><category scheme="http://www.blogger.com/atom/ns#" term="ExtGWT" /><category scheme="http://www.blogger.com/atom/ns#" term="WYSIWYG" /><category scheme="http://www.blogger.com/atom/ns#" term="SmartGWT" /><title>GWT Designer</title><content type="html">I am not a fan of WYSIWYG software. &amp;nbsp;Almost every experience I had with WYSIWYG tools had the same pattern:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;I implement some application using the tool because I am pressed for time.&lt;/li&gt;
&lt;li&gt;I get the initial project done very quickly.&lt;/li&gt;
&lt;li&gt;Someone requests a change.&lt;/li&gt;
&lt;li&gt;Because of the WYSIWYG generated code, I essentially have to rewrite the app. &lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;Maybe I just didn't use the WYSIWYG right. &amp;nbsp;Obviously, a lot of people love them. &amp;nbsp;But they were always counter-productive for me. &amp;nbsp;In the end, they cost me more time than they saved. &amp;nbsp;So, you can imagine my feelings when I read about GWT Designer. &amp;nbsp;Here was a Drag and drop visual editor for &lt;a href="http://code.google.com/webtoolkit/"&gt;GWT&lt;/a&gt;, complete with integration for &lt;a href="http://code.google.com/p/smartgwt/"&gt;SmartGWT&lt;/a&gt; and &lt;a href="http://www.sencha.com/products/gwt/"&gt;Ext GWT&lt;/a&gt;. &amp;nbsp;I was hesitant, to say the least.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;But I also firmly believe that as a Software Engineer, you have to keep your mind open and constantly re-evaluate new tools. &amp;nbsp;So, I installed the &lt;a href="http://code.google.com/webtoolkit/tools/download-gwtdesigner.html"&gt;GWT Designer&lt;/a&gt; plugin for Eclipse. &amp;nbsp;I used it for my capstone project, a CRUD application for running a sign installation company. &amp;nbsp;I was pleasantly surprised. &amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Here are a few observations about the Editor:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;I was able to jump between code and the visual editor easily&lt;/li&gt;
&lt;li&gt;Changing my code by hand didn't break the visual editor&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The code generated by the editor was easily readable and clean&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;Even though I started with the WYSIWYG, as I got a simple layout produced, I found myself easily translating into the code. It was a good experience. &amp;nbsp;The visual components I created were originally going to be mock ups, but they were so clean, I've transitioned them into the working code. &amp;nbsp;The application is not done yet, but its looking good - I've already been able to easily incorporate changes requested by my client. &amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Now, here are the drawbacks I encountered:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;My 5 year old PC and laptop run the visual components a little slow.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The visual rendering isn't yet perfect. &amp;nbsp;While it was mostly reflective of the actual output, there were a few differences.&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;All in all, though, the drawbacks were more annoyances than restrictions.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;If you use GWT, I recommend you try GWT Designer.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-7074452510886648746?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/xJ51oaRqU5aJpiy200kvaeCjjpA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xJ51oaRqU5aJpiy200kvaeCjjpA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/xJ51oaRqU5aJpiy200kvaeCjjpA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xJ51oaRqU5aJpiy200kvaeCjjpA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/-1KMfeVyG1M" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/7074452510886648746/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/12/gwt-designer.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/7074452510886648746?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/7074452510886648746?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/-1KMfeVyG1M/gwt-designer.html" title="GWT Designer" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/12/gwt-designer.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU4DQX48fCp7ImA9Wx9UGE8.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-1787118788793911701</id><published>2010-12-24T11:40:00.000-08:00</published><updated>2011-02-15T19:46:10.074-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-15T19:46:10.074-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="GWT" /><category scheme="http://www.blogger.com/atom/ns#" term="development" /><category scheme="http://www.blogger.com/atom/ns#" term="code java" /><category scheme="http://www.blogger.com/atom/ns#" term="security" /><category scheme="http://www.blogger.com/atom/ns#" term="GWTServerServlet" /><category scheme="http://www.blogger.com/atom/ns#" term="annotations" /><title>GWT Simple Security Configuration</title><content type="html">For my capstone project in my MSE program at Carroll University, I am writing a GWT application.  One of the requirements for this application is security.  There are several posts on securing a GWT application on the Web already, but most of them are more heavy weight solutions that show you how to use Spring-AOP and Spring-security  to secure your app.  &lt;br /&gt;
&lt;br /&gt;
For this project, I don't need anything that heavy-weight.  Here is a little review of how I secured this application.&lt;br /&gt;
&lt;br /&gt;
First, I wanted each service to have it's own access controls.  More specifically, I want to be able to secure each method in each service.  Second, I want to be able to support Role-based access.  This way I can add users to roles to grant them the permissions they need. Finally, I don't have a real need to redefine role access in real-time. Given these constraints, I was able to put together a quick and easy solution to secure my application.  &lt;br /&gt;
&lt;br /&gt;
First, I decided that I wanted to be able to easily see in my code what the access control was.  It seemed to me that Annotations would be an easy way to do this.  Something like:&lt;br /&gt;
&lt;br /&gt;
@Secure("ROLE_ADMINISTRATOR")&lt;br /&gt;
&lt;br /&gt;
But I want to be able to specify multiple roles that can be configured to this action, so, multiple roles would look more like this:&lt;br /&gt;
&lt;br /&gt;
@Secure({"ROLE_ADMINISTRATOR, ROLE_CONFIGURATION_MANAGER"})&lt;br /&gt;
&lt;br /&gt;
So, the first thing I did was define the Annotation.  &lt;br /&gt;
&lt;div align="left" class="java"&gt;&lt;table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0"&gt;&lt;tbody&gt;
&lt;tr&gt;       &lt;td align="left" valign="top"&gt;&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
&lt;span style="color: #7f0055;"&gt;&lt;b&gt;package&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;com.archie.sma.server.util.security;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: white;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #7f0055;"&gt;&lt;b&gt;import&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;java.lang.annotation.ElementType;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #7f0055;"&gt;&lt;b&gt;import&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;java.lang.annotation.Retention;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #7f0055;"&gt;&lt;b&gt;import&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;java.lang.annotation.RetentionPolicy;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #7f0055;"&gt;&lt;b&gt;import&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;java.lang.annotation.Target;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: white;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #646464;"&gt;@Retention&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: black;"&gt;RetentionPolicy.RUNTIME&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #646464;"&gt;@Target&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: black;"&gt;ElementType.METHOD&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: #7f0055;"&gt;&lt;b&gt;public&amp;nbsp;@interface&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Secure&amp;nbsp;&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: white;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #7f0055;"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;String&lt;/span&gt;&lt;span style="color: black;"&gt;[]&amp;nbsp;&lt;/span&gt;&lt;span style="color: black;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;()&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: white;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;/td&gt;       &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;AS you can see, annotations are annotated.  This one says that this annotation applies to Methods, and it is kept at runtime.&lt;br /&gt;
&lt;br /&gt;
Next I needed to apply the annotation to my services.  This was quite easy.  In my Service  interface on the client, I used the annotation:&lt;br /&gt;
&lt;br /&gt;
@Secure({"ROLE_ADMINISTRATOR", "ROLE_CONFIGURATION_MANAGER"})&lt;br /&gt;
public Object get(String id) throws Exception {&lt;br /&gt;
//Method implementation here...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Of course, I now need to tie these two together.  Since I'm Using GWT, I decided to Extend the GWTServerServlet. By extending the servlet and over-riding the processCall and onAfterRequestDeserialized method, I am able to easily use a little bit of Reflection to get the annotation and determine if the user in Session has access to the method.  This approach also requires me to extend my new security Servlet for all my secured classes.  So its on the developer to ensure he is extending the class correctly.  Here is the class:&lt;br /&gt;
&lt;div align="left" class="java"&gt;&lt;table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff"&gt;   &lt;tr&gt;
  &lt;!-- start source code --&gt;
   &lt;td nowrap="nowrap" valign="top" align="left"&gt;&lt;br /&gt;
    &lt;code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;class&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;SecureRemoteServiceServlet&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;extends&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;RemoteServiceServlet&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;transient&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;IDaoFactory&amp;nbsp;daoFactory;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#646464"&gt;@Override&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;void&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;init&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;ServletConfig&amp;nbsp;config&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;throws&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;ServletException&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;super&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;.init&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;config&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;daoFactory&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;new&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;DaoFactory&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;/**&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*&amp;nbsp;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f5fbf"&gt;*/&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;static&amp;nbsp;final&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;long&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;serialVersionUID&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#990000"&gt;1000L&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;static&amp;nbsp;final&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;String&amp;nbsp;USER&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;USER&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;private&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;User&amp;nbsp;user;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#646464"&gt;@Override&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;protected&amp;nbsp;final&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;void&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;onAfterRequestDeserialized&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;RPCRequest&amp;nbsp;rpcRequest&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Method&amp;nbsp;method&amp;nbsp;=&amp;nbsp;rpcRequest.getMethod&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Secure&amp;nbsp;secure&amp;nbsp;=&amp;nbsp;method.getAnnotation&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;Secure.&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;class&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;Boolean&amp;nbsp;hasAccess&amp;nbsp;=&amp;nbsp;Boolean.FALSE;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;secure&amp;nbsp;!=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;String&lt;/font&gt;&lt;font color="#000000"&gt;[]&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;roles&amp;nbsp;=&amp;nbsp;secure.value&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;HttpServletRequest&amp;nbsp;request&amp;nbsp;=&amp;nbsp;getThreadLocalRequest&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;user&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;User&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;request.getSession&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;.getAttribute&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;USER&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;user&amp;nbsp;!=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;List&amp;lt;Role&amp;gt;&amp;nbsp;userRoles&amp;nbsp;=&amp;nbsp;user.getRoles&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;for&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;Role&amp;nbsp;role&amp;nbsp;:&amp;nbsp;userRoles&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;for&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;int&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;i&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="#990000"&gt;0&lt;/font&gt;&lt;font color="#000000"&gt;;&amp;nbsp;i&amp;nbsp;&amp;lt;&amp;nbsp;roles.length;&amp;nbsp;i++&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;role.toString&lt;/font&gt;&lt;font color="#000000"&gt;()&lt;/font&gt;&lt;font color="#000000"&gt;.equals&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;roles&lt;/font&gt;&lt;font color="#000000"&gt;[&lt;/font&gt;&lt;font color="#000000"&gt;i&lt;/font&gt;&lt;font color="#000000"&gt;]))&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;hasAccess&amp;nbsp;!=&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;null&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;hasAccess&amp;nbsp;=&amp;nbsp;Boolean.TRUE;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;if&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;!hasAccess&lt;/font&gt;&lt;font color="#000000"&gt;){&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;throw&amp;nbsp;new&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;RuntimeException&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#2a00ff"&gt;&amp;#34;Not&amp;nbsp;Authorized&amp;#34;&lt;/font&gt;&lt;font color="#000000"&gt;)&lt;/font&gt;&lt;font color="#000000"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f7f5f"&gt;/*&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f7f5f"&gt;*&amp;nbsp;@Override&amp;nbsp;public&amp;nbsp;final&amp;nbsp;String&amp;nbsp;processCall(String&amp;nbsp;payload)&amp;nbsp;throws&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f7f5f"&gt;*&amp;nbsp;SerializationException&amp;nbsp;{&amp;nbsp;if&amp;nbsp;(!hasAccess)&amp;nbsp;{&amp;nbsp;throw&amp;nbsp;new&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f7f5f"&gt;*&amp;nbsp;UnexpectedException(&amp;#34;Security&amp;nbsp;Exception&amp;#34;,&amp;nbsp;new&amp;nbsp;Exception(&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f7f5f"&gt;*&amp;nbsp;&amp;#34;Not&amp;nbsp;Authorized&amp;#34;));&amp;nbsp;}&amp;nbsp;return&amp;nbsp;super.processCall(payload);&amp;nbsp;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#3f7f5f"&gt;*/&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;User&amp;nbsp;getUser&lt;/font&gt;&lt;font color="#000000"&gt;()&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;return&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;user;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;void&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;setUser&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;User&amp;nbsp;user&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;.user&amp;nbsp;=&amp;nbsp;user;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;IDaoFactory&amp;nbsp;getDaoFactory&lt;/font&gt;&lt;font color="#000000"&gt;()&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;return&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;daoFactory;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;public&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;void&amp;nbsp;&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;setDaoFactory&lt;/font&gt;&lt;font color="#000000"&gt;(&lt;/font&gt;&lt;font color="#000000"&gt;IDaoFactory&amp;nbsp;daoFactory&lt;/font&gt;&lt;font color="#000000"&gt;)&amp;nbsp;{&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#7f0055"&gt;&lt;b&gt;this&lt;/b&gt;&lt;/font&gt;&lt;font color="#000000"&gt;.daoFactory&amp;nbsp;=&amp;nbsp;daoFactory;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#ffffff"&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;
&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;/code&gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;/td&gt;
  &lt;!-- end source code --&gt;
   &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;So that is my quick and dirty security implementation for my GWT app.  This is not what you would call an enterprise solution, but it is a quick way to implement security for a little, self-contained application.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-1787118788793911701?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XGlEkNSjjr0_Gkn4No8148t4e50/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XGlEkNSjjr0_Gkn4No8148t4e50/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XGlEkNSjjr0_Gkn4No8148t4e50/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XGlEkNSjjr0_Gkn4No8148t4e50/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/Pe4Ooi9FQuE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/1787118788793911701/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/12/gwt-simple-security-configuration.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1787118788793911701?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/1787118788793911701?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/Pe4Ooi9FQuE/gwt-simple-security-configuration.html" title="GWT Simple Security Configuration" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>3</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/12/gwt-simple-security-configuration.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cAQ3k9cSp7ImA9WxFUFUs.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-2438194393393224113</id><published>2010-06-26T07:04:00.000-07:00</published><updated>2010-06-26T07:04:02.769-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-26T07:04:02.769-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="software engineering" /><category scheme="http://www.blogger.com/atom/ns#" term="computer science" /><title>Software engineering vs. Computer Science</title><content type="html">&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;There is a difference between Software engineering and Computer Science. &amp;nbsp;Just like there is a difference between Mechanical Engineering and Physics. &amp;nbsp;I believe that the big difference is that in the physical world, people have been building structures for thousands of years. &amp;nbsp;The difference between the theory of the physical world and the building of things in it is well defined. &amp;nbsp;Modern computing has been around for what, 60 years? &amp;nbsp;We have not yet defined the difference between the theory of Computing and the practical application of it.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;In computer science, the task is often to find the best, or most efficient way to perform a task. &amp;nbsp;Often, Computer Scientists are looking for theoretical limits, or trying to resolve a new computing issue in a novel way. &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;In software engineering, you are trying to build a system to meet a need using the tools at hand, given the constraints of your system. &amp;nbsp;Often this means NOT using the best solution. &amp;nbsp;It means using the best solution given the constraints placed on your use case. &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;As a Software Engineer, this means we do not necessarily build things on our own. &amp;nbsp;Rather, we evaluate alternatives and select the best one. &amp;nbsp;One example of this might be Web Frameworks. &amp;nbsp;Another might be ORM selection (or whether to use either of these technologies at all and actually build our own). &amp;nbsp; This is&amp;nbsp;analogous&amp;nbsp;to a mechanical engineer designing a car - he doesn't also design the tires, alternator, battery, etc. &amp;nbsp;Rather he looks at the alternative possibilities and selects the best one for the vehicle he is trying to create. &amp;nbsp;Of course, there are going to be many areas that the mechanical engineer does build. &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;It seems like in software engineering, using other peoples tools is frowned upon. &amp;nbsp;There is some sort of badge of honor to say, 'I built this myself'. &amp;nbsp;While it is impressive to have that broad range of skill, does that necessarily make you the best Software Engineer? &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;It is my belief that as a profession, we Software Engineers need to start thinking of our task as more of an engineering endeavor and less of computer science endeavor. &amp;nbsp;We need to see ourselves not as Scientists, but as Engineers. &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-2438194393393224113?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/61KheIpCwaLoCicvhG2IjY5Y9u0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/61KheIpCwaLoCicvhG2IjY5Y9u0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/61KheIpCwaLoCicvhG2IjY5Y9u0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/61KheIpCwaLoCicvhG2IjY5Y9u0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/3DlY983iTAI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/2438194393393224113/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/06/software-engineering-vs-computer.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/2438194393393224113?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/2438194393393224113?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/3DlY983iTAI/software-engineering-vs-computer.html" title="Software engineering vs. Computer Science" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/06/software-engineering-vs-computer.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkIBQXgzeCp7ImA9WxFVE0Q.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-6293773444037678107</id><published>2010-06-12T19:02:00.000-07:00</published><updated>2010-06-12T19:02:30.680-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-12T19:02:30.680-07:00</app:edited><title>Bug fix for Unit Test Generation Plugin</title><content type="html">I fixed a few bugs in the Unit Test Generation Plugin. &amp;nbsp;I think the next release is going to be a re-factor only, as the code is pretty rough and I want to be able to keep this project clean. &amp;nbsp;I believe after a few more edits I will be comfortable declaring the project a 'Beta' project. &lt;br /&gt;
&lt;br /&gt;
&lt;a href="https://sourceforge.net/projects/unittestgplugin/"&gt;Get the latest download&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-6293773444037678107?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zjW_R1yoBfG6GFqgYmN9v3UrfI8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zjW_R1yoBfG6GFqgYmN9v3UrfI8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zjW_R1yoBfG6GFqgYmN9v3UrfI8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zjW_R1yoBfG6GFqgYmN9v3UrfI8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/GCPYYGFUj68" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/6293773444037678107/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/06/bug-fix-for-unit-test-generation-plugin.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/6293773444037678107?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/6293773444037678107?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/GCPYYGFUj68/bug-fix-for-unit-test-generation-plugin.html" title="Bug fix for Unit Test Generation Plugin" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/06/bug-fix-for-unit-test-generation-plugin.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck8HRX4-eSp7ImA9WxFVEEk.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-3906270937095676697</id><published>2010-06-08T16:47:00.000-07:00</published><updated>2010-06-08T16:47:14.051-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-08T16:47:14.051-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="development" /><category scheme="http://www.blogger.com/atom/ns#" term="code" /><category scheme="http://www.blogger.com/atom/ns#" term="test" /><category scheme="http://www.blogger.com/atom/ns#" term="unit" /><category scheme="http://www.blogger.com/atom/ns#" term="refactor" /><title>Inversion of Control - Results</title><content type="html">In a &lt;a href="http://benarchie.blogspot.com/2010/05/identifying-and-refactoring-inversion.html"&gt;previous post&lt;/a&gt;&amp;nbsp;I talked about how to identify and&amp;nbsp;re-factor&amp;nbsp;a section of code that had a switch statement in it. &amp;nbsp;I was finally able to get to that statement at work and&amp;nbsp;re-factor&amp;nbsp;it. &amp;nbsp;I thought the results would be interesting to post. &lt;br /&gt;
&lt;br /&gt;
First and foremost, I was able to remove 120 unnecessary lines of code and replace it with 2 lines. &amp;nbsp;That's right, where once there were 120 lines there are now &amp;nbsp;2. &lt;br /&gt;
&lt;br /&gt;
But there is more to the story than that. &amp;nbsp;As I mention before, I've been unit testing the application(and that led to a new &lt;a href="https://sourceforge.net/projects/unittestgplugin/"&gt;Eclipse tool&lt;/a&gt;), so as I&amp;nbsp;re-factored&amp;nbsp;the system, I re-ran my tests. &amp;nbsp;At one point my tests FAILED. &amp;nbsp;That's right, I BROKE the intended&amp;nbsp;functionality&amp;nbsp;with a&amp;nbsp;re-factor&amp;nbsp;that&amp;nbsp;shouldn't&amp;nbsp;have.&lt;br /&gt;
&lt;br /&gt;
When I looked into it further, I found that one little switch statement buried in that twenty-two case wonder didn't follow the pattern. &amp;nbsp;Instead of getting an implementation from the factory and calling a getMap method, it was calling an entirely different method in the main class. &amp;nbsp;After a little research and a few short lines of code, the issue was fixed and the tests once again showed a green bar. &lt;br /&gt;
&lt;br /&gt;
The actual work here is not relevant, what is relevant is this: &amp;nbsp;&lt;i&gt;This is a pre-release version of this system. &amp;nbsp;It is not even in Beta testing yet, and there is already a place in the code that was 'hacked' to work differently than you would expect.&lt;/i&gt; &amp;nbsp;The implementation of a pattern will help prevent this kind of degrading of the code in the future, but it was there. &amp;nbsp;In the code. Before the software was even released.&lt;br /&gt;
&lt;br /&gt;
The second thing to take away from this: &amp;nbsp;&lt;i&gt;If I hadn't written all those unit tests, I wouldn't have found the error. &lt;/i&gt;&amp;nbsp;I would have&amp;nbsp;re-factored&amp;nbsp;the code and introduced a bug into the system without realizing it. &amp;nbsp;Unit tests saved me. &amp;nbsp;And because of that, my company will now have a system that is more resistant to errors and less prone to new bugs.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-3906270937095676697?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/iyLpxVDWlFCb2g6cNJT_-BACBW0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iyLpxVDWlFCb2g6cNJT_-BACBW0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/iyLpxVDWlFCb2g6cNJT_-BACBW0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iyLpxVDWlFCb2g6cNJT_-BACBW0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/LLULoqKqf1U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/3906270937095676697/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/06/inversion-of-control-results.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/3906270937095676697?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/3906270937095676697?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/LLULoqKqf1U/inversion-of-control-results.html" title="Inversion of Control - Results" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/06/inversion-of-control-results.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cGSHszeyp7ImA9WxFWGEo.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-3234917484876877907</id><published>2010-06-06T17:35:00.000-07:00</published><updated>2010-06-06T17:37:09.583-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-06T17:37:09.583-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="open source" /><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="development" /><category scheme="http://www.blogger.com/atom/ns#" term="plugin" /><category scheme="http://www.blogger.com/atom/ns#" term="eclipse" /><category scheme="http://www.blogger.com/atom/ns#" term="test" /><category scheme="http://www.blogger.com/atom/ns#" term="unit" /><title>A two week diversion.  Eclipse Plugin to Generate Unit Tests</title><content type="html">I took a new job about a month ago at an insurance company.  The company is in the middle of rewriting the Point of Sale application for its line of business.  Since the project had already kicked off and development had begun, I had been assigned the glorious task of filling in Unit test coverage.  &lt;br /&gt;
&lt;br /&gt;
You see, the management at this company is pretty new to Agile development methodologies, and until about a year ago, Unit tests were largely unknown to them.  They are driving a tight timeline for delivering the rewritten project.  Often, the developers are going over their burndown hours, so the first thing to suffer was, you guessed it, the unit tests.  On top of that, management is insistent on getting 80% coverage.  As you can probably guess from the title of this post, I was assigned the duty of creating unit tests for this iteration of the code, with the purpose of increasing code coverage to as close to 80% as possible.  &lt;br /&gt;
&lt;br /&gt;
When I was handed the code, the coverage, depending on sub-project, was between 0% and 20% covered.&lt;br /&gt;
&lt;br /&gt;
I decided to create an Eclipse plugin to help me with the task (On my own time, of course. &amp;nbsp;During the day, I wrote tests.) I call it Unit Test Generation Plugin. &amp;nbsp;This version is very basic. &amp;nbsp;It really only works to create tests for POJOs at this point, but I think I'll extend it a bit to make it more useful soon. &lt;br /&gt;
&lt;br /&gt;
I was really quite surprised at how easy and fun it was to create a plugin for Eclipse. &amp;nbsp; I thought I'd post some of the code and the process I used to create the plugin on the site. &lt;br /&gt;
&lt;br /&gt;
Here are some highlights. &amp;nbsp;Let me know if you'd like me to write more detail about a particular topic.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Binding An Event To The Eclipse Package Menu&lt;/b&gt;&lt;br /&gt;
Eclipse uses a powerful plugin framework to allow you to extend and enhance the platform. &amp;nbsp;The way to add extensions is to use the PDE, which is a part of the &lt;a href="http://www.eclipse.org/projects/project_summary.php?projectid=eclipse"&gt;core project&lt;/a&gt;. &amp;nbsp;Eclipse makes creating a new project VERY easy. &amp;nbsp;To start, go to File-&amp;gt;New-&amp;gt;Project...-&amp;gt;Plugin Project.&lt;br /&gt;
&lt;br /&gt;
In the New Plugin Window, Enter a name, like com.a.plugin. &amp;nbsp;The rest of the defaults can be left alone. &amp;nbsp;Click, Next. &amp;nbsp;Another popup appears to allow you to select more options for your project config. &amp;nbsp;Modify the name and provider all you like, but the defaults should suffice. &amp;nbsp;Click Next.&lt;br /&gt;
&lt;br /&gt;
The final panel allows you to select several templates that demonstrate different extensions. &amp;nbsp;You can select a template if you like, or none at all. &amp;nbsp;For this project, I will select none.&lt;br /&gt;
&lt;br /&gt;
When you finish, a new Eclipse plugin project is created and you are ready to continue. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Extension Points&lt;/b&gt;&lt;br /&gt;
An extension point is how you add new functionality to Eclipse. &amp;nbsp;They are configured using an XML file. &amp;nbsp;Eclipse supplies an editor to allow you to easily edit this file. &amp;nbsp;I had some trouble configuring my Handler through this screen, but was able to write the code by hand. &amp;nbsp;Once I edited it, it appeared correctly in the editor. &amp;nbsp;I've done a little reading and that might be because I didn't have some RCP plugins installed, but I haven't verified it yet.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Java Code Support&lt;/b&gt;&lt;br /&gt;
It also turns out that Eclipse has robust support for Java project Elements. &amp;nbsp;There are interfaces like ICompilationUnit, IPackageFragment and so forth. &amp;nbsp;It was pretty easy to generate new tests.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The Result&lt;/b&gt;&lt;br /&gt;
I have decided that I am going to make the project open-source and created a SourceForge project for it. &amp;nbsp;It is still in Alpha stage, and there is a bit of work to make it a 'real' plugin, but its now out there in the world.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://sourceforge.net/projects/unittestgplugin/"&gt;Unit Test Generation Plugin&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
Now, I'm sure that &lt;a href="http://www.objectmentor.com/omTeam/martin_r.html"&gt;Robert Martin&lt;/a&gt;  would hunt me down if he knew what I created, and would destroy the laptop it was written on (OK, I don't really know him, but I've read one of his books, &lt;a href="http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445?ie=UTF8&amp;amp;tag=benarcis-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969" target="_blank"&gt;Agile Software Development, Principles, Patterns, and Practices&lt;/a&gt;&lt;img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=benarcis-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0135974445" style="border: none !important; margin: 0px !important; padding: 0px !important;" width="1" /&gt;. &amp;nbsp;From the perspectives presented there, I can infer that he would not be happy with my tool&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-3234917484876877907?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/OuC3WEDKwFa0ChuMtK8xcuW22js/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OuC3WEDKwFa0ChuMtK8xcuW22js/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/OuC3WEDKwFa0ChuMtK8xcuW22js/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OuC3WEDKwFa0ChuMtK8xcuW22js/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/9NF9IuckaMs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/3234917484876877907/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/06/two-week-diversion-eclipse-plugin-to.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/3234917484876877907?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/3234917484876877907?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/9NF9IuckaMs/two-week-diversion-eclipse-plugin-to.html" title="A two week diversion.  Eclipse Plugin to Generate Unit Tests" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/06/two-week-diversion-eclipse-plugin-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkcER3g8fCp7ImA9WxFXGE4.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-6270310221180824334</id><published>2010-05-25T16:26:00.000-07:00</published><updated>2010-05-25T16:26:46.674-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-05-25T16:26:46.674-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="inversion of control" /><category scheme="http://www.blogger.com/atom/ns#" term="design patterns" /><category scheme="http://www.blogger.com/atom/ns#" term="code" /><category scheme="http://www.blogger.com/atom/ns#" term="refactoring" /><title>Identifying and refactoring - Inversion Of Control</title><content type="html">In my new position, I am now working on a large team of developers (15 in all).  This team is working to complete a single product that has been designed using a Service Oriented Architecture.  I have some insights into that design as well, however, in this post I thought I would show an example of how to identify and refactor to a design pattern, and why I feel this is a good thing for the project in the long term.  &lt;br /&gt;
&lt;br /&gt;
One of the hardest things about design patterns is recognizing when to use them and when not to.  I have seen so many architects apply patterns for patterns sake, making code more complex, and making it HARDER to apply patterns where they are truly needed.  I've also seen lots of code that isn't refactored to design patterns, and it COULD be made much easier to maintain and understand if it were changed.  The code I will be talking about today is of the latter type.  I've been writing Unit tests against the codebase at my new position. This has given me an opportunity to review the code.  In order to protect the IP of the company I work for, the samples below have been changed.  They represent the structure of some of the code, but are entirely unrelated to what my company does.  &lt;br /&gt;
&lt;br /&gt;
First, let me describe the general design of the code as it stands.  In this instance, we have a method that takes an Enumeration argument.  The method then calls the getInstance method of a Factory, and gets the proper implementation of an Interface.  The getInstance method also takes an Enumeration.  Finally, the code uses a case statement that tests the Enumeration value and uses the interface to set the values of different member variables in the class. &amp;nbsp;Here is the sample code that would implement this design:&lt;br /&gt;
&lt;br /&gt;
The Container. &amp;nbsp;This is the class where our execution starts. &amp;nbsp;We call setMapValue and pass it a Type enum.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style='color: Teal; background-color: '&gt;  1&lt;/span&gt; &lt;span style=' color: Blue;'&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style='color: Teal; background-color: '&gt;  2&lt;/span&gt; &lt;span style=' color: Blue;'&gt;import&lt;/span&gt; com.arciszewski.staterefactor.Type; 
&lt;span style='color: Teal; background-color: '&gt;  3&lt;/span&gt; &lt;span style=' color: Blue;'&gt;import&lt;/span&gt; java.util.Map; 
&lt;span style='color: Teal; background-color: '&gt;  4&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt;  5&lt;/span&gt; &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;class&lt;/span&gt; Container {
&lt;span style='color: Teal; background-color: '&gt;  6&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;private&lt;/span&gt; Map&amp;lt;String,String&amp;gt; map1; 
&lt;span style='color: Teal; background-color: '&gt;  7&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;private&lt;/span&gt; Map&amp;lt;String,String&amp;gt; map2; 
&lt;span style='color: Teal; background-color: '&gt;  8&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;private&lt;/span&gt; Map&amp;lt;String,String&amp;gt; map3; 
&lt;span style='color: Teal; background-color: '&gt;  9&lt;/span&gt;      
&lt;span style='color: Teal; background-color: '&gt; 10&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMapValue(Type type) {
&lt;span style='color: Teal; background-color: '&gt; 11&lt;/span&gt;          
&lt;span style='color: Teal; background-color: '&gt; 12&lt;/span&gt;         ITypeInterface intf = TypeFactory.getInstance(type); 
&lt;span style='color: Teal; background-color: '&gt; 13&lt;/span&gt;          
&lt;span style='color: Teal; background-color: '&gt; 14&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;switch&lt;/span&gt;(type) { 
&lt;span style='color: Teal; background-color: '&gt; 15&lt;/span&gt;             &lt;span style=' color: Blue;'&gt;case&lt;/span&gt; ONE : 
&lt;span style='color: Teal; background-color: '&gt; 16&lt;/span&gt;                 map1 = intf.getMap(); 
&lt;span style='color: Teal; background-color: '&gt; 17&lt;/span&gt;                 &lt;span style=' color: Blue;'&gt;return&lt;/span&gt;; 
&lt;span style='color: Teal; background-color: '&gt; 18&lt;/span&gt;             &lt;span style=' color: Blue;'&gt;case&lt;/span&gt; TWO :
&lt;span style='color: Teal; background-color: '&gt; 19&lt;/span&gt;                 map2 = intf.getMap(); 
&lt;span style='color: Teal; background-color: '&gt; 20&lt;/span&gt;                 &lt;span style=' color: Blue;'&gt;return&lt;/span&gt;; 
&lt;span style='color: Teal; background-color: '&gt; 21&lt;/span&gt;             &lt;span style=' color: Blue;'&gt;case&lt;/span&gt; THREE : 
&lt;span style='color: Teal; background-color: '&gt; 22&lt;/span&gt;                 map3 = intf.getMap(); 
&lt;span style='color: Teal; background-color: '&gt; 23&lt;/span&gt;                 &lt;span style=' color: Blue;'&gt;return&lt;/span&gt;; 
&lt;span style='color: Teal; background-color: '&gt; 24&lt;/span&gt;             &lt;span style=' color: Blue;'&gt;default&lt;/span&gt; : 
&lt;span style='color: Teal; background-color: '&gt; 25&lt;/span&gt;                 &lt;span style=' color: Blue;'&gt;return&lt;/span&gt;; 
&lt;span style='color: Teal; background-color: '&gt; 26&lt;/span&gt;                  
&lt;span style='color: Teal; background-color: '&gt; 27&lt;/span&gt;                  
&lt;span style='color: Teal; background-color: '&gt; 28&lt;/span&gt;         } 
&lt;span style='color: Teal; background-color: '&gt; 29&lt;/span&gt;          
&lt;span style='color: Teal; background-color: '&gt; 30&lt;/span&gt;          
&lt;span style='color: Teal; background-color: '&gt; 31&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 32&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 33&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap1() { 
&lt;span style='color: Teal; background-color: '&gt; 34&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;return&lt;/span&gt; map1; 
&lt;span style='color: Teal; background-color: '&gt; 35&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 36&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 37&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMap1(Map&amp;lt;String, String&amp;gt; map1) {
&lt;span style='color: Teal; background-color: '&gt; 38&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;this&lt;/span&gt;.map1 = map1; 
&lt;span style='color: Teal; background-color: '&gt; 39&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 40&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 41&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap2() { 
&lt;span style='color: Teal; background-color: '&gt; 42&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;return&lt;/span&gt; map2; 
&lt;span style='color: Teal; background-color: '&gt; 43&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 44&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 45&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMap2(Map&amp;lt;String, String&amp;gt; map2) {
&lt;span style='color: Teal; background-color: '&gt; 46&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;this&lt;/span&gt;.map2 = map2; 
&lt;span style='color: Teal; background-color: '&gt; 47&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 48&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 49&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap3() { 
&lt;span style='color: Teal; background-color: '&gt; 50&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;return&lt;/span&gt; map3; 
&lt;span style='color: Teal; background-color: '&gt; 51&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 52&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 53&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMap3(Map&amp;lt;String, String&amp;gt; map3) {
&lt;span style='color: Teal; background-color: '&gt; 54&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;this&lt;/span&gt;.map3 = map3; 
&lt;span style='color: Teal; background-color: '&gt; 55&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 56&lt;/span&gt;      
&lt;span style='color: Teal; background-color: '&gt; 57&lt;/span&gt; } 
&lt;span style='color: Teal; background-color: '&gt; 58&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 59&lt;/span&gt; &lt;/pre&gt;&lt;br /&gt;
This is the Factory that creates ITypeIntf concrete classes. &amp;nbsp;For now there is only one implementation for type.ONE.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: teal;"&gt;  1&lt;/span&gt; &lt;span style="color: blue;"&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style="color: teal;"&gt;  2&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  3&lt;/span&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; TypeFactory {
&lt;span style="color: teal;"&gt;  4&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  5&lt;/span&gt;     &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; ITypeInterface getInstance(Type type) {
&lt;span style="color: teal;"&gt;  6&lt;/span&gt;          
&lt;span style="color: teal;"&gt;  7&lt;/span&gt;         ITypeInterface typeIntf = &lt;span style="color: blue;"&gt;null&lt;/span&gt;; 
&lt;span style="color: teal;"&gt;  8&lt;/span&gt;         &lt;span style="color: blue;"&gt;if&lt;/span&gt;(type.ONE == type) { 
&lt;span style="color: teal;"&gt;  9&lt;/span&gt;           return new  TypeImplOne();  
&lt;span style="color: teal;"&gt; 10&lt;/span&gt;         } &lt;span style="color: blue;"&gt;if&lt;/span&gt;(type.TWO == type) { 
&lt;span style="color: teal;"&gt; 11&lt;/span&gt;              
&lt;span style="color: teal;"&gt; 12&lt;/span&gt;         } &lt;span style="color: blue;"&gt;if&lt;/span&gt;(type.THREE == type) { 
&lt;span style="color: teal;"&gt; 13&lt;/span&gt;              
&lt;span style="color: teal;"&gt; 14&lt;/span&gt;         } 
&lt;span style="color: teal;"&gt; 15&lt;/span&gt;         &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;null&lt;/span&gt;; 
&lt;span style="color: teal;"&gt; 16&lt;/span&gt;     } 
&lt;span style="color: teal;"&gt; 17&lt;/span&gt;      
&lt;span style="color: teal;"&gt; 18&lt;/span&gt; }&lt;/pre&gt;&lt;br /&gt;
The Enum. &amp;nbsp;Not much to say here.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: teal;"&gt;  1&lt;/span&gt; &lt;span style="color: blue;"&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style="color: teal;"&gt;  2&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  3&lt;/span&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;enum&lt;/span&gt; Type { 
&lt;span style="color: teal;"&gt;  4&lt;/span&gt;     ONE, TWO, THREE; 
&lt;span style="color: teal;"&gt;  5&lt;/span&gt; }&lt;/pre&gt;&lt;br /&gt;
The Interface that defines getMap.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: teal;"&gt;  1&lt;/span&gt; &lt;span style="color: blue;"&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style="color: teal;"&gt;  2&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  3&lt;/span&gt; &lt;span style="color: blue;"&gt;import&lt;/span&gt; java.util.Map; 
&lt;span style="color: teal;"&gt;  4&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  5&lt;/span&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; ITypeInterface {
&lt;span style="color: teal;"&gt;  6&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  7&lt;/span&gt;     &lt;span style="color: blue;"&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap(); 
&lt;span style="color: teal;"&gt;  8&lt;/span&gt; }&lt;/pre&gt;&lt;br /&gt;
An implementation class. Kept simple for discussion.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: teal;"&gt;  1&lt;/span&gt; &lt;span style="color: blue;"&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style="color: teal;"&gt;  2&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  3&lt;/span&gt; &lt;span style="color: blue;"&gt;import&lt;/span&gt; java.util.HashMap; 
&lt;span style="color: teal;"&gt;  4&lt;/span&gt; &lt;span style="color: blue;"&gt;import&lt;/span&gt; java.util.Map; 
&lt;span style="color: teal;"&gt;  5&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  6&lt;/span&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; TypeImplOne &lt;span style="color: blue;"&gt;implements&lt;/span&gt; ITypeInterface {
&lt;span style="color: teal;"&gt;  7&lt;/span&gt;  
&lt;span style="color: teal;"&gt;  8&lt;/span&gt;     @Override 
&lt;span style="color: teal;"&gt;  9&lt;/span&gt;     &lt;span style="color: blue;"&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap() { 
&lt;span style="color: teal;"&gt; 10&lt;/span&gt;         &lt;span style="color: green;"&gt;//Do something like get data from a DB.&lt;/span&gt; 
&lt;span style="color: teal;"&gt; 11&lt;/span&gt;         &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; HashMap&amp;lt;String, String&amp;gt;();
&lt;span style="color: teal;"&gt; 12&lt;/span&gt;     } 
&lt;span style="color: teal;"&gt; 13&lt;/span&gt;  
&lt;span style="color: teal;"&gt; 14&lt;/span&gt; }&lt;/pre&gt;&lt;br /&gt;
Looking at this code, you'd probably say that the developers did some things right. &amp;nbsp;They designed to interfaces, they are using a creational pattern, and the code is pretty clean and easy to read. &amp;nbsp;I tend to agree, except when it comes to that ugly switch statement. &amp;nbsp;Here are my issues with the switch statement:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Developers have to remember the return statement, or we will have NULL implementations when we shouldn't&lt;/li&gt;
&lt;li&gt;Developers will be tempted to add more logic to the case statements that are Type specific. &amp;nbsp;As these add up, the clarity of the code falls&lt;/li&gt;
&lt;li&gt;We're already verifying the type in the Factory, so this code is checking the Type value twice when it doesn't have to.&lt;/li&gt;
&lt;li&gt;Container needs to be aware of the different implementations of ITypeIntf. &amp;nbsp;Why should it need to? &amp;nbsp;Now we've added a dependency we don't need.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
There is a change we can make that will improve this code. &amp;nbsp;Let's change the ITypeInterface to take a Container object in its argument list. &amp;nbsp;Then we can set the value in the method and the case statement is no longer needed. &amp;nbsp;This is a classic IOC technique. &amp;nbsp;Here is what the code looks like after the refactoring.&lt;br /&gt;
&lt;br /&gt;
The new Container.  Notice, there is no switch statement.  None is needed. the IMPLEMENTATION of the ITypeIntf knows which attribute it needs to set.  Also note that Container doesn't need to know anything about the implementation that the Factory returns.  &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style='color: Teal; background-color: '&gt;  1&lt;/span&gt; &lt;span style=' color: Blue;'&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style='color: Teal; background-color: '&gt;  2&lt;/span&gt; &lt;span style=' color: Blue;'&gt;import&lt;/span&gt; com.arciszewski.staterefactor.Type; 
&lt;span style='color: Teal; background-color: '&gt;  3&lt;/span&gt; &lt;span style=' color: Blue;'&gt;import&lt;/span&gt; java.util.Map; 
&lt;span style='color: Teal; background-color: '&gt;  4&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt;  5&lt;/span&gt; &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;class&lt;/span&gt; Container {
&lt;span style='color: Teal; background-color: '&gt;  6&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;private&lt;/span&gt; Map&amp;lt;String,String&amp;gt; map1; 
&lt;span style='color: Teal; background-color: '&gt;  7&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;private&lt;/span&gt; Map&amp;lt;String,String&amp;gt; map2; 
&lt;span style='color: Teal; background-color: '&gt;  8&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;private&lt;/span&gt; Map&amp;lt;String,String&amp;gt; map3; 
&lt;span style='color: Teal; background-color: '&gt;  9&lt;/span&gt;      
&lt;span style='color: Teal; background-color: '&gt; 10&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMapValue(Type type) {
&lt;span style='color: Teal; background-color: '&gt; 11&lt;/span&gt;         ITypeInterface intf = TypeFactory.getInstance(type); 
&lt;span style='color: Teal; background-color: '&gt; 12&lt;/span&gt;         intf.setMapValue(&lt;span style=' color: Blue;'&gt;this&lt;/span&gt;); 
&lt;span style='color: Teal; background-color: '&gt; 13&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 14&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 15&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap1() { 
&lt;span style='color: Teal; background-color: '&gt; 16&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;return&lt;/span&gt; map1; 
&lt;span style='color: Teal; background-color: '&gt; 17&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 18&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 19&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMap1(Map&amp;lt;String, String&amp;gt; map1) {
&lt;span style='color: Teal; background-color: '&gt; 20&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;this&lt;/span&gt;.map1 = map1; 
&lt;span style='color: Teal; background-color: '&gt; 21&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 22&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 23&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap2() { 
&lt;span style='color: Teal; background-color: '&gt; 24&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;return&lt;/span&gt; map2; 
&lt;span style='color: Teal; background-color: '&gt; 25&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 26&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 27&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMap2(Map&amp;lt;String, String&amp;gt; map2) {
&lt;span style='color: Teal; background-color: '&gt; 28&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;this&lt;/span&gt;.map2 = map2; 
&lt;span style='color: Teal; background-color: '&gt; 29&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 30&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 31&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; Map&amp;lt;String, String&amp;gt; getMap3() { 
&lt;span style='color: Teal; background-color: '&gt; 32&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;return&lt;/span&gt; map3; 
&lt;span style='color: Teal; background-color: '&gt; 33&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 34&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 35&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMap3(Map&amp;lt;String, String&amp;gt; map3) {
&lt;span style='color: Teal; background-color: '&gt; 36&lt;/span&gt;         &lt;span style=' color: Blue;'&gt;this&lt;/span&gt;.map3 = map3; 
&lt;span style='color: Teal; background-color: '&gt; 37&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 38&lt;/span&gt;      
&lt;span style='color: Teal; background-color: '&gt; 39&lt;/span&gt;      
&lt;span style='color: Teal; background-color: '&gt; 40&lt;/span&gt; }&lt;/pre&gt;&lt;br /&gt;
The new interface.  The interface definition was changed to return void and to make the name descriptive of what it does now.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style='color: Teal; background-color: '&gt;  1&lt;/span&gt; &lt;span style=' color: Blue;'&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style='color: Teal; background-color: '&gt;  2&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt;  3&lt;/span&gt; &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;interface&lt;/span&gt; ITypeInterface {
&lt;span style='color: Teal; background-color: '&gt;  4&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt;  5&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMapValue(Container container);
&lt;span style='color: Teal; background-color: '&gt;  6&lt;/span&gt; }&lt;/pre&gt;&lt;br /&gt;
The new implementation.  Notice that the new implementation now has a dependency on Container.  That's OK in my mind, since the interface is intended to work on Container. Here's what we gained, though.  Now ALL the logic for this condition is in ONE place.  Container will never need to be changed, even if a new implementation is defined (For instance, an implementation that will set ALL the map attribute values.)&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style='color: Teal; background-color: '&gt;  1&lt;/span&gt; &lt;span style=' color: Blue;'&gt;package&lt;/span&gt; com.arciszewski.staterefactor; 
&lt;span style='color: Teal; background-color: '&gt;  2&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt;  3&lt;/span&gt; &lt;span style=' color: Blue;'&gt;import&lt;/span&gt; java.util.HashMap; 
&lt;span style='color: Teal; background-color: '&gt;  4&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt;  5&lt;/span&gt; &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;class&lt;/span&gt; TypeImplOne &lt;span style=' color: Blue;'&gt;implements&lt;/span&gt; ITypeInterface {
&lt;span style='color: Teal; background-color: '&gt;  6&lt;/span&gt;     &lt;span style=' color: Green;'&gt;// Changed the name, too. getMap no longer describes this function, but&lt;/span&gt;
&lt;span style='color: Teal; background-color: '&gt;  7&lt;/span&gt;     &lt;span style=' color: Green;'&gt;// setMapValue does.&lt;/span&gt; 
&lt;span style='color: Teal; background-color: '&gt;  8&lt;/span&gt;     @Override 
&lt;span style='color: Teal; background-color: '&gt;  9&lt;/span&gt;     &lt;span style=' color: Blue;'&gt;public&lt;/span&gt; &lt;span style=' color: Blue;'&gt;void&lt;/span&gt; setMapValue(Container container) {
&lt;span style='color: Teal; background-color: '&gt; 10&lt;/span&gt;         &lt;span style=' color: Green;'&gt;// Do something like get data from a DB.&lt;/span&gt; 
&lt;span style='color: Teal; background-color: '&gt; 11&lt;/span&gt;         container.setMap1(&lt;span style=' color: Blue;'&gt;new&lt;/span&gt; HashMap&amp;lt;String, String&amp;gt;());
&lt;span style='color: Teal; background-color: '&gt; 12&lt;/span&gt;     } 
&lt;span style='color: Teal; background-color: '&gt; 13&lt;/span&gt;  
&lt;span style='color: Teal; background-color: '&gt; 14&lt;/span&gt; }&lt;/pre&gt;&lt;br /&gt;
So what pattern does this conform to?  It looks a bit like a State or Strategy pattern.  It also looks a bit like a Command pattern.  Which would you say it is?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-6270310221180824334?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/_QbzV9l-FMBkwoHnoMqgl5K2Pnk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_QbzV9l-FMBkwoHnoMqgl5K2Pnk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/_QbzV9l-FMBkwoHnoMqgl5K2Pnk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_QbzV9l-FMBkwoHnoMqgl5K2Pnk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/yZrbi7iTano" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/6270310221180824334/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/05/identifying-and-refactoring-inversion.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/6270310221180824334?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/6270310221180824334?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/yZrbi7iTano/identifying-and-refactoring-inversion.html" title="Identifying and refactoring - Inversion Of Control" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/05/identifying-and-refactoring-inversion.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU8BRncyeCp7ImA9WxFXFk4.&quot;"><id>tag:blogger.com,1999:blog-3060516345583304229.post-8044225507707392199</id><published>2010-05-23T07:28:00.000-07:00</published><updated>2010-05-23T11:04:17.990-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-05-23T11:04:17.990-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="code" /><category scheme="http://www.blogger.com/atom/ns#" term="database" /><category scheme="http://www.blogger.com/atom/ns#" term="android" /><category scheme="http://www.blogger.com/atom/ns#" term="sqllite" /><title>Android Application Part 6 - SQLLite Database</title><content type="html">&lt;span class="Apple-style-span" style="font-family: Arial;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;This post is part 6 of a series. &amp;nbsp;&lt;/span&gt;&lt;a href="http://benarchie.blogspot.com/2010/05/android-application-part-5-creating.html"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;Click here to go to part 5&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial; font-size: small;"&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman';"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;In my previous post, I walked through the scaffolding for the ListService, which will query a REST Web service and sync data locally. &amp;nbsp;Of course, the code was just the implementation to allow the service to run in a new Thread. &amp;nbsp;Today I am going to start implementing the storage part of the application. &amp;nbsp;Android has a a &lt;a href="http://www.sqlite.org/"&gt;SQLLite&lt;/a&gt;. Database. &amp;nbsp;The OS makes it available through a service. &amp;nbsp;It even comes packaged with a SQLLiteOpenHelper class. &amp;nbsp;The implementation for this one is rather easy. &amp;nbsp;Here I've modified the sample presented on the &lt;a href="http://developer.android.com/guide/topics/data/data-storage.html#db"&gt;Android Developer Site&lt;/a&gt;. &amp;nbsp;First I'll show you the code, then I'll talk through what its doing. &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="sc16"&gt;package&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;com&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;arciszewski&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;family&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;shopping&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;db&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc5"&gt;import&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;android&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;content&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;Context&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc5"&gt;import&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;android&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;database&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;sqlite&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;SQLiteDatabase&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc5"&gt;import&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;android&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;database&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;sqlite&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;SQLiteOpenHelper&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc5"&gt;import&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;android&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;database&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;sqlite&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;SQLiteDatabase&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;CursorFactory&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;public&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;class&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;FamilyListOpenHelper&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc5"&gt;extends&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;SQLiteOpenHelper&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;{&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;private&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;static&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;final&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;int&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;DATABASE_VERSION&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;=&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc4"&gt;1&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;private&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;static&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;final&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;String&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;FAMILY_LIST_TABLE_NAME&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;=&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc6"&gt;"familyList.db"&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;private&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;static&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;final&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;String&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;LIST_TABLE_CREATE&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;=&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc6"&gt;"CREATE TABLE LIST (ID INTEGER PRIMARY KEY AUTOINCREMENT, SERVER_ID INTEGER, NAME TEXT, DESCRIPTION TEXT);"&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;private&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;static&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;final&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;String&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;ITEM_TABLE_CREATE&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;=&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc6"&gt;"CREATE TABLE ITEM (ID INTEGER PRIMARY KEY AUTOINCREMENT, SERVER_ID INTEGER, NAME TEXT, LIST_ID INTEGER NOT NULL, NEEDED BOOLEAN, UPDATED_DATE TEXT, UPDATED_BY TEXT);"&lt;/span&gt;&lt;span class="sc10"&gt;;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;public&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;FamilyListOpenHelper&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;Context&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;context&lt;/span&gt;&lt;span class="sc10"&gt;)&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;{&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc5"&gt;this&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;context&lt;/span&gt;&lt;span class="sc10"&gt;,&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc5"&gt;null&lt;/span&gt;&lt;span class="sc10"&gt;);&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc10"&gt;}&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;public&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;FamilyListOpenHelper&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;Context&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;context&lt;/span&gt;&lt;span class="sc10"&gt;,&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;CursorFactory&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;factory&lt;/span&gt;&lt;span class="sc10"&gt;)&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;{&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc5"&gt;super&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;context&lt;/span&gt;&lt;span class="sc10"&gt;,&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;FAMILY_LIST_TABLE_NAME&lt;/span&gt;&lt;span class="sc10"&gt;,&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;factory&lt;/span&gt;&lt;span class="sc10"&gt;,&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;DATABASE_VERSION&lt;/span&gt;&lt;span class="sc10"&gt;);;&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc10"&gt;}&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc11"&gt;@Override&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;public&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;void&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;onCreate&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;SQLiteDatabase&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;db&lt;/span&gt;&lt;span class="sc10"&gt;)&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;{&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc11"&gt;db&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;execSQL&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;LIST_TABLE_CREATE&lt;/span&gt;&lt;span class="sc10"&gt;);&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc11"&gt;db&lt;/span&gt;&lt;span class="sc10"&gt;.&lt;/span&gt;&lt;span class="sc11"&gt;execSQL&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;ITEM_TABLE_CREATE&lt;/span&gt;&lt;span class="sc10"&gt;);&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc10"&gt;}&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc11"&gt;@Override&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc16"&gt;public&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;void&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;onUpgrade&lt;/span&gt;&lt;span class="sc10"&gt;(&lt;/span&gt;&lt;span class="sc11"&gt;SQLiteDatabase&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;db&lt;/span&gt;&lt;span class="sc10"&gt;,&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;int&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;oldVersion&lt;/span&gt;&lt;span class="sc10"&gt;,&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc16"&gt;int&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc11"&gt;newVersion&lt;/span&gt;&lt;span class="sc10"&gt;)&lt;/span&gt;&lt;span class="sc0"&gt; &lt;/span&gt;&lt;span class="sc10"&gt;{&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc2"&gt;// TODO Auto-generated method stub&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc10"&gt;}&lt;/span&gt;&lt;span class="sc0"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span class="sc10"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span class="sc10"&gt;So what is this code doing? First, I've defined which version of the DB this is:&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;DATABASE_VERSION = 1;&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;I like this feature of Android.  They have built-in support for upgrading.  You'll notice, I have not implemented the onUpgrade method, that is because I have no need for it yet. But when I come out with version 2.0, I will have the opportunity to perform upgrade operations.&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;Next, I define the table names and create SQL for the List and Item tables.  &lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;private static final String LIST_TABLE_CREATE = "CREATE TABLE LIST (ID INTEGER PRIMARY KEY AUTOINCREMENT, SERVER_ID INTEGER, NAME TEXT, DESCRIPTION TEXT);"; private static final String ITEM_TABLE_CREATE = "CREATE TABLE ITEM (ID INTEGER PRIMARY KEY AUTOINCREMENT, SERVER_ID INTEGER, NAME TEXT, LIST_ID INTEGER NOT NULL, NEEDED BOOLEAN, UPDATED_DATE TEXT, UPDATED_BY TEXT);";&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;Finally, I've implemented a constructor that will always open the Family List Database and return it to the caller.  That's it.  And that is pretty easy my friends.  Of course, I'm not making this available as a service to other applications, which is slightly more complex.  &lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="sc10"&gt;Next, I'll implement some querying and saving.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3060516345583304229-8044225507707392199?l=benarchie.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/z6xk5OOTxGQTojkSVjh7m3bJnhI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/z6xk5OOTxGQTojkSVjh7m3bJnhI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/z6xk5OOTxGQTojkSVjh7m3bJnhI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/z6xk5OOTxGQTojkSVjh7m3bJnhI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BenArciszewskiDevelopment/~4/A11meJahsuM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://benarchie.blogspot.com/feeds/8044225507707392199/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://benarchie.blogspot.com/2010/05/android-application-part-6-sqllite.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/8044225507707392199?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3060516345583304229/posts/default/8044225507707392199?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BenArciszewskiDevelopment/~3/A11meJahsuM/android-application-part-6-sqllite.html" title="Android Application Part 6 - SQLLite Database" /><author><name>Benjamin Arciszewski</name><uri>https://profiles.google.com/111171223607920359187</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh4.googleusercontent.com/-MKgePp1Tz9Y/AAAAAAAAAAI/AAAAAAAAAAA/ZcilClwWDGc/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://benarchie.blogspot.com/2010/05/android-application-part-6-sqllite.html</feedburner:origLink></entry></feed>

