<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8145593511362203982</id><updated>2025-09-23T16:21:16.509-05:00</updated><title type='text'>PhilCali Code v0.1</title><subtitle type='html'>Trying something new.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>25</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-148549535946758328</id><published>2011-05-10T12:25:00.002-05:00</published><updated>2011-05-10T14:56:27.398-05:00</updated><title type='text'>Do You Speak Cronish?</title><content type='html'>&lt;p&gt;A while back, I spoke about writing a &lt;a href=&quot;http://philcalicode.blogspot.com/2010/10/dsl-magic.html&quot;&gt;cron dsl&lt;/a&gt;. Well, some progress
has been made.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;h2&gt;Invocation&lt;/h2&gt;

&lt;p&gt;How do you talk about cron? Do you say that a job runs &lt;em&gt;every day at midnight&lt;/em&gt;?
Well if you do, then you need no further instructions on how to create
your crons programmatically. Some example of some crons:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: I should have run the text in the console before output wrong data.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;&quot;Every day at midnight&quot;.crons == &quot;0 0 * * *&quot;
&quot;Every 14 days at midnight&quot;.crons == &quot;0 0 */14 * *&quot;
&quot;Every day on the weekday in June, July, and August at 3:30&quot;.crons == &quot;30 3 * 6,7,8 1-5&quot;
&lt;/pre&gt;

&lt;p&gt;We all speak cronish to some degree!&lt;/p&gt;

&lt;h2&gt;The Syntax&lt;/h2&gt;

&lt;p&gt;One night, I decided to break down a cron statement into some determinable blocks.
I came up with the following &lt;a href=&quot;https://github.com/philcali/cronish/blob/master/language.md&quot;&gt;grammar rules&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Incrementing statements&lt;/li&gt;
&lt;li&gt;Connectors&lt;/li&gt;
&lt;li&gt;Values / Descriptors&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Incrementing Statements&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Incrementals&lt;/em&gt; is the keyword &lt;em&gt;every&lt;/em&gt;. This produces the famous *. Any numeric value
you punch in after &lt;em&gt;every&lt;/em&gt; makes it one of these: &lt;em&gt;*/n&lt;/em&gt;. A cron statement &lt;em&gt;must&lt;/em&gt; start
with one of these. Therefore, the shortest incremental time you can do, is something like:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// second, minute, hour, day of month, month, day of week, year
&quot;Every second&quot;.cron == Cron(&quot;*&quot;, &quot;*&quot;, &quot;*&quot;, &quot;*&quot;, &quot;*&quot;, &quot;*&quot;, &quot;*&quot;)
&lt;/pre&gt;

&lt;h3&gt;Connectors&lt;/h3&gt;

&lt;p&gt;The connectors I&#39;ve chosen help make an English sentence regarding a cron job clear.
Here&#39;s the run down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;at&lt;/code&gt; refers only to time, which includes seconds, minutes, and hours. ie: &lt;em&gt;at 4am and 4pm&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;on&lt;/code&gt; refers to day values, particularly day of week values. ie: &lt;em&gt;on Friday, Saturday, and Sunday&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;in&lt;/code&gt; refers to month values. ie: &lt;em&gt;in January to May&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;on the nth&lt;/code&gt; refers to day of month values. ie: &lt;em&gt;on the 1st, 2nd, 3rd, and 4th day&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;in the year n&lt;/code&gt; is special syntax for a year value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Connectors can be strung together with other connectors or incrementals. Some examples would be:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;&quot;Every day at midnight in every month&quot;.crons ==
&quot;Every month at midnight on every day&quot;.crons

&quot;every year in July on the weekend at 1pm-4pm&quot;.crons ==
&quot;every day on the weekend at 1pm-4pm in July&quot;.crons
&lt;/pre&gt;

&lt;p&gt;Just follow the simple rules, and it&#39;s pretty easy to build your time.&lt;/p&gt;

&lt;h3&gt;Values / Descriptors&lt;/h3&gt;

&lt;p&gt;Descriptors are just more keywords we use in English to describe something. These are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;midnight&lt;/code&gt; and &lt;code&gt;noon&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n:n:n&lt;/code&gt; for full clock description.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;other&lt;/code&gt; in the incrementals instead of &lt;code&gt;2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;am&lt;/code&gt; and &lt;code&gt;pm&lt;/code&gt; for hours.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;st&lt;/code&gt;, &lt;code&gt;nd&lt;/code&gt;, &lt;code&gt;rd&lt;/code&gt; and &lt;code&gt;th&lt;/code&gt; for days in the month.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;weekend&lt;/code&gt; and &lt;code&gt;weekday&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;last&lt;/code&gt; applies for day of week and day of month.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The values are either numeric or English values for fields. Days are Monday - Sunday,
months are January - December, and day of month are simply numbers. &lt;/p&gt;

&lt;h2&gt;Scala Cron Job&lt;/h2&gt;

&lt;p&gt;Describing a cron job in pure Scala is also done through dsl type speak.
The syntax is heavily borrowed from sbt&#39;s task creation.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;val payroll = task {
  println(&quot;Getting paid today ... with virtual cash!&quot;)
} executes &quot;every Friday on the last day in every month&quot;

// And we&#39;re off!
// Alternatively you could have written it like this

val visitation = task {
  println(&quot;Greetings, my name is Mouth Mouthman.&quot;)
}

visitation executes &quot;every Wednesday at 6pm&quot;
visitation executes &quot;every Sunday at 3pm&quot;

// Kill a job with stop
payroll stop // Oh noes!
&lt;/pre&gt;

&lt;h2&gt;Where do we go from here?&lt;/h2&gt;

&lt;p&gt;There&#39;s a lot of work that still needs to happen before this could be released.
&lt;a href=&quot;https://github.com/philcali/cronish&quot;&gt;Cronish&lt;/a&gt; is freely available at my github. I&#39;m working towards a stable
release.&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/148549535946758328/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/05/do-you-speak-cronish.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/148549535946758328'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/148549535946758328'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/05/do-you-speak-cronish.html' title='Do You Speak Cronish?'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-1483952900899311915</id><published>2011-04-12T21:26:00.000-05:00</published><updated>2011-04-12T21:26:58.875-05:00</updated><title type='text'>99 Problems</title><content type='html'>&lt;p&gt;I ran across an awesome blog post today titled &lt;a href=&quot;http://blog.tbueno.com/2011/04/12/programming_problems_to_improve_your_language_skills.html&quot;&gt;Programming Problems To Improve Your Language Skills&lt;/a&gt;.
I never actually wrote anything in Haskell yet, but I thought that solving the &lt;a href=&quot;http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html&quot;&gt;Ninety-Nine Lisp Problems&lt;/a&gt;
was a great way to start. That&#39;s the point of this post. There is, however, a goofy Scala
snippet that&#39;s in order first.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;h2&gt;Scala Snippet&lt;/h2&gt;

&lt;p&gt;It&#39;s a funny thing, logical operators. In php and python, if you want to test that &lt;code&gt;this&lt;/code&gt;
something is true and &lt;code&gt;that&lt;/code&gt; something is true, you&#39;d simply write &lt;code&gt;if this and that&lt;/code&gt;.
Very straight forward. Those of us coming from C understand that &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; represent
&lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt; respectively. I never actually questioned it... that&#39;s just the way it was.&lt;/p&gt;

&lt;p&gt;My wife is taking a beginner CSC class at university, and when she saw &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, she simply read
it as &lt;em&gt;and-and&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;What&#39;s does &quot;and-and&quot; mean, and I never seen those vertical things before...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A very natural response. In Scala, we can change this! &lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;class BooleanExpression(origin: Boolean) {
  def and (expr: =&amp;gt; Boolean) = origin &amp;amp;&amp;amp; expr
  def or (expr: =&amp;gt; Boolean) = origin || expr
}

implicit def boolean2BooleanX(some: Boolean) =
  new BooleanExpression(some)
&lt;/pre&gt;

&lt;p&gt;Now your conditional sound very natural:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;if (file.exists and something)
  println(&quot;Yeeeeee&quot;)

val x = 5
if(x &amp;gt; 4 and (x &amp;lt; 10 or x &amp;gt; 100))
  println(&quot;We got it anyway&quot;)
&lt;/pre&gt;

&lt;p&gt;It is important to note that this is less efficient. It is, however, cool.&lt;/p&gt;

&lt;h2&gt;Haskell Problems&lt;/h2&gt;

&lt;p&gt;And here&#39;s the first 20 problems, almost. I skipped a couple, but I plan 
on going back to it. I tried not to use &lt;code&gt;Prelude&lt;/code&gt; with all the pre-defined
list functions, but it still doesn&#39;t hurt when the goal is to learn a
language.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;-- problem 1
my_last :: [a] -&amp;gt; a
my_last (x:[]) = x
my_last (x:xs) = my_last xs

-- problem 2
my_but_last :: [a] -&amp;gt; [a]
my_but_last (x:y:[]) = [x, y]
my_but_last (x:rest) = my_but_last rest

-- problem 3
element_at :: [a] -&amp;gt; Int -&amp;gt; a
element_at xs i = head [y | (x, y) &amp;lt;- zip [0..] xs, x == i]

-- problem 4
count :: [a] -&amp;gt; Int
count xs = foldl (\acc x -&amp;gt; acc + 1) 0 xs

-- problem 5
rev :: [a] -&amp;gt; [a]
rev [] = []
rev (x:xs) = rev xs ++ [x]

-- problem 6
palindrome :: (Eq a) =&amp;gt; [a] -&amp;gt; Bool
palindrome xs = if(rev xs == xs) then True else False

-- problem 7

-- problem 8
compress :: (Eq a) =&amp;gt; [a] -&amp;gt; [a]
compress [] = []
compress (x:xs) = [x] ++ compress(dropWhile(== x) xs)

-- problem 9
pack :: (Eq a) =&amp;gt; [a] -&amp;gt; [[a]]
pack [] = []
pack (x:xs) = [[x] ++ takeWhile (== x) xs] ++ pack(dropWhile(== x) xs)

-- problem 10
encode :: (Eq a) =&amp;gt; [a] -&amp;gt; [(Int,a)]
encode xs = [(count ls, head ls) | ls &amp;lt;- pack xs]

-- problem 11

-- problem 12
decode :: (Eq a) =&amp;gt; [(Int, a)] -&amp;gt; [a]
decode xs = concat . map (\el -&amp;gt; let (times, i) = el in repl i times) xs

-- problem 13

-- problem 14
dupe :: [a] -&amp;gt; [a]
dupe [] = []
dupe (x:xs) = x : x : dupe xs

-- problem 15
repl :: [a] -&amp;gt; Int -&amp;gt; [a]
repl [] _ = []
repl (x:xs) n = map (\_ -&amp;gt; x) [1..n] ++ repl xs n

-- problem 16
dropr :: [a] -&amp;gt; Int -&amp;gt;[a]
dropr xs i 
  | count xs &amp;lt; i = xs
  | otherwise = let (headr, (_:rest)) = splitAt (i - 1) xs in headr ++ dropr rest i

-- problem 17
split :: [a] -&amp;gt; Int -&amp;gt; ([a], [a])
split xs n
  | n == 0 = ([], xs)
  | count xs &amp;lt; n = (xs, [])
  | otherwise = (take n xs, drop n xs)

-- problem 18
slice :: [a] -&amp;gt; Int -&amp;gt; Int -&amp;gt; [a]
slice xs from to
  | from == 1 &amp;amp;&amp;amp; to == count xs = xs
  | from &amp;gt; to = []
  | otherwise = [x | (index, x) &amp;lt;- zip [1..] xs, index &amp;gt;= from &amp;amp;&amp;amp; index &amp;lt;= to]

-- problem 19
rotate :: [a] -&amp;gt; Int -&amp;gt; [a]
rotate xs n = let index = if n &amp;lt; 0 then count xs + n else n
                  (first, second) = split xs index 
              in second ++ first

-- problem 20
remove :: [a] -&amp;gt; Int -&amp;gt; [a]
remove xs n = [x | (i, x) &amp;lt;- zip [1..] xs, i /= n]
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/1483952900899311915/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/04/99-problems.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/1483952900899311915'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/1483952900899311915'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/04/99-problems.html' title='99 Problems'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-6631062188449647957</id><published>2011-04-01T16:17:00.000-05:00</published><updated>2011-06-17T06:27:06.806-05:00</updated><title type='text'>Had it All Wrong</title><content type='html'>&lt;div&gt;&lt;p&gt;Development comes in spurts. Even though I&#39;m in the middle of several
personal projects, sometimes a simple change in pace really helps.
I took a break from coding to dive in some &lt;a href=&quot;http://www.haskell.org/haskellwiki/Introduction&quot;&gt;Haskell&lt;/a&gt; (some break). I ran across
an online book, published by Miran Lipovaca, that looked fun and interesting
enough to learn the language: &lt;a href=&quot;http://learnyouahaskell.com/&quot;&gt;Learn You a Haskell for Great Good&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I read through five chapters last night, only to discover that I
&lt;em&gt;should&lt;/em&gt; have learned Haskell before learning Scala. I now see how
much concepts Scala has pulled from Haskell. My opinions on the 
experience thusfar will be expounded on after the jump.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;h2&gt;For the Great Good, Indeed&lt;/h2&gt;

&lt;p&gt;The more I understand about Haskell, the more I love it. Due to my time
learning Scala, I feel very at home with the language. The point of
this post is to observe similarities with the two, as I go about learning.&lt;/p&gt;

&lt;p&gt;The last chapter I read was about recursion, something I understand
very well at this point. As Miran describes, the poster child for Haskell
is the quicksort implementation.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;quicksort :: (Ord a) =&amp;gt; [a] -&amp;gt; [a]  
quicksort [] = []  
quicksort (x:xs) =   
    let smallerSorted = quicksort [a | a &amp;lt;- xs, a &amp;lt;= x]  
        biggerSorted = quicksort [a | a &amp;lt;- xs, a &amp;gt; x]  
    in  smallerSorted ++ [x] ++ biggerSorted
&lt;/pre&gt;

&lt;p&gt;Now a side by side Scala implementation...&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Need this for Haskell like Ord type
implicit Ordered._

def quicksort[A: Ordering](ls: List[A]): List[A] = ls match {
  case Nil =&amp;gt; Nil
  case x :: xs =&amp;gt;
    val (smaller, bigger) = xs partition(_ &amp;lt;= x)
    quicksort(smaller) ++ List(x) ++ quicksort(bigger)
}
&lt;/pre&gt;

&lt;h2&gt;The Breakdown&lt;/h2&gt;

&lt;p&gt;Time for a line by line comparison. &lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Haskell
quicksort :: (Ord a) =&amp;gt; [a] -&amp;gt; [a]  
// Scala
def quicksort[A: Ordering](ls: List[A]): List[A]
&lt;/pre&gt;

&lt;p&gt;The first line is Haskell&#39;s way of defining a function. &lt;code&gt;quicksort&lt;/code&gt; takes a single
parameter, which is a list of type &lt;code&gt;a&lt;/code&gt; defined to be &lt;code&gt;Ord&lt;/code&gt;. It will return a list
that is type &lt;code&gt;a&lt;/code&gt;. More specifically, it will return a sorted one.&lt;/p&gt;

&lt;p&gt;The Scala implementation does just as well.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Haskell
quicksort [] = []  
// Scala
ls match {
  case Nil =&amp;gt; Nil
&lt;/pre&gt;

&lt;p&gt;The seeming redefinition of &lt;code&gt;quicksort&lt;/code&gt; is simply constructing a pattern match.
That bit of Haskell specifically states: &lt;em&gt;Given an empty list, an empty list will be returned&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In Scala, you have to be a bit more explicit in your declaration of a pattern match,
initiated by the keyword &lt;code&gt;match&lt;/code&gt;. A &lt;code&gt;match&lt;/code&gt; expression will return something (in this case,
we&#39;ve explicitly defined its type as a &lt;code&gt;List[A]&lt;/code&gt;, so we better do that). Staying true
by our &lt;em&gt;line&lt;/em&gt; by line analysis, should &lt;code&gt;ls&lt;/code&gt; be &lt;code&gt;Nil&lt;/code&gt; (or empty), return empty.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Haskell
quicksort (x:xs) =   
// Scala
  case x :: xs =&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Should the input parameter be a non-empty list, extract the head of the list from
its remainder, and do some work. A singleton list will be extracted thusly:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;x:[]
&lt;/pre&gt;

&lt;p&gt;Scala supports pattern matching and extraction in its pattern matching very similarly.
If a Scala object defines an &lt;code&gt;unapply&lt;/code&gt; method then you can take advantage of this behavior.
Once again, we see how Scala caters to this way of thinking.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Haskell
    let smallerSorted = quicksort [a | a &amp;lt;- xs, a &amp;lt;= x]  
        biggerSorted = quicksort [a | a &amp;lt;- xs, a &amp;gt; x]  
// Scala
    val (smaller, bigger) = xs partition(_ &amp;lt;= x)
// Optional rewritten like so
    val smaller = quicksort(xs.filter (_ &amp;lt;= x))
    val bigger = quicksort(xs.filter (_ &amp;gt; x))
// Scala for comprehensions
    val smaller = quicksort(for(a &amp;lt;- xs; if a &amp;lt;= x) yield(a))
    val bigger = quicksort(for(a &amp;lt;- xs; if a &amp;gt; x) yield(a))
&lt;/pre&gt;

&lt;p&gt;Haskell is binding some variables to use in its &lt;code&gt;in&lt;/code&gt; statement which we&#39;ll go into
in a minute. Haskell has incredible list comprehensions. You can filter,
pattern match, use &lt;code&gt;let&lt;/code&gt; expressions, etc.&lt;/p&gt;

&lt;p&gt;Scala&#39;s answer to Haskell&#39;s list comprehensions, is the &lt;code&gt;for&lt;/code&gt; comprehension. For this
example, though, it&#39;s a bit overkill. &lt;code&gt;List&lt;/code&gt;&#39;s in Scala inherit from a &lt;code&gt;Traversable&lt;/code&gt; type
which comes with powerful operations: &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;zip&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, to name a few.&lt;/p&gt;

&lt;p&gt;We&#39;re almost done.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Haskell
    in  smallerSorted ++ [x] ++ biggerSorted
// Scala
    quicksort(smaller) ++ List(x) ++ quicksort(bigger)
// Or other implementations above
    smaller ++ List(x) ++ bigger
&lt;/pre&gt;

&lt;p&gt;Haskell will now use the bindings set before and work. Haskell will do its
work lazily, which is awesome.&lt;/p&gt;

&lt;p&gt;Scala list concatenation with other &lt;code&gt;List&lt;/code&gt;s is done exactly the same way
as Haskell. &lt;/p&gt;

&lt;p&gt;Pretty cool. I&#39;m looking forward to learning more.&lt;/p&gt;

&lt;p&gt;-- Philip Cali &lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/6631062188449647957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/04/had-it-all-wrong.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6631062188449647957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6631062188449647957'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/04/had-it-all-wrong.html' title='Had it All Wrong'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-8771014126414659393</id><published>2011-03-26T21:07:00.000-05:00</published><updated>2011-03-26T21:07:17.014-05:00</updated><title type='text'>Dropbox Documentation Server</title><content type='html'>&lt;p&gt;What does this even mean? Well, it means I&#39;m really cheap, and lazy. It
probably means I&#39;m silly, too, when I tell you what I&#39;m doing.&lt;/p&gt;

&lt;p&gt;First, a back story is in order. I was reading some internet articles
the other day, when completely irrelevantly, I thought of making my
&lt;a href=&quot;http://dl.dropbox.com/u/1030/index.html&quot;&gt;Public Dropbox&lt;/a&gt; folder a static web site. Dropbox has a really neat
desktop tool that syncs a folder on your machine to their cloud storage, 
&lt;em&gt;automagically&lt;/em&gt;. Being that the site would be static, I started to
wonder what I could even do with it. Then I realized: this is perfect for
a documentation server! (Maybe not!)&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;h2&gt;Step 1: Getting the Docs&lt;/h2&gt;

&lt;p&gt;Sbt is a wonderful build tool. It can generate really nice API documentation
with a single command. The only thing about these docs, is that they&#39;re not
pretty to look at. &lt;a href=&quot;http://jashkenas.github.com/docco/&quot;&gt;Docco&lt;/a&gt; on the other hand, produces some really nice
looking documentation. The &lt;a href=&quot;http://circumflex.ru/api/2.0/circumflex-docco/src/main/scala/docco.scala.html&quot;&gt;circumflex crew&lt;/a&gt; already ported their re-imagination
of docco style documentation for scala. &lt;/p&gt;

&lt;p&gt;I made quick work into building a sbt plugin that simply wraps the circumflex 
batch docco functionality with a single &lt;code&gt;docco&lt;/code&gt; action within sbt.&lt;/p&gt;

&lt;h2&gt;Step 2: the Plugin&lt;/h2&gt;

&lt;p&gt;Like I mentioned before, all this plugin (initially) would need to do, is
piggy back off the circumflex docco project to do batch docco processing on
the project&#39;s &lt;code&gt;src&lt;/code&gt; directory. Enter &lt;a href=&quot;https://github.com/philcali/sbt-cx-docco&quot;&gt;sbt-cx-docco&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In a developer&#39;s project definition, they can route the docco output to a specific
folder. Naturally, for this experiement, I routed it to my Public Dropbox folder on
my local hard drive.&lt;/p&gt;

&lt;p&gt;I am thinking more about this, and I have more plans for this plugin that involves
severing my circumflex dependency. You may or may not see about this in the future.&lt;/p&gt;

&lt;h2&gt;Step 3: ???&lt;/h2&gt;

&lt;p&gt;I have no idea what else I have planned for this thing. I&#39;m thinking about setting
a monido that looks at the folder, and adds the project to the list for me. This depends
on how lazy I get, I guess. &lt;/p&gt;

&lt;h2&gt;Step 4: Profit!&lt;/h2&gt;

&lt;p&gt;A free static web server. Pretty good, Dropbox... Pretty good.&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/8771014126414659393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/03/dropbox-documentation-server.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/8771014126414659393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/8771014126414659393'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/03/dropbox-documentation-server.html' title='Dropbox Documentation Server'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-7670759284031231053</id><published>2011-03-15T11:14:00.000-05:00</published><updated>2011-03-15T11:14:35.761-05:00</updated><title type='text'>Monido, What?</title><content type='html'>&lt;p&gt;I&#39;ve been working on a little project called &lt;a href=&quot;https://github.com/philcali/monido&quot;&gt;Monido&lt;/a&gt;, which is 
simply a monitoring service for Scala. It can monitor anything from
a file, a directory, a DB table, or web page. I think you get the
picture.&lt;/p&gt;

&lt;p&gt;The project, however, only ships with a single reference implementation to
monitor the file system for changes. I&#39;m using it for this very post at this
moment. More on that later.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;h2&gt;The Program&lt;/h2&gt;

&lt;p&gt;I explained in an &lt;a href=&quot;http://philcalicode.blogspot.com/2010/09/moved.html&quot;&gt;earlier post&lt;/a&gt; that I write my blog entries in markdown,
and store them in a git repo. I have since modified that &lt;code&gt;BloggerBot&lt;/code&gt; script
to only inject the html source in my clipboard when it runs. The command looks like:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;java -jar blogbot.jar ~/notes/posts/24.md
&lt;/pre&gt;

&lt;p&gt;Monido can automate this procedure:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;monido ~/notes/posts -e java -jar blogbot.jar
&lt;/pre&gt;

&lt;p&gt;Every time I save this post, I get:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;Writing conversion to your clipboard ....
Done.
&lt;/pre&gt;

&lt;p&gt;This saves me a few key strokes!&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;-e&lt;/code&gt; argument for monido is essentially like using the &lt;code&gt;pipe&lt;/code&gt; for a Unix system.
The full file path of the changed file is passed to the terminal argument, and immediately
run. Monido defines itself as a pure JVM, cross-platform solution to file monitoring.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;monido ~/some/other/thing -e ls -l
&lt;/pre&gt;

&lt;p&gt;Monido can spawn recursive monitors, although this practice is discouraged on using a
large directory tree. Your operating system does this for you, so you should use that instead.
&lt;a href=&quot;http://jnotify.sourceforge.net/&quot;&gt;Jnotify&lt;/a&gt; is a good Java wrapper around these OS events. &lt;/p&gt;

&lt;p&gt;Monido is &lt;em&gt;not&lt;/em&gt; just a file monitoring service, though! This is where the good part begins.&lt;/p&gt;

&lt;h2&gt;The Library&lt;/h2&gt;

&lt;p&gt;Monido&#39;s infrastructure is very flexible, being that it&#39;s comprised of two (optionally three),
components. These components are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;PulsingComponent&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MonitorComponent&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ListeningComponent&lt;/code&gt; (optional)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The explanation of these components are given on the &lt;a href=&quot;https://github.com/philcali/monido&quot;&gt;Monido&lt;/a&gt; github page:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A &lt;code&gt;PulsatingComponent&lt;/code&gt; is something that wakes the MonitorComponent to do something. 
  A &lt;code&gt;PulsatingComponent&lt;/code&gt; could wake it at set intervals, once a day, manually, etc. 
  The &lt;code&gt;MonitorComponent&lt;/code&gt; will simply monitor whatever it was told to monitor (The 
  &lt;code&gt;FileMonido&lt;/code&gt; reference implementation monitors the file system.) Optionally, the 
  Monitor can notify a client of a change (or anything else really) by making use of 
  the &lt;code&gt;ListeningComponent&lt;/code&gt;.&lt;/p&gt;
  
  &lt;p&gt;Lots of moving parts that have arbitrary dependencies make it a great candidate for some DI.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, I will show you how to do a &lt;code&gt;DBMonido&lt;/code&gt;, just for fun.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import com.github.philcali.monido._

// First we need to define the DBMonitor by extending the
// MonitorComponent.
trait DBMonitorComponent extends MonitorComponent {
  this: ListeningComponent[Adapter] =&amp;gt;
  // Let is connect via a db string
  class DBMonitor(db: String) extends SimpleMonitorDevice{
    def connect: Adapter = // connect to db
    def pulsed {
      // Making a fictional query
      val results = connect.query(&quot;SELECT COUNT(id) as count from entries&quot;)
      if(results(&quot;count&quot;) &amp;gt;= 9000) listener.changed(connect)
    }
  }
}

// Define Monido now
object DBMonido extends Monido with ListeningComponent[Adapter]
                               with DBMonitorComponent 
                               with PulsatingComponentImpl {

  // This object will NOT compile until dependencies are met
  val listener = new MonidoListener {
    def changed(db: Adapter) {
      // Clear records
      db.query(&quot;TRUNCATE entries&quot;)
    }
  }

  // the MonitorComponent expects a monitor value to be defined
  val monitor = new DBMonitor(&quot;jdbc:sqlite:test.db&quot;)
  // Next the PulsatingComponent expects a pulsar value to be defined
  // Sneal peak at 0.2 Scalendar syntax
  import com.github.philcali.scalendar._
  val pulsar = new Pulsar(1.day.inMillis)
}

DBMonido.start
// And we&#39;re off!
&lt;/pre&gt;

&lt;p&gt;This is a pretty silly example of checking a record once a day for the change you&#39;re
looking for, but now I&#39;ll show you how easy it is to configure the Monido. Say now
I wanted to use a different pulsating implementation. I&#39;ll use a fictitious 
&lt;code&gt;CronPulsing&lt;/code&gt; implementation that uses a cron string for its configuration.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Re-define the DB monido
object DBMonido extends Monido with ListeningComponent[Adapter]
                               with DBMonitorComponent 
                               with CronPulsing {
  ...
  // same as before. The monitoring component worked great, so
  // leave it alone.

  // Sneak peak at the cron project
  import com.github.philcali.cron._
  val pulsar = new CronPulsar(&quot;every Sunday at midnight&quot;.cron)
}

DBMonido.start
// And we&#39;re off!
&lt;/pre&gt;

&lt;p&gt;So there&#39;s your &lt;code&gt;Monido&lt;/code&gt;. I don&#39;t know how useful it could be to everyone
else, but I find myself in need of &lt;code&gt;Monido&lt;/code&gt;&#39;s pretty often.&lt;/p&gt;

&lt;p&gt;-- Philip &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/7670759284031231053/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/03/monido-what.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7670759284031231053'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7670759284031231053'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/03/monido-what.html' title='Monido, What?'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-2401660592468791265</id><published>2011-02-23T16:49:00.001-06:00</published><updated>2011-03-01T20:31:20.583-06:00</updated><title type='text'>Scalendar v 1.0</title><content type='html'>&lt;h3&gt;Update&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;It is now hosted on both &lt;a href=&quot;https://github.com/philcali/scalendar&quot;&gt;scalendar&lt;/a&gt;&#39;s github page, and on &lt;a href=&quot;http://scala-tools.org/&quot;&gt;scala-tools&lt;/a&gt;. It was approved a couple of days ago, and hopefully, other people might find it useful.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As I worked on my wife&#39;s ipad app, I kept refining my Scala Calendar wrapper
(which I coined Scalendar thanks to &lt;a href=&quot;http://www.linkedin.com/pub/brad-cupit/20/661/62&quot;&gt;brad&lt;/a&gt;). It began to
evolve into a really nice api, in which you work with immutable objects that
wrap the Java Calendar api. While it completely interoperates with Java time,
it works well enough as a stand-alone library.&lt;/p&gt;

&lt;p&gt;The main functionality like date traversal and property getters haven&#39;t changed
from the &lt;a href=&quot;http://philcalicode.blogspot.com/2011/02/manipulating-time.html&quot;&gt;last post&lt;/a&gt;. If you&#39;re interested, read more about the changes after the
jump.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;h2&gt;Construction&lt;/h2&gt;

&lt;p&gt;My biggest issue with the version I was working with, was the ability to &lt;em&gt;make&lt;/em&gt;
time. As I began to use it throughout the app, I began to realize rather quickly
that it was indeed the biggest issue.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Now to convert from strings, you need to define
// a SimpleDateFormat, or just use the Pattern object

import com.philipcali.utils.calendar._

// If you don&#39;t plan on converting strings
// to time, then simply import
// import com.philipcali.utils.Scalendar

// A global implicit will do
implicit val pattern = Pattern(&quot;M-d-yyyy&quot;)

val monthsAlive = (&quot;11-19-1985&quot; to Scalendar.now).delta.months

// Creation from now
val now = Scalendar.now

// &quot;Setters&quot;
// It completely integrates with java.util.Calendar
import java.util.Calendar._

val endOfWorld = now.year(2012).month(DECEMBER).day(21)

val countdown = endOfWorld to now

&quot;t %d days&quot; format(countdown.delta.days) // t -667 days

&quot;There&#39;s only %d months left!&quot; format(countdown.reverse.delta.months)

// Optional, the &quot;make time&quot; syntax:
val made = Scalendar(year = 2012, month = DECEMBER, day = 21)
val madeMore = Scalendar(year = 2012, 
                         month = DECEMBER, day = 21, hour = 23)

made &amp;lt; endOfWorld // true, made 0&#39;s out anything below the day
madeMore &amp;lt; endOfWorld // false, because hour was set

// Of course time can be built with unix timestamp
val fromLong = Scalendar(Scalendar.now.time)

// Below is an eample of interaction with a DB and
// formatting the results via a duration traversal
val duration = Scalendar.now to Scalendar.now + (3 months)

// Fictional DBO querying
val events = Event.find (
  Event.userid === &quot;philip.cali&quot;,
  Event.created &amp;gt; duration.start.time,
  Event.created &amp;lt; duration.end.time  
)

// Format events for each month
duration.traverse(1 month) { monthD =&amp;gt; 
  // It&#39;s entirely possible to get more granular
  // but that&#39;s overkill for this example
  println(&quot;Events in %s&quot; format(monthD.month.name))
  events.filter(e =&amp;gt; monthD contains(e.created)) foreach(println)
}
&lt;/pre&gt;

&lt;p&gt;I got rid of the hasNext method, as that typically requires state,
and the Scalendar class is completely immutable. I&#39;ve made use of 
Scala 2.8 &lt;a href=&quot;http://www.scala-lang.org/docu/files/packageobjects/packageobjects.html&quot;&gt;package objects&lt;/a&gt;, and completely realized how valuable it
was when I wanted to save imports by placing my implicit conversions
in there. Now the clients get a rather robust date api just by importing
Scalendar.&lt;/p&gt;

&lt;p&gt;The code will be on my &lt;a href=&quot;https://github.com/philcali&quot;&gt;github&lt;/a&gt; very soon. I have to rip the api
out of the project I&#39;m currently using it in and create a separate
project. One gave birth to the other.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/2401660592468791265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/02/scalendar-v-10.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/2401660592468791265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/2401660592468791265'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/02/scalendar-v-10.html' title='Scalendar v 1.0'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-7136008388288975252</id><published>2011-02-11T11:05:00.000-06:00</published><updated>2011-02-11T11:05:14.033-06:00</updated><title type='text'>Manipulating time</title><content type='html'>&lt;p&gt;Handling time using the standard Java libraries can be
a real pain. I am writing a webapp that uses pretty
complicated date arithmetic, and while Java&#39;s &lt;a href=&quot;http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html&quot;&gt;Calendar&lt;/a&gt;
object &lt;em&gt;works&lt;/em&gt;, clients of the library are forced to
work with a mutable object, thus being rather verbose
to accomplish a simple task.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;Enter &lt;strong&gt;ScalaCalendar&lt;/strong&gt; to save the day! I began writing
a Scala wrapper for the Java Calendar object, and I&#39;m mostly
happy how it turned out. Here&#39;s an example of how it works:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Importing the base ScalaCalendar object like so
// allows you to do some pretty powerful things.
import com.philipcali.calendar.ScalaCalendar._

// Let&#39;s work with the simple things
val rightNow = java.util.Date() // or createTime() or &quot;now&quot;
val tomorrow = rightNow + 1 day
val nextMonth = rightNow + 1 month
val yesterday = rightNow - 1 day   

// Generate a duration is rather simple too
val span = rightNow to (rightNow + 1 week)

rightNow isIn span // returns true

rightNow isWeekend
rightNow isWeekday

span.delta.days // returns 7
span.delta.months // returns 0

// Calendar fields
// Your standard field have a name method
// which tries to define the field in English
rightNow.time == rightNow.millisecond
rightNow.second.value
rightNow.minute.value
rightNow.hour.value
rightNow.day.value
rightNow.day.inWeek
rightNow.day.inYear
rightNow.week.value
rightNow.week.InYear
rightNow.month.value
rightNow.year.value

// Work with strings
val allFeb = &quot;2/1/2011&quot; to (&quot;3/1/2011&quot; - 1 day)

// This is where it gets good:
// A duration is traversable by any other duration
span.by(1 day) foreach { dayDuration
  println(dayDuration.day.name)
}

// Only work with MWF days
val mwf = span.by(1 day) filter(_.day.name match {
  case &quot;Monday&quot; | &quot;Wednesday&quot; | &quot;Friday&quot; =&amp;gt; true
  case _ =&amp;gt; false
})

// Only weekends
span.by(1 day) filter(_.isWeekend) foreach { time =&amp;gt;
  println(&quot;Partying it up on %s!&quot; format(time.day.name))
}

// Using this logic, building a calendar table is painless
val calHtml = 
&amp;lt;table&amp;gt; {
  &quot;2/1/2011&quot;.calendarMonth.traverse(1 week) { weekD =&amp;gt;
    &amp;lt;tr&amp;gt; {
      weekD.traverse(1 day) { dayD =&amp;gt;
        &amp;lt;td&amp;gt;{ dayD.day.value }&amp;lt;/td&amp;gt;
      }
    }&amp;lt;/tr&amp;gt;
  }
}&amp;lt;/table&amp;gt;

// There&#39;s convenience methods for generating a Calendar month
// week, and day
&quot;2/1/2011&quot;.calendarMonth // contains 1/30/2011 - 3/5/2011
&quot;2/1/2011&quot;.calendarWeek // contains 1/30/2011 - 2/5/2011
&quot;2/1/2011&quot;.calendarDay // contains 00:00:00 - 23:59:59

// You can even traverse a timespan in reverse
val countdown = (&quot;2/28/2011&quot; to &quot;2/1/2011&quot;).by(1 day) map(_.day.value)
countdown.take(5).mkString(&quot;,&quot;) // &quot;28,27,26,25,24&quot;

// Traverse a duration at your own space
val february = &quot;2/1/2011&quot; to &quot;2/1/2011&quot; + (1 month)

while(february.hasNext) {
  february.next(1 day)
  // do something
}

// Construct something wacky
// This gets the weekdays every other week in the month
for(days &amp;lt;- february.by(1 week); if(days.week.value % 2 == 0)
     day &amp;lt;- weeks.by(1 day); if day.isWeekDay) {
  println(day.day)
}
&lt;/pre&gt;

&lt;p&gt;The by method splits the durations into a lists of durations, while the
traverse method splits and applies a function you give to transform
that duration into something meaningful. The ScalaCalendar integrates
with java.util.Date and java.util.Calendar seamlessly.&lt;/p&gt;

&lt;p&gt;Most people will probably look at this and face palm, but it satisfied my
needs for this particular project quite nicely. &lt;/p&gt;

&lt;p&gt;I&#39;m going to release the calendar api on my &lt;a href=&quot;https://github.com/philcali&quot;&gt;github&lt;/a&gt; account very
soon.&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/7136008388288975252/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/02/manipulating-time.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7136008388288975252'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7136008388288975252'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/02/manipulating-time.html' title='Manipulating time'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-6094592654905470336</id><published>2011-01-26T20:34:00.000-06:00</published><updated>2011-01-26T20:34:14.660-06:00</updated><title type='text'>Three Programming Languages</title><content type='html'>&lt;p&gt;I ran across &lt;a href=&quot;http://blog.mathgladiator.com/2010/11/3-programming-languages-you-need-to.html&quot;&gt;this post&lt;/a&gt; a few months ago, that I recently given
some more thought to while making supper. (Funny how the brain works
isn&#39;t it?) I&#39;ve come up with my &quot;three&quot; programming languages as of
this writing, and I&#39;d like to share them with you.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;The author classifies the three programming languages every one must know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Happiness Language&lt;/li&gt;
&lt;li&gt;The Hack-it-out / Get Things Done language&lt;/li&gt;
&lt;li&gt;The Bread and Butter language&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I disagree with the author that there needs/should to be a difference for
all three languages. In my experience, there&#39;s really no difference between
the &lt;strong&gt;Happiness&lt;/strong&gt; and &lt;strong&gt;GTD&lt;/strong&gt; language. To me, if you can quickly solve the
problem with the language in your head, then you can simply type it out and
execute it &lt;em&gt;quickly&lt;/em&gt;, thus getting it done. Anyway...&lt;/p&gt;

&lt;p&gt;I think there are three languages, though:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The language you can quickly solve problems with (call it Happiness, GTD, whatever). 
For me, this is &lt;a href=&quot;http://www.scala-lang.org/&quot;&gt;Scala&lt;/a&gt;. This language makes me happy, and I&#39;ve recently wrote a script
that calculates an average for a dataset for work rather quickly.&lt;/li&gt;
&lt;li&gt;The language you get paid to work in. While I can solve problems the fastest in Scala,
I&#39;m glad there a distinction for me. I get paid to work in PHP, and it makes my stomach turn a little
to define a body of an anonymous function with a string (pre 5.3 code base).
I&#39;m glad my precious Scala is free from the political stress and red tape at my 9 to 5. This
keeps Scala safe and happy.&lt;/li&gt;
&lt;li&gt;The language you desire to learn more about. Having used Scala for more than two years,
I can honestly say I wish I knew Scala more than I do now. I learn something new about the
language on every new personal project I work on, and I feel I&#39;m only starting to scratch
the surface of some greater potential.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So there you have it. I do believe broadening your programming language knowledge 
makes you a more versatile problem solver, &lt;em&gt;to a certain extent&lt;/em&gt;. Ultimately, I think
problem solving skills are sharpened like any other skill set: &lt;em&gt;solving problems&lt;/em&gt;. People just
use learning a programming language as an excuse to tackle a new problem differently.&lt;/p&gt;

&lt;p&gt;On a slightly different note, I laughed aloud when someone commented on aforementioned blog post:
&lt;em&gt;Javascript is your happiness language? Are you a serial killer?&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;I would say that with PHP, as I think looking at variables starting with
the dollar sign is torture to the visual sense. When I program in PHP the goal is to make it
as terse as possible with a billion helper functions to avoid those awful
dollar signs. &lt;/p&gt;

&lt;p&gt;-- Philip Cali &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/6094592654905470336/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/01/three-programming-languages.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6094592654905470336'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6094592654905470336'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/01/three-programming-languages.html' title='Three Programming Languages'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-3377813602148606937</id><published>2011-01-23T21:03:00.000-06:00</published><updated>2011-01-23T21:03:04.427-06:00</updated><title type='text'>Who needs a Template Engine?</title><content type='html'>&lt;p&gt;My wife commissioned me for another web app this weekend, and we spec&#39;d
it out using an app on her iPad (We both had a lot of fun). I wanted to
prototype this sucker as quick as possible, and to be frank, my template
engine I wrote for &lt;a href=&quot;https://github.com/scalatra/scalatra&quot;&gt;Scalatra&lt;/a&gt; wasn&#39;t cutting it. It dawned on me rather
suddenly, something my &lt;a href=&quot;http://www.linkedin.com/pub/brad-cupit/20/661/62&quot;&gt;former co-woker&lt;/a&gt; mentioned to me in passing. &lt;/p&gt;

&lt;p&gt;&quot;Scala supports in-line XML literals... Why not just go with that?&quot;&lt;/p&gt;

&lt;p&gt;And here I am almost a year later, saying to myself:
&lt;em&gt;Huh... you have an excellent point....&lt;/em&gt;&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;Why not, indeed. Using raw Scala &lt;em&gt;as&lt;/em&gt; my template engine, I get some
great benefits right out of the box, which I will elaborate a little later.
First I want to address the big negative aspect.&lt;/p&gt;

&lt;h3&gt;Web Designers would hate me&lt;/h3&gt;

&lt;p&gt;If a web designer (non-Scala coder) had to build an interface using this
method, they would probably shoot me, or want to anyway. The purpose of a
web template is explained quite beautifully &lt;a href=&quot;http://en.wikipedia.org/wiki/Web_template&quot;&gt;here&lt;/a&gt;. And while this new
proposed method achieves this goal, but they&#39;d have to compile Scala code
to see changes. Ouch!&lt;/p&gt;

&lt;p&gt;Let&#39;s take a look at what I mean, though. Let&#39;s just say, a designer was
instructed to format a blog post. All they would have to do is this:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;object BlogPost extends Template[Post] {
  def template(post: Post) = {
    &amp;lt;div class=&quot;post&quot;&amp;gt;
      &amp;lt;div class=&quot;post-header&quot;&amp;gt;
        &amp;lt;h1&amp;gt;{ post.title }&amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div class=&quot;post-body&quot;&amp;gt;
        &amp;lt;p&amp;gt;
          { post.body }
        &amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  }
}
&lt;/pre&gt;

&lt;p&gt;Now in a Scalatra servlet, rendering this template is rather simple:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;get(&quot;/single/:id&quot;) {
  val post = Post.get(params(&quot;id&quot;))
  BlogPost(post)
}
&lt;/pre&gt;

&lt;h3&gt;Scala *is* the Template Engine&lt;/h3&gt;

&lt;p&gt;Why bother building functionality for conditionals and loops, when the language
supports this already. All I had to do was build a simple inheritance system
that makes it easy to wrap templates in templates (for headers and footers and
such). I was amazed at some of the implicit benefits this granted me:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I know if my template is valid &lt;strong&gt;at compile-time&lt;/strong&gt;! Scala won&#39;t even compile
invalid XML literals. Very cool.&lt;/li&gt;
&lt;li&gt;My templates can be &lt;strong&gt;Unit Tested&lt;/strong&gt;! Not sure how useful this is yet, but
it&#39;s possible :P&lt;/li&gt;
&lt;li&gt;I don&#39;t have to learn another web template language! Hoo-ray!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, I&#39;ll spend less time talking about this switch, and more time prototyping
the app.&lt;/p&gt;

&lt;p&gt;Just for kicks, let&#39;s see more of it in action:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;object HomePage extends WrappedTemplate {
  val master = MasterTemplate 

  def wrapped(context: Context) = context match {
    case Context(_, posts: List[Post]) =&amp;gt;
    &amp;lt;div id=&quot;content&quot;&amp;gt;
      &amp;lt;div id=&quot;posts&quot;&amp;gt;
        { posts.map(BlogPost(_)) }
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  }
}

object MasterTemplate extends ParentTemplate {
  // I don&#39;t really like this, but it works for now
  def template(data: (WrappedTemplate, Context)) = data._2 match {
    case Context(title: String, _*) =&amp;gt;
    &amp;lt;html&amp;gt;
      &amp;lt;head&amp;gt;
        &amp;lt;title&amp;gt;{ title }&amp;lt;/title&amp;gt;
      &amp;lt;/head&amp;gt;
      &amp;lt;body&amp;gt;
        { wrapped(data) }
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  }
}

// In Scalatra servlet
...
get(&quot;/&quot;) {
  val posts = Post.find()
  HomePage(Context(&quot;title&quot; -&amp;gt; &quot;Home&quot;, &quot;post&quot; -&amp;gt; posts))
  // Optionally, use can use this shorthand
  // HomePage(NoKeyContext(&quot;Home&quot;, posts))
}
&lt;/pre&gt;

&lt;p&gt;(FYI, I&#39;m not prototyping a blog. Code here is strictly for example
purposes.)&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/3377813602148606937/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/01/who-needs-template-engine.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/3377813602148606937'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/3377813602148606937'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/01/who-needs-template-engine.html' title='Who needs a Template Engine?'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-2705567659724728832</id><published>2011-01-17T14:41:00.000-06:00</published><updated>2011-01-17T14:41:31.153-06:00</updated><title type='text'>Remote Control v0.1 Alpha</title><content type='html'>&lt;p&gt;The remote control package has finally reached an alpha stage today.
The library includes ways for a server program to give client programs
a way to change its behavior on the fly, or to run arbitrary code
on command, and receive values from those arbitrary commands.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;It all started &lt;a href=&quot;http://philcalicode.blogspot.com/2011/01/scala-hot-swapping-pseudo-hot-swap.html&quot;&gt;here&lt;/a&gt;, which brought us &lt;a href=&quot;http://philcalicode.blogspot.com/2011/01/real-time-solutions-via-scalabot.html&quot;&gt;there&lt;/a&gt;. The &lt;a href=&quot;http://philcalicode.blogspot.com/2011/01/real-time-solutions-via-scalabot.html&quot;&gt;last post&lt;/a&gt; in the series provided
a way to inject a program&#39;s context into potentially dynamic portions of the
server application. I have since refactored the library to make the client side interaction
much easier.&lt;/p&gt;

&lt;p&gt;I thought it would be easier for me to describe the library calls and use cases with code
samples.&lt;/p&gt;

&lt;h2&gt;Dynamic&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;dynamic&lt;/code&gt; call has been refactored into something that makes more sense than the previous
way.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Back to the old way, sweet!
dynamic(&#39;code) {
  println(&quot;Potentially dynamic code, without context preservation&quot;)
}

// With Context
val name = &quot;Charlie Murphy&quot;
dynamic(&#39;code, Context(&quot;name&quot; -&amp;gt; name)) {
  println(&quot;My name is %s&quot; format(name)
}

// Shorthand
dynamic(&#39;code, name) {
  println(&quot;My name is %s&quot; format(name)
}
&lt;/pre&gt;

&lt;p&gt;I have removed the need to pass a function that takes in a &lt;code&gt;Context&lt;/code&gt;. This is redundant.
What ever is inside the dynamic portions will &lt;em&gt;know&lt;/em&gt; the context. I&#39;ve provided a shorthand
which takes in a varargs of &lt;code&gt;Any&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;Update&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;update&lt;/code&gt; syntax got the most improvements.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;update(&#39;code) { ctx =&amp;gt;
  println(&quot;There is no context, so I will speak in riddles&quot;)
}

// The update pulling values from the context
// with the key
update(&#39;code) { ctx =&amp;gt;
  val name: String = ctx(&quot;name&quot;)
  println(&quot;%s and I&quot; format(name))
}

// The update applying a partial function 
// and uses Scala extractors to pull values
update(&#39;code) {
  case Context(name: String) =&amp;gt; 
    println(&quot;Your name is not %s&quot; format(name.reverse))
}
&lt;/pre&gt;

&lt;h2&gt;Reset&lt;/h2&gt;

&lt;p&gt;I&#39;ve given the client the ability to reset the dynamic portion to its
default state. A simple call to &lt;code&gt;reset&lt;/code&gt; will do it.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Back to &quot;My name is Charlie Murphy&quot;
reset(&#39;code)
&lt;/pre&gt;

&lt;h2&gt;Prepend and Append&lt;/h2&gt;

&lt;p&gt;There may be times a client doesn&#39;t want to change the dynamic portion, only add
something directly before or after it. That&#39;s where &lt;code&gt;prepend&lt;/code&gt; and &lt;code&gt;append&lt;/code&gt; come in handy.
For example:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Takes a Context like update
prepend(&#39;code) { ctx =&amp;gt;
  println(&quot;Hey!&quot;)
}

append(&#39;code) { ctx =&amp;gt;
  println(&quot;What&#39;s yours?&quot;)
}

// When dynamic is called it will now print
// Hey!
// My name is Charlie Murphy
// What&#39;s yours?
&lt;/pre&gt;

&lt;h2&gt;Remote Control&lt;/h2&gt;

&lt;p&gt;That&#39;s it as far as changing a server&#39;s behavior from a client program. The next part
of this post is show to how &lt;em&gt;control&lt;/em&gt; a server from a client program. The main difference between
changing behavior and controlling a server, is what the client can expect from a command.
If you are still confused, think of it this way:&lt;/p&gt;

&lt;p&gt;If you worked in the fast food industry, and your manager told you until further
notice to inform customers who order a specific menu item, &quot;We don&#39;t serve your kind here.&quot; Star Wars
references aside, you, the application, changed your original behavior from serving everyone to rejecting
those who order a specific item on the menu. Let&#39;s change gears now.&lt;/p&gt;

&lt;p&gt;Your manager is thirsty, and yells, &quot;Hey, Mikie, pass me a cold one!&quot; He was looking at you when
he spoke, so even though your name isn&#39;t &lt;strong&gt;Mikie&lt;/strong&gt;, you do what your told immediately.&lt;/p&gt;

&lt;p&gt;That is the key difference between behavioral modifications and commands.&lt;/p&gt;

&lt;p&gt;Any server application can handle remote commands by inheriting from the &lt;code&gt;RemoteControlled&lt;/code&gt; trait.
An easier way to see this is through the scala irc robot.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import remote.RemoteControlled

class Scalabot extends Pircbot with RemoteControlled {
  override def id = &#39;scalabot
   // The rest is the same
  ....
&lt;/pre&gt;

&lt;p&gt;To interface with the robot remotely, the client package would use the Controller object
to connect and sent signals, like so:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import control.Controller._

// A remote Controller will provide itself as default context
connect(&#39;scalabot) signal {
  case Context(self: Scalabot) =&amp;gt;
   self.joinChannel(&quot;#scala&quot;) 
}
&lt;/pre&gt;

&lt;p&gt;I provided a more complex example, to show how to retrieve data from the remote controller.
A client can receive data in two ways: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;through the &lt;code&gt;signal&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;through the &lt;code&gt;signalFuture&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;signal&lt;/code&gt; method will try to return a response immediately, if your signal returns anything
at all. The &lt;code&gt;signalFuture&lt;/code&gt; method will immediately return a function containing the result. (This
is quite literally the &lt;a href=&quot;http://www.scala-lang.org/api/current/scala/actors/Future.html&quot;&gt;future&lt;/a&gt;. It&#39;s a function that will apply itself until the value is there). 
Here&#39;s the example of a client program that wants exported data from the ircbot.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import ircbot.ScalaBot
import context._
import control.Controller.connect
import signals._

object Main extends Application {
  // Computation signal takes in an optional description
  // used for logging purposes
  val compSig = Computation({
    case Context(self: ScalaBot) =&amp;gt;
      // Dump db as csv
      val results = self.db.query(&quot;SELECT * FROM chats&quot;)(_.executeQuery())

      results.map(_.values.mkString(&quot;,&quot;))
  } /*, &quot;Running function which returns csv data&quot; */)

  connect(&#39;scalabot) signal(compSig) match {
    case Some(Response(csv)) =&amp;gt;
      val writer = new java.io.FileWriter(&quot;chats.csv&quot;)
      csv.asInstanceOf[List[String]].map(_ + &quot;\n&quot;).foreach(writer.write)
      writer.close()
    case _ =&amp;gt; println(&quot;This means we did not get a response back&quot;)
  }
}
&lt;/pre&gt;

&lt;p&gt;I ran the client package after chatting a few times, and the results was a chats.csv
on my local machine.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;1295284140634,schmee,Testing my chats
1295284147310,schmee,Doing some more chats
1295284161032,schmee,philbo is my robot
&lt;/pre&gt;

&lt;p&gt;All in all, it works. I have a few more features I want to add to it, but I&#39;m happy
with it so far. &lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/2705567659724728832/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/01/remote-control-v01-alpha.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/2705567659724728832'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/2705567659724728832'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/01/remote-control-v01-alpha.html' title='Remote Control v0.1 Alpha'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-8773770134054612760</id><published>2011-01-13T15:27:00.000-06:00</published><updated>2011-01-13T15:27:20.913-06:00</updated><title type='text'>Dynamicism with Context Preservation</title><content type='html'>&lt;p&gt;In my &lt;a href=&quot;http://philcalicode.blogspot.com/2011/01/real-time-solutions-via-scalabot.html&quot;&gt;last post&lt;/a&gt; I talked about changing some code in real time. One of
the problems with my current approach was the loss of the program context.
If we are using my Scalabot example, this means that I can make no changes
to ScalaBot from the dynamic portions, not dynamic calls, etc. I was limited
to interacting with a constant data store. I have since tweaked my library
which allows for context preservation, if context is important to the dynamic
call.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;The signature changed from:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;dynamic(&#39;symbol) {
  // Block of code
}
&lt;/pre&gt;

&lt;p&gt;To:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;dynamic(&#39;symbol, Context(&quot;variable&quot; -&amp;gt; variable)) { ctx =&amp;gt;
  // Block of code
}

// Or if context is unimportant
dynamic(&#39;symbol) { ctx =&amp;gt;
  // Block of code
}
&lt;/pre&gt;

&lt;p&gt;The update signature hardly changed. A context is always passed into a dynamic block now.
It could be an empty context, but a context nonetheless.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;update(&#39;symbol) { ctx =&amp;gt;
  if(!ctx.params.isEmpty) {
    // Grab stuff from context, and operate
    val x = ctx(&quot;variable&quot;)
    // do something with x
  }
}
&lt;/pre&gt;

&lt;p&gt;I&#39;ll show you how I used it to turn my ScalaBot against its master. It&#39;s always
more fun to witness the AI get a mind of its own, and destroy everything. Taking a 
look at the dynamic block from yesterday, let&#39;s preserve the context.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;dynamic(&#39;message, Context(&quot;bot&quot; -&amp;gt; this, &quot;sender&quot; -&amp;gt; sender)) { ctx =&amp;gt;
  println(&quot;A message posted from %s&quot; format(sender))
}
&lt;/pre&gt;

&lt;p&gt;Once again, I chatted a couple times, before I turned my bot against me.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import ircbot.ScalaBot
import hotcode.HotSwap._

object Main extends Application {
  update(&#39;message) { ctx =&amp;gt;
    val bot: ScalaBot = ctx(&quot;bot&quot;)
    val sender: String = ctx(&quot;sender&quot;)

    sender match {
      case &quot;schmee&quot; =&amp;gt; bot.sendMessage(&quot;#pircbot&quot;, &quot;schmee is a loser. Don&#39;t listen to him&quot;)
      case _ =&amp;gt; println(sender)
    }
  }
}
&lt;/pre&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguWlvXRieyDAZNijt0FHlgWBC-VzBltn_T_hndqqSqncYthC03ZM7Eact9zucae1Tyx_dt1_Cu5D-RK6-QrFQcNhQSsNyvcP_2gXpXehC29ZAa57YmKOdPPwlgcQ0WLLvm_pke_24abAne/s1600/Screen+shot+2011-01-13+at+12.59.16+PM.png&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguWlvXRieyDAZNijt0FHlgWBC-VzBltn_T_hndqqSqncYthC03ZM7Eact9zucae1Tyx_dt1_Cu5D-RK6-QrFQcNhQSsNyvcP_2gXpXehC29ZAa57YmKOdPPwlgcQ0WLLvm_pke_24abAne/s320/Screen+shot+2011-01-13+at+12.59.16+PM.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time I truly changed the bot&#39;s behavior, because I have a reference to the bot itself.
I knew before going into this, that the presentation of this library would take a hit, but
I thought the end result was worth it. I&#39;ve decided to &lt;em&gt;sell&lt;/em&gt; this library a little differently.
It&#39;s more of a remote control library. Hot swapping is not possible with it, but I like what it
does.&lt;/p&gt;

&lt;p&gt;I have an idea of some additional code to allow more remote controlling. Will get to that later.&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/8773770134054612760/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/01/dynamicism-with-context-preservation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/8773770134054612760'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/8773770134054612760'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/01/dynamicism-with-context-preservation.html' title='Dynamicism with Context Preservation'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguWlvXRieyDAZNijt0FHlgWBC-VzBltn_T_hndqqSqncYthC03ZM7Eact9zucae1Tyx_dt1_Cu5D-RK6-QrFQcNhQSsNyvcP_2gXpXehC29ZAa57YmKOdPPwlgcQ0WLLvm_pke_24abAne/s72-c/Screen+shot+2011-01-13+at+12.59.16+PM.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-3258029921275173213</id><published>2011-01-12T21:01:00.000-06:00</published><updated>2011-01-12T21:01:32.914-06:00</updated><title type='text'>Real-Time Solutions via ScalaBot</title><content type='html'>&lt;p&gt;I was talking about a pseudo hot swapping solution in my &lt;a href=&quot;http://philcalicode.blogspot.com/2011/01/scala-hot-swapping-pseudo-hot-swap.html&quot;&gt;last post&lt;/a&gt;. 
I briefly spoke of its potential, but at the time of its writing, I wasn&#39;t 
even sure it would solve problems. I decided a good way for me to find out 
if it was capable of solving those problems, would be to build something with it.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;The first decent scenario I could think of, was to program a little ircbot
whose behavior I could change on the fly. The more I thought it, the more I was
convinced that it would be a perfect fit. So here&#39;s the spiel:&lt;/p&gt;

&lt;p&gt;Let&#39;s say I want to read the #scala irc logs every day. Yes, I&#39;m that much of a dork.
This much of the requirement I knew into building the program, and hurried to launch
the app. Well, after three days of reading logs, I&#39;d like for my little robot to do
something a little different upon each message it receives. In a normal situation,
this would require me to logout of the channel, and put the other up. I &lt;em&gt;could&lt;/em&gt; potentially
miss a nugget from &lt;a href=&quot;http://en.wikipedia.org/wiki/Martin_Odersky&quot;&gt;Martin Odersky&lt;/a&gt;, which could have horrible consequences. So, the best
case scenario would be if I could have my changes without missing a beat.&lt;/p&gt;

&lt;p&gt;Now that I got my scenario lined up, I went to look for some java irc client out there.
After evaluating &lt;a href=&quot;http://martyr.sourceforge.net/&quot;&gt;several&lt;/a&gt; irc &lt;a href=&quot;http://jerklib.sourceforge.net/&quot;&gt;libraries&lt;/a&gt; out &lt;a href=&quot;https://github.com/sorcix/sIRC/&quot;&gt;there&lt;/a&gt;, I ran across &lt;a href=&quot;http://www.jibble.org/pircbot.php&quot;&gt;PircBot&lt;/a&gt;. It was
hosted on maven, which was a win for me and sbt.&lt;/p&gt;

&lt;p&gt;I threw together this simple class (and look, it uses my &lt;a href=&quot;http://philcalicode.blogspot.com/2010/09/simplifying-jdbc.html&quot;&gt;db adapter&lt;/a&gt;!), which simply
logs into an irc channel, and stores every message into a sqlite database:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import org.jibble.pircbot.PircBot
import hotcode.HotSwap._

class ScalaBot(name: String) extends PircBot {
  this.setName(name)
  this.setLogin(&quot;philip&quot;)

  val db = Adapter(&quot;org.sqlite.JDBC&quot;, &quot;jdbc:sqlite:chats.db&quot;)

  db.exec(&quot;&quot;&quot;CREATE TABLE IF NOT EXISTS chats (
              timestamp INTEGER,
              sender STRING,
              msg TEXT)&quot;&quot;&quot;)(_.execute()) 

  override def onMessage(c: String, sender: String, login: String, h: String, msg: String) {
    db.exec(&quot;INSERT INTO chats VALUES (?, ?, ?)&quot;) { stm =&amp;gt;
      stm.setLong(1, System.currentTimeMillis)
      stm.setString(2, sender)
      stm.setString(3, msg)
      stm.execute()
    }

    dynamic(&#39;message) {
      println(&quot;Post hook for dynamic message processing&quot;)
    }
  } 
}
&lt;/pre&gt;

&lt;p&gt;Yes! Raw SQL... &lt;a href=&quot;http://en.wikipedia.org/wiki/McDonald&#39;s_advertising&quot;&gt;I&#39;m lovin&#39; it&lt;/a&gt;! (&lt;em&gt;Clears throat&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;I want to draw your attention to the dynamic call. Notice, right now, it&#39;s
wrapping a trivial println statement. I&#39;m the kind of programmer, who
programs back-doors in every application. Can you Imagine the security risks?
I digress...&lt;/p&gt;

&lt;p&gt;I launch the application as is, because it solves my first problems. I now want
to change it&#39;s behavior after it stores a message. Here&#39;s where we get to the good
stuff.&lt;/p&gt;

&lt;p&gt;I used freenode&#39;s &lt;a href=&quot;http://webchat.freenode.net/?randomnick=1&quot;&gt;web app&lt;/a&gt; to log in as a human. There&#39;s an empty channel (thankfully)
at #pircbot where you can test your bots. A playgoround full of robots. I chatted some
random things as the user &quot;schmee&quot;, and sbt printing out what you&#39;d expect:&lt;/p&gt;

&lt;pre&gt;[info] Post hook for dynamic message processing
[info] Post hook for dynamic message processing
&lt;/pre&gt;

&lt;p&gt;Great. So at least I know it&#39;s running what&#39;s in the dynamic block. The following
segment is going to change that (fingers crossed).&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import hotcode.HotSwap._

object Main extends Application {
  update(&#39;message) {
    val db = Adapter(&quot;org.sqlite.JDBC&quot;, &quot;jdbc:sqlite:chats.db&quot;) 
    val message = db.single(&quot;SELECT * FROM chats ORDER BY timestamp DESC LIMIT 1&quot;)(_.executeQuery())

    println(message)
  }
}
&lt;/pre&gt;

&lt;p&gt;I run that, and chat a couple times more. Low and behold, my sbt output:&lt;/p&gt;

&lt;pre&gt;[info] Map(timestamp -&amp;gt; 1294867472648, sender -&amp;gt; schmee, msg -&amp;gt; Something special)
[info] Map(timestamp -&amp;gt; 1294867517142, sender -&amp;gt; schmee, msg -&amp;gt; trying it again)
&lt;/pre&gt;

&lt;p&gt;It works, but there are some notable problems. The context is all lost. It works great
if I&#39;m reading from a datastore, or changing values in the datastore. I can&#39;t really
change the bot itself though. I have some ideas about how to address this, but I
think the presentation is going to take a hit. At any rate, I was pleased to see it
work this well.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/3258029921275173213/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/01/real-time-solutions-via-scalabot.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/3258029921275173213'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/3258029921275173213'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/01/real-time-solutions-via-scalabot.html' title='Real-Time Solutions via ScalaBot'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-593187716105734485</id><published>2011-01-11T10:40:00.000-06:00</published><updated>2011-01-11T10:40:23.273-06:00</updated><title type='text'>Scala Hot Swapping (Pseudo Hot Swap)</title><content type='html'>&lt;p&gt;I wrote some library code recently, triggered by a &lt;a href=&quot;http://yarivsblog.blogspot.com/2008/05/erlang-vs-scala.html&quot;&gt;post I read&lt;/a&gt;. The gist of
the author&#39;s post was to compare Scala and Erlang. I actually agree with him.
I want to bring your attention to where I come into this, &lt;a href=&quot;http://en.wikipedia.org/wiki/Hot_swapping&quot;&gt;Hot code swapping&lt;/a&gt;.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;I created a library capable of swapping out dynamic code, particularly run on
a server. If I want to change behavior of some code without having to recompile the
code and reboot the server, I should be able to do this.&lt;/p&gt;

&lt;p&gt;My first attempt at this was more of a proof of concept, but it &lt;em&gt;does&lt;/em&gt; work.
Now, it&#39;s more of a pseudo hot swap solution, involving remote actors in
the internal Scala library. &lt;/p&gt;

&lt;p&gt;Here&#39;s some example of the client code in question.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import com.philipcali.hotcode.HotSwap._

def log(s: String) { 
  // logs text to file appender 
}

for(i &amp;lt;- 1 to 10) {
  println(&quot;Iteration %d&quot; format(i))
  dynamic(&#39;main) {
    log(&quot;Potentially dynamic code&quot;)
  }

  // Sleep for a second so I can inject some code
  Thread.sleep(1000)
}
&lt;/pre&gt;

&lt;p&gt;The control construct &lt;code&gt;dynamic&lt;/code&gt; is what we zero in on. The code inside that
block is stored on a &lt;code&gt;RemoteActor&lt;/code&gt;. Now, a few seconds later,
we want to change it.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import com.philipcali.hotcode.HotSwap._

def log(s: String) { 
  // logs text to file appender 
}

update(&#39;main) {
  log(&quot;Just been injected!&quot;)
}
&lt;/pre&gt;

&lt;p&gt;The call to &lt;code&gt;update&lt;/code&gt; here, replaces what&#39;s currently running with whatever is in
the update code block. Under the covers, nothing is lost. The &lt;code&gt;RemoteActor&lt;/code&gt; is
actually storing revisions of the dynamic code, which can be replaced at any
given time.&lt;/p&gt;

&lt;p&gt;If we look at the resulting output in our logged file:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;Potentially dynamic code
Potentially dynamic code
Potentially dynamic code
Just been injected!
Just been injected!
Just been injected!
Just been injected!
Just been injected!
Just been injected!
Just been injected!
&lt;/pre&gt;

&lt;p&gt;So there you have it: pseudo hot swapping. The &lt;em&gt;dynamic&lt;/em&gt; code here is hardly dynamic,
because everything is compiled, but changing behavior to an application should be 
fairly painless. Now, to do what the author wanted in that comparison post (which was
to simply add a log line), you don&#39;t have to shutdown the server!&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/593187716105734485/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2011/01/scala-hot-swapping-pseudo-hot-swap.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/593187716105734485'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/593187716105734485'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2011/01/scala-hot-swapping-pseudo-hot-swap.html' title='Scala Hot Swapping (Pseudo Hot Swap)'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-7929209058339747442</id><published>2010-10-30T09:27:00.000-05:00</published><updated>2010-10-30T09:27:29.117-05:00</updated><title type='text'>Nano Time is Here!</title><content type='html'>&lt;p&gt;And you know what that means: an attempt to write another book! I&#39;ve written up
some thoughts on my characters, locations, and important what not&#39;s on Google Docs
in anticipation. &lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;Last time I participated, I wrote my entire novel in vim, segregating my chapters 
into different files. There&#39;s really not much else you need than that... Or so I thought.&lt;/p&gt;

&lt;p&gt;I&#39;m taking a rather different approach this year. I&#39;m using the &lt;a href=&quot;http://www.vim.org/scripts/script.php?script_id=1658&quot;&gt;nerd tree plugin&lt;/a&gt; to keep better track of
my chapters. The big change, however, is that I&#39;m writing all my chapters in markdown.
Through markdown, I&#39;m provided with all the snazzy text formatting I need, but it&#39;s still plain text.
I plan on writing a simple script to strip the markdown for the final paper
submission. There&#39;s a good reason I&#39;m writing the book in markdown...&lt;/p&gt;

&lt;p&gt;You see, there&#39;s an awesome e-book management tool called &lt;a href=&quot;http://calibre-ebook.com/&quot;&gt;calibre&lt;/a&gt;. I got this program
to manage e-books on my wife&#39;s iPad, and they also offer a way to add books that anyone written
into the Books app through various formats. I found the plain text format needing much 
modification to get it to look right on the device. Calibre&#39;s html support, on the other hand, is 
incredible, but who wants to write all those nasty tags. This lazy programmer intends to convert his
markdown to html. //End third person speech.&lt;/p&gt;

&lt;p&gt;I have an update on the cron DSL I&#39;ve been working. Stay tuned on my adventure about building
grammar rules (try to keep your eyes open)!&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/7929209058339747442/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/10/nano-time-is-here.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7929209058339747442'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7929209058339747442'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/10/nano-time-is-here.html' title='Nano Time is Here!'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-7495377092767594362</id><published>2010-10-25T08:08:00.001-05:00</published><updated>2011-05-17T09:47:22.337-05:00</updated><title type='text'>DSL Magic</title><content type='html'>&lt;p&gt;&lt;strong&gt;Updated&lt;/strong&gt;: &lt;em&gt;Work is being done for a cron dsl work on my project &lt;a href=&quot;http://philcalicode.blogspot.com/2011/05/do-you-speak-cronish.html&quot;&gt;cronish&lt;/a&gt;. Check it out... fork me... I&#39;m open to suggestions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I have never taken a strong interest in creating DSL&#39;s before tinkering with Scala.
I had an eye opening experience this weekend, that originated as a joke.&lt;/p&gt;

&lt;p&gt;I&#39;ve recently taken an interest in the &lt;a href=&quot;http://www.imdb.com/find?s=all&amp;amp;q=harry+potter&quot;&gt;Harry Potter&lt;/a&gt; series. Other Potter fans may also
know that the next one in the series is coming out next month. On my birthday, in fact.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;I wanted to write a program that simply let me ask my computer when the movie was coming
to theaters. Scala is perfect for this. The DSL was thrown together in a hurry, so I am
not particularly fond of the underlying code, but the finished product was this:&lt;/p&gt;

&lt;pre&gt;scala&amp;gt; import com.philipcali.potter.dsl._
import com.philipcali.potter.dsl._

scala&amp;gt; When is &quot;Harry Potter and the Deathly Hallows: Part 1&quot; coming out?
Harry Potter and the Deathly Hallows: Part 1 is coming out in 24 days!

scala&amp;gt; When is &quot;Harry Potter and the Deathly Hallows: Part 1&quot; in theaters?
Harry Potter and the Deathly Hallows: Part 1 is coming out in 24 days!
&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;What exactly is the point&lt;/em&gt;? I can ask my computer when a movie is coming out &lt;em&gt;casually&lt;/em&gt;,
and it will tell me... through Scala. The DSL is rather pointless for a Scala programmer.
Using this simple DSL, a non programmer could &lt;em&gt;code&lt;/em&gt; something, and that is the point I am
getting to. The usefulness behind the DSL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That&#39;s when it hit me!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A cron DSL. Imagine being able to do something like this:&lt;/p&gt;

&lt;pre&gt;scala&amp;gt; val schedule = &quot;every day at 1:00pm&quot;.cron
schedule: java.lang.String = &quot;0 13 * * *&quot;

scala&amp;gt; &quot;every half hour on the 19th of November&quot;.cron
res0: java.lang.String = &quot;30 * 19 11 *&quot;

scala&amp;gt; &quot;every hour on every Thursday in December, January, and February&quot;.cron
res1: java.lang.String = &quot;0 * * 12,1,2 4&quot;
&lt;/pre&gt;

&lt;p&gt;Once the parser is created, configuring a scheduler through annotations, actors, or other
third party software would be a piece of cake. I plan on using Scala&#39;s built-in support
for &lt;a href=&quot;http://www.codecommit.com/blog/scala/the-magic-behind-parser-combinators&quot;&gt;language parsing&lt;/a&gt; to achieve this goal. &lt;em&gt;And a new project was conceived&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/7495377092767594362/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/10/dsl-magic.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7495377092767594362'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7495377092767594362'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/10/dsl-magic.html' title='DSL Magic'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-6869954675842381122</id><published>2010-09-29T09:02:00.000-05:00</published><updated>2010-09-29T09:02:44.911-05:00</updated><title type='text'>BDD and ScalaTest</title><content type='html'>&lt;p&gt;I&#39;ve read a good bit about &lt;a href=&quot;http://www.scalatest.org/&quot;&gt;ScalaTest&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Behavior_driven_development&quot;&gt;BDD&lt;/a&gt; before, but I actually used it for the first time
on a particular project I&#39;m working on at the moment.&lt;/p&gt;

&lt;p&gt;The code in question was a bit of utility code that simply extracts a zip archive or archive&#39;s 
a directory (a basic packaging utility). Writing a spec for this was very easy.&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Given an archive, extracting it should produce the known directory structure.&lt;/li&gt;
&lt;li&gt;The extracted contents should be the same as the known contents&lt;/li&gt;
&lt;li&gt;Archiving should produce an archive of directory&lt;/li&gt;
&lt;li&gt;Archive should be valid (ie: no errors on extracting)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That&#39;s four quick ones. Below, I have the spec written out as a ScalaTest. I couldn&#39;t help
but to have a big, goofy smile when I ran it through sbt. &lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;package test

import org.scalatest.{FlatSpec, BeforeAndAfterAll}
import org.scalatest.matchers.ShouldMatchers
import Zip._

class ZipSpec extends FlatSpec with ShouldMatchers with BeforeAndAfterAll {
  import java.io.File
  val archivePath = getClass.getClassLoader.getResource(&quot;archive.zip&quot;)

  override def afterAll(configMap: Map[String, Any]) {
    def recurse(file: File)(fun: File =&amp;gt; Unit) {
      if(file.isDirectory) 
        file.listFiles.filter(f =&amp;gt; !f.getName.startsWith(&quot;.&quot;)).foreach {
          recurse(_)(fun)
        }
      fun(file)
    }

    // Delete temp files
    recurse(new File(&quot;archive&quot;)) { _.delete }
    recurse(new File(&quot;temp&quot;)) { _.delete }
    new File(&quot;../archive.zip&quot;).delete
    new File(&quot;archive.zip&quot;).delete
  }

  &quot;Test archive&quot; should &quot;exists&quot; in {
    val archive = new File(archivePath.getFile)
    archive should not be (null)
  }  

  &quot;Extract&quot; should &quot;&quot;&quot;create directory tree: 
  archive/ 
  archive/child/ 
  archive/child/more.txt 
  archive/test.xml&quot;&quot;&quot; in {
    extract(archivePath.getFile)

    // Checking Dir tree
    new File(&quot;archive&quot;) should be (&#39;exists)
    new File(&quot;archive/test.xml&quot;) should be (&#39;exists)
    new File(&quot;archive/child&quot;) should be (&#39;exists)
    new File(&quot;archive/child/more.txt&quot;) should be (&#39;exists)
  }

  it should &quot;&quot;&quot;create a new directory tree:
  temp/archive
  temp/archive/child/
  temp/archive/child/more.txt
  temp/archive/test.xml&quot;&quot;&quot; in {
    extract(archivePath.getFile, &quot;temp&quot;)

    new File(&quot;temp/archive&quot;) should be (&#39;exists)
    new File(&quot;temp/archive/test.xml&quot;) should be (&#39;exists)
    new File(&quot;temp/archive/child&quot;) should be (&#39;exists)
    new File(&quot;temp/archive/child/more.txt&quot;) should be (&#39;exists)
  }

  &quot;Extracted flat files&quot; should &quot;contain correct data&quot; in {
    import scala.xml._
    import scala.io.Source.{fromFile =&amp;gt; open}

    val xmltext = &amp;lt;stuff&amp;gt;
  &amp;lt;more-stuff&amp;gt;Test&amp;lt;/more-stuff&amp;gt;
&amp;lt;/stuff&amp;gt;
    val text = &quot;You&#39;ll never find me!&quot;

    XML.loadFile(&quot;archive/test.xml&quot;) should be === xmltext
    open(&quot;archive/child/more.txt&quot;).getLines.mkString should be === text
  }

  &quot;Archive&quot; should &quot;create archive.zip&quot; in {
    archive(&quot;archive&quot;)

    new File(&quot;archive.zip&quot;) should be (&#39;exists)
  }

  it should &quot;create archive.zip in parent directory&quot; in {
    archive(&quot;archive&quot;, &quot;../&quot;)

    new File(&quot;../archive.zip&quot;) should be (&#39;exists)
  }

  &quot;../archive.zip and archive.zip&quot; should &quot;be the same size&quot; in {
    val parentzip = new File(&quot;../archive.zip&quot;)
    val archivezip = new File(&quot;archive.zip&quot;)

    parentzip.length should be === archivezip.length
  }

  &quot;Extracting archive.zip&quot; should &quot;not throw any exceptions&quot; in {
    extract(&quot;archive.zip&quot;)
  }
}
&lt;/pre&gt;

&lt;p&gt;Here&#39;s example output from sbt&#39;s execution of the test spec.&lt;/p&gt;

&lt;pre&gt;[info] == test.ZipSpec ==
[info] Test archive
[info] Test Starting: Test archive should exists
[info] Test Passed: Test archive should exists
[info] Extract
[info] Test Starting: Extract should create directory tree:
[info]   archive/
[info]   archive/child/
[info]   archive/child/more.txt
[info]   archive/test.xml
[info] Test Passed: Extract should create directory tree:
[info]   archive/
[info]   archive/child/
[info]   archive/child/more.txt
[info]   archive/test.xml
[info] Test Starting: Extract should create a new directory tree:
[info]   temp/archive
[info]   temp/archive/child/
[info]   temp/archive/child/more.txt
[info]   temp/archive/test.xml
[info] Test Passed: Extract should create a new directory tree:
[info]   temp/archive
[info]   temp/archive/child/
[info]   temp/archive/child/more.txt
[info]   temp/archive/test.xml
[info] Extracted flat files
[info] Test Starting: Extracted flat files should contain correct data
[info] Test Passed: Extracted flat files should contain correct data
[info] Archive
[info] Test Starting: Archive should create archive.zip
[info] Test Passed: Archive should create archive.zip
[info] Test Starting: Archive should create archive.zip in parent directory
[info] Test Passed: Archive should create archive.zip in parent directory
[info] ../archive.zip and archive.zip
[info] Test Starting: ../archive.zip and archive.zip should be the same size
[info] Test Passed: ../archive.zip and archive.zip should be the same size
[info] Extracting archive.zip
[info] Test Starting: Extracting archive.zip should not throw any exceptions
[info] Test Passed: Extracting archive.zip should not throw any exceptions
[info] == test.ZipSpec ==
[info]
[info] == test-complete ==
[info] == test-complete ==
[info]
[info] == test-finish ==
[info] Passed: : Total 8, Failed 0, Errors 0, Passed 8, Skipped 0
[info]
[info] All tests PASSED.
[info] == test-finish ==
&lt;/pre&gt;

&lt;p&gt;As inevitably a part of testing an archive utility, the test code will create a bunch of files
we don&#39;t want. Fortunately for us, ScalaTest comes with a &lt;strong&gt;BeforeAndAfterAll&lt;/strong&gt; trait for us. I
just to needed override &lt;strong&gt;afterAll&lt;/strong&gt; in this case, where I put the code to remove what was created.&lt;/p&gt;

&lt;p&gt;I feel the immediate advantage of this test framework is that the test code itself quite literally
&lt;em&gt;explains&lt;/em&gt; what&#39;s going on. Once I got the spec written, refactoring the implementation code
was breezy (which is characteristic of unit testing in general). &lt;/p&gt;

&lt;p&gt;Consider me a ScalaTest convert. I doubt I&#39;ll ever write a traditional &lt;a href=&quot;http://en.wikipedia.org/wiki/Unit_testing&quot;&gt;unit test&lt;/a&gt; again.&lt;/p&gt;

&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/6869954675842381122/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/09/bdd-and-scalatest.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6869954675842381122'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6869954675842381122'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/09/bdd-and-scalatest.html' title='BDD and ScalaTest'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-3449789436442731608</id><published>2010-09-13T15:22:00.001-05:00</published><updated>2010-09-13T15:23:36.357-05:00</updated><title type='text'>LWJGL / SBT plugin example app</title><content type='html'>&lt;p&gt;I decided to try out my new plugin for my new work environment, and my nephew has been frequently inquiring
to make him a &lt;em&gt;video game&lt;/em&gt;, but I particularly enjoy my free afternoons. I thought a good way to test the
plugin and appease the little guy, was to simply write a small game!&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;I&#39;ll be the first to admit that I&#39;m not a game programmer. Far from it. It requires a different way of thinking
, and to frank with everyone: I&#39;m not very good at it! I would, however, be lying if I
said I wasn&#39;t itching to make another game. It has been over a year since I used LWJGL and Slick
to make &lt;a href=&quot;http://github.com/philcali/Scetris&quot;&gt;Scetris&lt;/a&gt;, so I thought a quick &lt;a href=&quot;http://github.com/philcali/tictactoe-example&quot;&gt;Tic-Tac-Toe&lt;/a&gt; game was in order. (Don&#39;t shoot me for my baby steps!)&lt;/p&gt;

&lt;p&gt;Suffice to say, all three were accomplished relatively quickly.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;a href=&quot;http://github.com/philcali/sbt-lwjgl-plugin&quot;&gt;sbt-lwjgl-plugin&lt;/a&gt; works great! The speed I was able to have this project up and running 
was hardly comparable to that of my previous eclipse work environment.&lt;/li&gt;
&lt;li&gt;This was a great exercise getting back in the &lt;em&gt;swing of things&lt;/em&gt;, as they say. Slick makes making a simple game 
like this rather breezy.&lt;/li&gt;
&lt;li&gt;I think my nephew will enjoy the little game.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here&#39;s a screenshot. If the game looks like I spent a couple of hours on it, then &lt;strong&gt;mission accomplished&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQXd6QpYWQekPI5IMiSsS2rhOtNEfp5_CHmMZAUzDGCJKG89u_35Nm0jjzfoRsqJOqyqUN9vQyJqBIbzhrp__AlvWGq88fmf7l0vErKT0XXxp52AOqpjKHXUId1fQ6pOw4mtW-YlmYxmSy/s1600/Screen+shot+2010-09-13+at+2.25.03+PM.png&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQXd6QpYWQekPI5IMiSsS2rhOtNEfp5_CHmMZAUzDGCJKG89u_35Nm0jjzfoRsqJOqyqUN9vQyJqBIbzhrp__AlvWGq88fmf7l0vErKT0XXxp52AOqpjKHXUId1fQ6pOw4mtW-YlmYxmSy/s320/Screen+shot+2010-09-13+at+2.25.03+PM.png&quot; alt=&quot;Screenshot&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/3449789436442731608/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/09/lwjgl-sbt-plugin-example-app.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/3449789436442731608'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/3449789436442731608'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/09/lwjgl-sbt-plugin-example-app.html' title='LWJGL / SBT plugin example app'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQXd6QpYWQekPI5IMiSsS2rhOtNEfp5_CHmMZAUzDGCJKG89u_35Nm0jjzfoRsqJOqyqUN9vQyJqBIbzhrp__AlvWGq88fmf7l0vErKT0XXxp52AOqpjKHXUId1fQ6pOw4mtW-YlmYxmSy/s72-c/Screen+shot+2010-09-13+at+2.25.03+PM.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-1788313302664305745</id><published>2010-09-09T12:04:00.000-05:00</published><updated>2010-09-09T12:04:31.776-05:00</updated><title type='text'>LWJGL / SBT plugin</title><content type='html'>&lt;p&gt;So I created a &lt;a href=&quot;http://lwjgl.org&quot;&gt;LWJGL&lt;/a&gt; / sbt plugin today, thanks to this rather simple-but-awesome
&lt;a href=&quot;http://blog.misadventuregames.com/post/248744147/scala-and-lwjgl-with-sbt-updated&quot;&gt;blog post&lt;/a&gt; I read. I also added a little nugget for developers using &lt;a href=&quot;http://slick.cokeandcode.com/index.php&quot;&gt;Slick&lt;/a&gt;, which is a simple
trait for slick dependencies. It is probably more silly than useful, but it was easy enough
to add.&lt;/p&gt;

&lt;p&gt;This &lt;em&gt;project&lt;/em&gt; spawned because my aggravation in having to develop games in sbt. Fortunate for me,
someone had encountered a similar issue.&lt;/p&gt;

&lt;p&gt;The plugin, and more information about the plugin, can be found on github: &lt;a href=&quot;http://github.com/philcali/sbt-lwjgl-plugin&quot;&gt;sbt-lwjgl-plugin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;-- Philip Cali&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/1788313302664305745/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/09/lwjgl-sbt-plugin.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/1788313302664305745'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/1788313302664305745'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/09/lwjgl-sbt-plugin.html' title='LWJGL / SBT plugin'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-2559857729978236728</id><published>2010-09-02T10:19:00.001-05:00</published><updated>2010-09-02T10:19:39.965-05:00</updated><title type='text'>Streamlining Appengine Development</title><content type='html'>&lt;p&gt;So I have a few apps on appengine. I have my beautiful wife to thank for that.
I never planned on deploying anything other than a silly &lt;a href=&quot;http://philicalicode.appspot.com&quot;&gt;blog&lt;/a&gt; on it before she
came in my life, coming up with awesome ideas. She would mention in passing,
&quot;I wish there was an app for x on my ipad&quot;. Chances are, there would be, but
it would cost a dollar or two. I know that does not sound like much,
but I always look for an opportunity to expand the brain a little. So
I write her apps.&lt;/p&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;p&gt;All my apps prefice with &lt;strong&gt;1337&lt;/strong&gt;, which I coined the &lt;em&gt;leet suite&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://1337shoplist.appspot.com&quot;&gt;1337 Shop list&lt;/a&gt;: Your standard shopping list app. You can share with people, and anyone with a Google account can use it.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://1337flashcard.appspot.com&quot;&gt;1337 Flashcard&lt;/a&gt;: Your standard flashcard app that uses html 5 transitions, and stuff.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://1337todolist.appspot.com&quot;&gt;1337 Todo list&lt;/a&gt;: Your standard todo list with Google Calendar integration (which actually is not done yet).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They all support a mobile (specifically iPhone) version, and a desktop version. Through out all this,
I have made several libraries to help roll these guys out, and I was using eclipse to build and deploy
the builds. I will digress now.&lt;/p&gt;

&lt;p&gt;Eclipse is a monster. On my Macbook pro, eclispe alone consumes 1 gig of the 1.7 gigs I have available.
This is the cool thing about Eclipse: it dies when there is no memory left to consume.&lt;/p&gt;

&lt;p&gt;So I was in this frustrating cycle of coding, watching that blasted beach ball, coding, crashing,
launching eclipse, waiting, waiting, waiting, coding, watching the beach ball, crashing.&lt;/p&gt;

&lt;p&gt;There has got to be a better way, right!? There &lt;em&gt;is&lt;/em&gt;!
Over the past year, I have converted all the home grown scala projects to use sbt. &lt;/p&gt;

&lt;p&gt;The sbt/git eco-system, is very inviting. My biggest complaint about coding
Scala in vim was the lack of auto (method/field) complete ability, and seeing 
compile errors as I typed.&lt;/p&gt;

&lt;p&gt;But I figured it was time to grow up. I combined terminal multiplexing (screen) with vim and sbt.
I now code, compile (build), and deploy all from the command-line. This type of work environment
has alleviated a lot of the memory over head to run eclipse, combined with the fact that I
learned a few things about vim and screen.&lt;/p&gt;

&lt;p&gt;Now all I have to do to roll out a new appengine app with all my libraries, is:&lt;/p&gt;

&lt;pre&gt;git clone ssh://user@domain/var/git/appengine_scaffolding.git new_app
cd new_app
git rm -rf .git
sbt
[info] Building project appengine_scaffolding 1.0 against Scala 2.8.0
[info]    using AppengineScaffoldingProject with sbt 0.7.4 and Scala 2.7.7
&amp;gt; update
&amp;gt; dev-appserver-start -p 8888
&lt;/pre&gt;

&lt;p&gt;Direct my browser to http://localhost:8888, and bam! That is all.&lt;/p&gt;

&lt;p&gt;Some immediate advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learning more about sbt. I love that build tool. You configure your build and dependencies all through Scala code.&lt;/li&gt;
&lt;li&gt;Learning more about screen. I have used screen for years. I love screen. On a day to day basis, I could get by
with few open terminals, create, remove, rename, etc. Screen is very complicated. It allows you to split your view horizontally
or vertically. This feature is &lt;strong&gt;very&lt;/strong&gt; nice with sbt. I can reload or compile in sbt, and have the source file in my view right
above it.&lt;/li&gt;
&lt;li&gt;Learning more about vim. Vim is something else I used for years. I learn something new about it every week, it seems.&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;http://github.com/Yasushi/sbt-appengine-plugin&quot;&gt;appengine sbt plugin&lt;/a&gt;. A must get if you plan on doing similiar sbt/scala appengine development.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I realize this post was more about advertising than anything. Sorry if it bored you to death.&lt;/p&gt;

&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/2559857729978236728/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/09/streamlining-appengine-development.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/2559857729978236728'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/2559857729978236728'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/09/streamlining-appengine-development.html' title='Streamlining Appengine Development'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-5900530108646060382</id><published>2010-09-01T12:59:00.005-05:00</published><updated>2010-09-01T13:03:22.687-05:00</updated><title type='text'>Moved!</title><content type='html'>&lt;p&gt;So I ported all the content from my &lt;a href=&quot;http://philcalicode.appspot.com/&quot;&gt;old blog&lt;/a&gt;, and started one here hosted by Google. 
I have been so impressed by Google, and all their online services.
They are good to their customers, and really good to developers, what with all the libraries.&lt;/p&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://code.google.com/apis/gdata/&quot;&gt;Gdata&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Appengine&lt;/li&gt;
&lt;li&gt;Online services (gmail, calendar, contacts, map, blogger, etc)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I converted all my previous posts from html to markdown.
&lt;a href=&quot;http://en.wikipedia.org/wiki/Markdown&quot;&gt;Markdown&lt;/a&gt; is a nice readable markup language in its default state, which is easily
converted into other formats. While I trust Google with a lot of things, 
I always like to have physical backups of all my posts.&lt;/p&gt;

&lt;p&gt;So I store all my posts in my personal git repo, in multiple markdown files.
Cool enough. There is a really nice webapp with javascript Markdown to html conversion, called
&lt;a href=&quot;http://attacklab.net/showdown/&quot;&gt;Showdown&lt;/a&gt;. I could paste my markdown, convert it, then copy and paste the converted html into Blogger.&lt;/p&gt;

&lt;p&gt;But I am a developer, who is also very lazy. I wrote a small script that transforms a markdown
file into html, and loads the converted text into my clipboard for pasting into Blogger.&lt;/p&gt;

&lt;p&gt;For those interested, the code for this script is below:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;package com.calico.blogger.BlogBot

...
import com.petebevin.markdown.MarkdownProcessor
import scala.io.Source.{fromFile =&amp;gt; open}
import grizzled.util.{withCloseable =&amp;gt; withc}

object BlogBot {
  def writeToClipboard(text: String) {
    val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard
    val str = new StringSelection(text)
    clipboard.setContents(str, null)
  }

  def main(args: Array[String]) {
    if(args.size &amp;lt; 1) {
      println(&quot;Please provide me a text file to convert from markdown to html&quot;)
    }   

    // Remove the &amp;lt;code&amp;gt;&amp;lt;/code&amp;gt; protions
    val regex = &quot;&quot;&quot;&amp;lt;code&amp;gt;|&amp;lt;/code&amp;gt;&quot;&quot;&quot;.r

    // Add syntax highlighting
    val brush = &quot;&quot;&quot;&amp;lt;pre&amp;gt;&quot;&quot;&quot;.r

    val m = new MarkdownProcessor
    val markdown = m.markdown(open(args(0)).getLines.mkString(&quot;\n&quot;))

    val nocode = regex.replaceAllIn(markdown, &quot;&quot;) 
    val ret = brush.replaceAllIn(nocode, &quot;&quot;&quot;&amp;lt;pre class=&quot;brush: scala; toolbar: false;&quot;&amp;gt;&quot;&quot;&quot;)

    // Write to file
    println(&quot;Writing conversion to file: out.html ....&quot;)
    withc(new FileWriter(&quot;out.html&quot;)) { w =&amp;gt;
      w.write(ret)
    }   

    // Write to clipboard
    println(&quot;Writing conversion to your clipboard ....&quot;)
    try {
      writeToClipboard(ret)
      println(&quot;Done.&quot;)
    } catch {
      case e: Exception =&amp;gt; println(&quot;Failed ... &quot; + e.getMessage)
    }   
  }
}
&lt;/pre&gt;

&lt;p&gt;So the process is made quite simple with scala, sbt, vim, and screen. I like working in pure command-line
these days.&lt;/p&gt;

&lt;p&gt;This is a post to inform people that I am still alive, and doing development on the side.&lt;/p&gt;

&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/5900530108646060382/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/09/moved.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/5900530108646060382'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/5900530108646060382'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/09/moved.html' title='Moved!'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-7722295800039664635</id><published>2009-06-11T10:26:00.000-05:00</published><updated>2010-09-01T10:29:22.785-05:00</updated><title type='text'>Quotes with Actors Part II</title><content type='html'>&lt;p&gt;
This is where we&#39;ll be getting to the good part. For those of you are just now joining, the part one of the this series can be found &lt;a href=&quot;2009/06/quotes-with-actors-part-i.html&quot;&gt;here&lt;/a&gt;. Today I&#39;m going to be talking about 
how to make those communicating actors cross JVM, cross machine, cross platform. By changing a our code base a bit, and make use of the RemoteActors, we can achieve our goal.&lt;/p&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;So let me give you a scenario. Let&#39;s say Steve has an extensive collection of video games quotes that he thought were generally entertaining, and his buddy Bill has an extensive collection B-movie Sci-Fi quotes he thought were entertaining. Steve read a few of Bill&#39;s random quotes; low and behold, even he was entertained! But here&#39;s the problem: Steve doesn&#39;t want to pollute his quote collection with Bill&#39;s quotes, but he want people to know about Bill&#39;s quotes, when people connect to his quote server.&lt;/p&gt;

&lt;p&gt;In the last post we had a quote generator that would start an actor and loop away. Since we&#39;ve established a client/server terminology, let&#39;s take that code and make it accessible over the wire.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import scala.actors.Actor._
import scala.actors.RemoteActor._
import java.util.Random

class QuoteServer(val port: Int, val name: Symbol) {
  lazy val quotes = ... // Load our quotes from the &quot;store&quot;

  val generator = actor {
    alive(port)
    register(name, self)
    val rnd = new Random()
    loop {
      react {
        case Request() =&amp;gt; {   
          val (who, said, from) = quotes(rnd.nextInt(quotes.size))
          sender ! Quote(from, who, said)
        }   
      }   
    }   
  }
}
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;alive&lt;/strong&gt; and &lt;strong&gt;register&lt;/strong&gt; are methods on the object RemoteActor. One tells the actor to stay alive on a particular port, and the other registers the actor (in this case, self). That&#39;s basically it! Now we just need some code that kicks off that process to try it out.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;scala&amp;gt; import calico.quotes.server.QuoteServer
import calico.quotes.server.QuoteServer

scala&amp;gt; import calico.quotes.client.Request
import calico.quotes.client.Request

scala&amp;gt; val server = new QuoteServer(9090, &#39;local)
server: calico.quotes.server.QuoteServer = calico.quotes.server.QuoteServer@1742c56

scala&amp;gt; server.generator !? Request()
res0: Any = Quote(Suikoden II,Luca Blight,&quot;I&#39;ve chopped hundreds,
 thousands of necks. I can do it with my eyes closed!&quot;)
&lt;/pre&gt;

&lt;p&gt;Now I haven&#39;t demonstrated the remoteness yet, but it&#39;s all set up to work properly. The attentive would have noticed that the Request case class was moved to a client package. Some client some make a request. Remember, we are trying to make request over the wire. Sounds like it could be difficult right?&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import scala.actors.remote.{Node, RemoteActor}

case class Request()
case class Quote(from: String, who: String, what: String)

class QuoteC(ip: String, port: Int, val name: Symbol) {
  private val remote = RemoteActor.select(Node(ip, port), name)

  def quote = { 
    remote !? (1000, Request()) match {
      case Some(Quote(from, who, what)) =&amp;gt; what + &quot;\n&quot; + &quot; - &quot; + who + &quot; from &quot; + from
      case _ =&amp;gt; &quot;No quotes! Sorry... Is the server up?&quot;
    }   
  }
}
&lt;/pre&gt;

&lt;p&gt;We changed a couple of things from out last implementation, most notably, the use of the RemoteActor. The !? operator is overloaded with ability to set a timeout. Since we&#39;re dealing with remote activity, we don&#39;t want a request attempt to block the process. Instead, we just give the user a nice generic error message, for simplicity&#39;s sake.&lt;/p&gt;

&lt;p&gt;Without show some implementation code of this simple library, we can do something like this:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;&amp;gt; java -jar quote_server.jar
Please enter a port and server name

&amp;gt; java - jar quote_server.jar 9073 video_games
Starting server &#39;video_games on port 9073...
&lt;/pre&gt;

&lt;p&gt;Let&#39;s try to connect.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;&amp;gt; java -jar quote_client.jar
Welcome to the quote REPL
&amp;gt; /connect 127.0.0.1 9073 video_games
Connecting to &#39;video_games server
&amp;gt; /quote
&quot;I&#39;ve chopped hundreds, thousands of necks. I can do 
 it with my eyes closed!&quot;
- Luca Blight from Suikoden II
&amp;gt; /exit
Goodbye!
&lt;/pre&gt;

&lt;p&gt;I went ahead and threw the client implementation in a loop for kicks, but a quote server can act as a client to other quote servers with a small change.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;...
case Request() =&amp;gt; { 
   val (who, said, from) = quotes(rnd.nextInt(quotes.size))
      sender ! Quote(from, who, said)
}   
case ServerList(ls) =&amp;gt; sender ! ServerList(QuoteC.servers)
...
&lt;/pre&gt;

&lt;p&gt;Now we have a way to return a server list from this server, so it&#39;s possible for a client to &quot;hop&quot; around from server to server, or a server to create categories with server lists, etc. The point is: a distributed quote server system can appear localized from a single client connection. This is exactly what we wanted from our example implementation.&lt;/p&gt;

&lt;p&gt;Some things to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We could easily make and online interface by using the lift web framework.&lt;/li&gt;
&lt;li&gt;Concurrency is achieved by one quote server being able to handle several concurrent request.&lt;/li&gt;
&lt;li&gt;Going remote is easy!&lt;/li&gt;
&lt;li&gt;There are several implementations one can make with our little library.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This was an example of how one make use of the RemoteActor&#39;s in their code. It may not be the best example, but it&#39;s a good place to start in understanding how it works.&lt;/p&gt;

&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/7722295800039664635/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2009/06/quotes-with-actors-part-ii.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7722295800039664635'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7722295800039664635'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2009/06/quotes-with-actors-part-ii.html' title='Quotes with Actors Part II'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-4932043286115850209</id><published>2009-06-04T10:03:00.000-05:00</published><updated>2010-09-01T10:17:14.813-05:00</updated><title type='text'>Quotes with Actors Part I</title><content type='html'>&lt;p&gt;
Several months ago, I got serious with the Scala Actor library, and quickly fell in love. Very little data is required to thread together your application with actors, and make you feel like a rawk star ;)
This is part one of a two part series covering some assets of the scala actor library. First, we&#39;ll be looking at how to incorporate actors in code, then how to make application communication with RemoteActors.
&lt;/p&gt;

&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;p&gt;The application we&#39;re going to make is a quote generator. Clients would send a request for a random quote, and the application will send a quote in the form of a string, formatted like so: &quot;&lt;strong&gt;What was said&lt;/strong&gt;&quot; - &lt;strong&gt;Who&lt;/strong&gt; said it from &lt;strong&gt;Where&lt;/strong&gt;. With those requirements, we can come up with a couple of case classes for this simple application.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;case class Request()
case class Quote(from: String, who: String, what: String)
&lt;/pre&gt;

&lt;p&gt;What are case classes? In Scala, a case class definition is shorthand for this:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;object Quote {
  def apply(from: String, who: String, what: String) = new Quote(from, who, what)
  def unapply(q: Quote): Option[(String, String, String)] = Some(q.from, q.who, q.what)
}
class Quote (val from: String, val who: String, val what)
&lt;/pre&gt;

&lt;p&gt;Case classes are used for pattern matching, which the actor library makes heavy use of. The unapply is required for extracting, an incredible convenience for pattern matching. You can see that appending case in front of your class saves yourself a lot of typing.&lt;/p&gt;

&lt;p&gt;If we go back to our requirement, it said: Clients would send a &lt;strong&gt;request&lt;/strong&gt; for a random &lt;strong&gt;quote&lt;/strong&gt;. A verb and a noun. Similar to: a browser makes a &lt;strong&gt;GET&lt;/strong&gt; and displays the &lt;strong&gt;response&lt;/strong&gt;. So the next step is to make a quote &quot;server&quot; with a running actor.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import java.util.Random
import scala.io.Source.{fromFile =&amp;gt; open}
import scala.actors.Actor._

val quotes = { 
  (for(line &amp;lt;- open(&quot;resources/quotes.txt&quot;).getLines;
    val split = line.split(&quot;=&quot;)) yield((split(0), split(1), split(2)))).toList
}

val generator = actor {
    val rnd = new Random()
    loop {
      react {
       case Request() =&amp;gt; {
         val (who, said, from) = quotes(rnd.nextInt(quotes.size))
         sender ! Quote(from, who, said)
      }
    }
  }
}
&lt;/pre&gt;

&lt;p&gt;Alright let&#39;s break this up. The first thing I&#39;m doing is getting a list of tuples from a text file that contains our quotes. We&#39;ll call that our &quot;store&quot; of quotes in val quotes. I imported everything from object Actor, but we&#39;re only concerned about &lt;strong&gt;actor&lt;/strong&gt;, &lt;strong&gt;loop&lt;/strong&gt;, &lt;strong&gt;react&lt;/strong&gt;, and &lt;strong&gt;sender&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;actor&lt;/strong&gt; method takes in a body of code, instantiates an Actor class, and starts our actor. It&#39;s not enough that our sever starts. The server is required to run as long as we need it.&lt;/li&gt;
&lt;li&gt;That is where loop comes in. &lt;strong&gt;loop&lt;/strong&gt; takes in a function, calls that function and repeats itself. It is a never ending loop, which is exactly what we want.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;react&lt;/strong&gt; takes in a function and applies it to any message the actor receives in its mailbox. This is where pattern matching is key. Our server is only concerned about quote requests. In the event that our server has received a request, it grabs a random quote from the store.&lt;/li&gt;
&lt;li&gt;Think of &lt;strong&gt;sender&lt;/strong&gt; being the from address on the mail. Our server says &quot;send this quote to the sender of this request.&quot; That is done by the ! operator and the Quote case class to the right of it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now all we need is a client to make that request. The client code would look something like so:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;generator !? Request() match {
  case Quote(from, who, what) =&amp;gt; what + &quot;\n&quot; + &quot; - &quot; + who + &quot; from &quot; + from
  case _ =&amp;gt; &quot;No quotes! Sorry :(&quot;
}
&lt;/pre&gt;

&lt;p&gt;The !? operator is like a send/receive method. It sends the generator a request and receive a Quote. In the event that it&#39;s not a quote (which it should always be), return an &quot;error&quot; string. All we do now is some formal wrapper of the code above, and we got a random quote generator.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;package calico.quotes

import scala.io.Source.{fromFile =&amp;gt; open}
import scala.actors.Actor._
import java.util.Random

case class Request()
case class Quote(from: String, who: String, what: String)

object QMachine {
  val quotes = { 
    (for(line &amp;lt;- open(&quot;resources/quotes.txt&quot;).getLines;
        val split = line.split(&quot;=&quot;)) yield((split(0), split(1), split(2)))).toList
  }

  val generator = actor {
    val rnd = new Random()
    loop {
      react {
        case Request() =&amp;gt; {
          val (who, said, from) = quotes(rnd.nextInt(quotes.size))
          sender ! Quote(from, who, said)
        }   
      }   
    }   
  }
  def apply() = new QMachine(generator)
}

class QMachine(generator: scala.actors.Actor) {
  def quote = { 
    generator !? Request() match {
      case Quote(from, who, what) =&amp;gt; what + &quot;\n&quot; + &quot; - &quot; + who + &quot; from &quot; + from
      case _ =&amp;gt; &quot;No quotes! Sorry :(&quot; 
    }   
  }
}
&lt;/pre&gt;

&lt;p&gt;Now we run this through sbt (&lt;a href=&quot;http://code.google.com/p/simple-build-tool&quot;&gt;Simple Build Tool&lt;/a&gt;; I highly recommend that you build all Scala projects with it), we can fire up the console and test it out.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;scala&amp;gt; import calico.quotes.QMachine
import calico.quotes.QMachine

scala&amp;gt; val qm = QMachine()
qm: calico.quotes.QMachine = calico.quotes.QMachine@952527

scala&amp;gt; qm.quote
res0: java.lang.String = 
&quot;Let me tell you something. There are weak men and strong men 
in this world. The strong men take everything and the weak men die. 
That&#39;s how the world was designed. Now I will show you how it works, weaklings!!!&quot;
 - Luca Blight from Suikoden II

scala&amp;gt; qm.quote
res1: java.lang.String = 
&quot;I&#39;ve chopped hundreds, thousands of necks. I can do it with my eyes closed!&quot;
 - Luca Blight from Suikoden II

scala&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Ah, nothing like uplifting quotes from RPG&#39;s most ruthless villain, Luca Blight. Some things to notice about our quote generator implementation through actors are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &quot;client&quot; knows about our &quot;server&quot;, but our server could care less about the client. This separation is great, and largely popularised by the MVC paradigm.&lt;/li&gt;
&lt;li&gt;A few lines of code, and we can make our quote server send quotes over the wire with RemoteActors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And this is about it. Stay tuned for part two, when we make this &quot;remote.&quot;&lt;/p&gt;

&lt;p&gt;-- Philip &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/4932043286115850209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2009/06/quotes-with-actors-part-i.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/4932043286115850209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/4932043286115850209'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2009/06/quotes-with-actors-part-i.html' title='Quotes with Actors Part I'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-6177079647767442476</id><published>2009-05-11T10:00:00.003-05:00</published><updated>2010-09-01T10:03:13.735-05:00</updated><title type='text'>Factorial goofiness</title><content type='html'>&lt;p&gt;
It&#39;s Monday afternoon, and it&#39;s time for a silly post. Have you ever been browsing the Internet, looking at something completely code irrelevant, yet something struck you: &quot;I bet I could do this in X language&quot;? I saw a tshirt that said &quot;factorial!&quot;, when something like that hit me.&lt;/p&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;p&gt;Using some of the power of Scala, I&#39;m going to make a factorial look like it does in math books. Meaning, when you see the phrase 4!, it should evaluate to 24. Well, let&#39;s just get a factorial function to work first.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;scala&amp;gt; def fac(n: Int) : Int = if(n == 0) 1 else n * fac(n -1)
fac: (Int)Int

scala&amp;gt; fac(4)
res0: Int = 24
&lt;/pre&gt;

&lt;p&gt;There&#39;s our logic! I want to use a bang operator though. I want it to be a &quot;true&quot; factorial resemblance. If I want a bang operator, I&#39;ll need to have my own class.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;scala&amp;gt; class Fac(val i: Int) {
     | def fac(n: Int): Int = if(n == 0) 1 else n * fac(n - 1)
     | def ! = fac(i)
     | }
defined class Fac

scala&amp;gt; val test = new Fac(4)
test: Fac = Fac@1d9d55b

scala&amp;gt; test!
res1: Int = 24
&lt;/pre&gt;

&lt;p&gt;Well, now. That&#39;s starting to &lt;strong&gt;look&lt;/strong&gt; right, but there&#39;s still this thing: new Fac. I want to get rid of that as well. I mean, I just want to type 4!, and it will result in 24. This can be done through implicit conversions.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;scala&amp;gt; implicit def int2Fac(n: Int) = new Fac(n)
int2Fac: (Int)Fac

scala&amp;gt; 4!
res2: Int = 24
&lt;/pre&gt;

&lt;p&gt;And that&#39;s it! A pretty stupid example, but this example shows us several things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An example showing off operator overloading.&lt;/li&gt;
&lt;li&gt;An example showing optional periods and parenthesis in method calls.&lt;/li&gt;
&lt;li&gt;The reason for implicit conversions, and a perfect use case.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A fun example for a Monday.&lt;/p&gt;

&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/6177079647767442476/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2009/05/factorial-goofiness.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6177079647767442476'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/6177079647767442476'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2009/05/factorial-goofiness.html' title='Factorial goofiness'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-7480761985506626392</id><published>2009-05-04T15:34:00.000-05:00</published><updated>2010-09-01T09:35:39.908-05:00</updated><title type='text'>Simplifying the JDBC</title><content type='html'>&lt;p&gt;
The title of this post may raise some brows, but I mean what I say. You see, I do a lot of little batch scripting with sqlite databases, and since Scala is my scripting language of choice (as of this writing), I interact with the JDBC API quite a bit. In my situation, it would be silly to import all the 3rd party libraries that make it simple (the big ones being Spring JDBC and Hibernate). Allow me to show you what I mean...&lt;/p&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
&lt;p&gt;For those of you who haven&#39;t ever interacted with JDBC, or forgotten completely, allow me to enlighten you. The following was taken from &lt;a href=&quot;http://www.zentus.com/sqlitejdbc/&quot;&gt;sqlite JDBC homepage&lt;/a&gt;:&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import java.sql.*;

public class Test {
  public static void main(String[] args) throws Exception {
      Class.forName(&quot;org.sqlite.JDBC&quot;);
      Connection conn = DriverManager.getConnection(&quot;jdbc:sqlite:test.db&quot;);
      Statement stat = conn.createStatement();
      stat.executeUpdate(&quot;drop table if exists people;&quot;);
      stat.executeUpdate(&quot;create table people (name, occupation);&quot;);
      PreparedStatement prep = conn.prepareStatement(
          &quot;insert into people values (?, ?);&quot;);

      prep.setString(1, &quot;Gandhi&quot;);
      prep.setString(2, &quot;politics&quot;);
      prep.addBatch();
      prep.setString(1, &quot;Turing&quot;);
      prep.setString(2, &quot;computers&quot;);
      prep.addBatch();
      prep.setString(1, &quot;Wittgenstein&quot;);
      prep.setString(2, &quot;smartypants&quot;);
      prep.addBatch();

      conn.setAutoCommit(false);
      prep.executeBatch();
      conn.setAutoCommit(true);

      ResultSet rs = stat.executeQuery(&quot;select * from people;&quot;);
      while (rs.next()) {
          System.out.println(&quot;name = &quot; + rs.getString(&quot;name&quot;));
          System.out.println(&quot;job = &quot; + rs.getString(&quot;occupation&quot;));
      }
      rs.close();
      conn.close();
  }
}
&lt;/pre&gt;

&lt;p&gt;They do a little bit of everything here. Create a table, drop tables, inserts, and querying. Now let me show the the same program in question rewritten in Scala using my DB API. The actual code behind the API will come last.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;import calicodb.Adapter

object Main extends Application {
  val sqlitedb = Adapter(&quot;org.sqlite.JDBC&quot;, &quot;jdbc:sqlite:test.db&quot;)
  sqlitedb.exec(&quot;drop table if exists people;&quot;)(_.execute())
  sqlitedb.exec(&quot;create table people (name, occupation);&quot;)(_.execute())

  // Get our people ready
  val people = List((&quot;Gandhi&quot;, &quot;politcs&quot;), (&quot;Turing&quot;, &quot;computers&quot;), (&quot;Wittgenstein&quot;, &quot;smartypants&quot;))

  sqlitedb.exec(&quot;insert into people values (?, ?);&quot;) { prep =&amp;gt;
    for(p &amp;lt;- people; val (name, occupation) = p) {
      prep.setString(1, name)
      prep.setString(2, occupation)
      prep.addBatch()
    }
    prep.executeBatch()
  }

  val rtn = sqlite.query(&quot;select * from people;&quot;)(_.executeQuery())
  println(rtn)
  // List[Map[String, Any]](Map[String, Any](name -&amp;gt; Gandhi, occupation -&amp;gt; politics) , ...
}
&lt;/pre&gt;

&lt;p&gt;In my version, you should notice a few things different. I&#39;m providing some space between us, because I want you to try to find as many of them as you can before I tell you. There&#39;s one in particular, that should catch your eye ;)&lt;/p&gt;

&lt;p&gt;Okay. Ready? Let&#39;s start from the top. The big one: there&#39;s no close() statments anywhere! This is surely a recipe for disaster. Don&#39;t worry, it&#39;s not as bad as you think. You&#39;ll see in a second.&lt;/p&gt;

&lt;p&gt;Other than that, I&#39;m using an Adpater to interact with the sqlite database, but it seems like I&#39;m messing around with a PreparedStatment directly. Because I am! I guess now is as good a time as any to look at the underlying code.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;/** 
* For an update, insert, create, delete.
*/  
def exec(execute: String)(fun: PreparedStatement =&amp;gt; Any) = { 
    val conn = getConnection
    try {
        val stm = conn.prepareStatement(execute)
        conn.setAutoCommit(false)
        fun(stm)
        conn.commit()
        stm.close()
    } catch {
        case _ =&amp;gt; conn.rollback()
    } finally {
        conn.close()
    }
}
&lt;/pre&gt;

&lt;p&gt;From my last post, you should be able to tell that I&#39;m a big fan of general functions being passed around, and allowing the user to say what&#39;s up. Now it&#39;s clearer that my second parameter to this method is a function that takes a PreparedStatement, and does some work. Closure support allowed me to do batch commits based on a List of data. Fair enough. Good enough for me. Now there&#39;s this thing of queries returning a List of Map&#39;s. Must be some kind of magic. Let&#39;s take a look at that one now.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;def query(q: String) (fun: PreparedStatement =&amp;gt; ResultSet): List[Map[String, Any]] = {
    val conn = getConnection

    try {
        val stm = conn.prepareStatement(q)
        val results: List[Map[String, Any]] = fun(stm)
        stm.close()
        results
    } finally {
        conn.close()
    }
}
&lt;/pre&gt;

&lt;p&gt;Ah, same as before... except you&#39;re assiging a ResultSet to a List[Map[String, Any]] !? That compiles!? Yes. I really left that in there to show off another feature of Scala: implicit conversions. There&#39;s another function that does the bulk of the conversion work.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;/** Convert an ugly jdbc result set to a collection that can be used in code*/
implicit def rs2friendly(rs: ResultSet): List[Map[String, Any]] = {
 val meta = rs.getMetaData
 val names = for(i &amp;lt;- 1 to meta.getColumnCount) yield (meta.getColumnLabel(i))

 def buildr(names: List[String], rs: ResultSet): List[Map[String, Any]] = {
   rs.next match {
     case true =&amp;gt; {
       val tuples = for(name &amp;lt;- names) yield (name, rs.getObject(name))
       val map = Map() ++ tuples
       map :: buildr(names, rs)
     }
     case _ =&amp;gt; Nil
   }
 }

 val rtn = buildr(names.toList, rs)
 // Safe: close the result set since we&#39;re through
 rs.close()
 rtn
}
&lt;/pre&gt;

&lt;p&gt;And there you have it. The Adapter does some nice things. It does all of it&#39;s nice things without relying on 3rd party warez. And it&#39;s low level and general enough to use for a lot of JDBC drivers out there. Heck making this script compatible with MySQL is easy enough.&lt;/p&gt;

&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// MySQL
val mysqldb = Adapter(&quot;com.mysql.jdbc.Driver&quot;, &quot;jdbc:mysql://host:port/db&quot;)
&lt;/pre&gt;

&lt;p&gt;That&#39;s all I have for today. I hope it was interesting enough :P&lt;/p&gt;

&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/7480761985506626392/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2010/09/simplifying-jdbc.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7480761985506626392'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7480761985506626392'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2010/09/simplifying-jdbc.html' title='Simplifying the JDBC'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8145593511362203982.post-7561925771984403168</id><published>2009-05-04T13:39:00.003-05:00</published><updated>2010-08-31T15:16:13.715-05:00</updated><title type='text'>File utilities and the beauty of closures</title><content type='html'>&lt;p&gt;Arg! I made the same mistake again. You know: the one where you made a bad svn commit, and there are those pesky .svn folders everywhere. Well, it happened to me twice in the same night, and I&#39;m too lazy to delete them by hand. I decided a simple Scala script would do the trick.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;p&gt;A simple little recursive function would do the trick. I fired up the scala interpreter, and whipped out the following function in a minute or two:&lt;/p&gt;&lt;pre class=&quot;brush: scala; toolbar: false; gutter: false;&quot;&gt;scala&amp;gt; def recurse(folder: java.io.File) {
  | if(folder.isDirectory) {
  |   folder.listFiles.foreach{recurse(_)}
  | } 
  | if (folder.getName.contain(&quot;.svn&quot;)) folder.delete
  |}  
recurse: (java.io.File)Unit

scala&amp;gt; recurse(new File(&quot;.&quot;))
&lt;/pre&gt;&lt;p&gt;Bam! That did the trick! I started to think about the function more (even though it was already stupid simple). There&#39;s a way to make this recursive function more general. Say I wanted a function that allows me to traverse a directory and &lt;strong&gt;do&lt;/strong&gt; whatever I want, instead of traverse a directory and only delete .svn directories. A small change to the recursive function would allow that:&lt;/p&gt;&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;def recurse(folder: File) (action: File =&amp;gt; Unit) {
  if(folder.isDirectory) {
    folder.listFiles.foreach(f =&amp;gt; recurse(f)(action))
  }   
  action(folder)
}
&lt;/pre&gt;&lt;p&gt;The keen Scalafied eye would quickly see what I did there. For the rest of us, I&#39;m going to begin explaining now:&lt;/p&gt;&lt;p&gt;The function recurse now takes 2 parameters: a java.io.File, and a function that takes in a java.io.File and returns Unit (which in the Scala world is equivalent to Java&#39;s void). The function parameter is named action. For each file, and directory, action will be called on it. So, now I can call it like so:&lt;/p&gt;&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;recurse(new File(&quot;.&quot;)) { f=&amp;gt; 
  if (f.getName.contains(&quot;.svn&quot;)) f.delete
}
&lt;/pre&gt;&lt;p&gt;That&#39;s just doing the same as before, but now, let&#39;s say I wanted to find every modified .scala from now to a week ago:&lt;/p&gt;&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// Get the date from a week ago
val cal = Calendar.getInstance
cal.add(Calendar.DATE, -7)
recurse(new File(&quot;.&quot;)) { f=&amp;gt;
  if (f.getName.endsWith(&quot;.scala&quot;) &amp;amp;&amp;amp; f.lastModified &amp;gt;= cal.getTime) println(f.getName)
}
&lt;/pre&gt;&lt;p&gt;Even better still: with Scala&#39;s closure support and partial functions, you can do some nifty actions with our very basic utility function. I want to show you the latter first:&lt;/p&gt;&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// This is partial function syntax
// Regardless of the parent directory, I want to delete svn&#39;s
val deleteSvnIn = recurse(_: File) { f=&amp;gt;
  if (f.getName.contains(&quot;.svn&quot;)) f.delete
}

// Calling the partial looks very natural
deleteSvnIn(new File(&quot;.&quot;))
deleteSvnIn(new File(&quot;/some/very/bad/dir&quot;))

// Or vice versa
val inCurrentDir(new File(&quot;.&quot;)) _

inCurrentDir { f =&amp;gt;
  if(f.getName.endsWith(&quot;.scala&quot;)) println(f.getAbsolutePath)
}
&lt;/pre&gt;&lt;p&gt;What I&#39;ve shown you thus far is nothing spectacular. What I mean by that, is simply the same could be accomplished with any language that allows anonymous functions. Now, I&#39;ll show you closures. Allow me to give you a scenario first, so what I say makes sense: You were giving this library to work with, except you actually need the recurse function to &lt;strong&gt;return&lt;/strong&gt; a value not just arbitrarily &lt;strong&gt;do&lt;/strong&gt; something. You need to persist some data whether it be a count, a list, whatever. Given that you can&#39;t change&lt;br /&gt;
the horribly coded utility function, your options are:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Write another function that does what you need.&lt;/li&gt;
&lt;li&gt;Rely on Scala&#39;s closure support for your help, rewrite the function if time permits, and submit a patch :)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Let&#39;s rely on closures for the time being.&lt;/p&gt;&lt;pre class=&quot;brush: scala; toolbar: false;&quot;&gt;// I want to count all the svn directories
var count = 0
recurse(new File(&quot;.&quot;)) { f=&amp;gt;
  if (f.getName.contains(&quot;.svn&quot;)) count += 1
}

println(count)

// I want to store all scala files
var ls = List[File]()
recurse(new File(&quot;.&quot;)) { f=&amp;gt;
  if (f.getName.endsWith(&quot;.scala&quot;)) ls += f
}
println(ls)
&lt;/pre&gt;&lt;p&gt;And there you have it! Even though we&#39;re solving problems outside the scope of this function&#39;s original goal, you&#39;re able to bend the rules with the power of closures. The functional programmer would have noticed the use of var&#39;s and scowled. I merely wanted to prove a point, not start a flame war.&lt;/p&gt;&lt;p&gt;-- Philip&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://philcalicode.blogspot.com/feeds/7561925771984403168/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://philcalicode.blogspot.com/2009/05/file-utilities-and-beauty-of-closures.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7561925771984403168'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8145593511362203982/posts/default/7561925771984403168'/><link rel='alternate' type='text/html' href='http://philcalicode.blogspot.com/2009/05/file-utilities-and-beauty-of-closures.html' title='File utilities and the beauty of closures'/><author><name>Philip</name><uri>http://www.blogger.com/profile/06896443671786220765</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMTWBUNfn4TLf2OuLJwuWvin2ksSkm_l5kf5AviFQuM1iUckdxxhY_TvZQGU0NtcxpINesNkFJB-PM0ZBdisrE-MAhJ-ERcTMLSUGG-5d3iml_AOt1XbLwH2__qAM9fMo/s220/philip_conference.png'/></author><thr:total>0</thr:total></entry></feed>