<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>a blog about programming</description><title>il codice</title><generator>Tumblr (3.0; @iamchrislewis)</generator><link>http://blog.thegodcode.net/</link><item><title>Idea: Validation + Adapters for Terminal Programs and Web Services</title><description>&lt;p&gt;Derive a common utility or library for options and constraints from something like &lt;a href="https://github.com/jstrachan/scopt"&gt;scopt&lt;/a&gt; and &lt;a href="http://unfiltered.databinder.net/"&gt;Unfiltered&lt;/a&gt;’s parameter validation library. With a common way to specify input arguments, functions to produce boiler plate for command like programs and web endpoints would be easy to write. This would be a great use for &lt;a href="https://github.com/scalaz/scalaz"&gt;scalaz&lt;/a&gt; / validation.&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/12201126814</link><guid>http://blog.thegodcode.net/post/12201126814</guid><pubDate>Tue, 01 Nov 2011 13:18:08 -0400</pubDate><category>projects</category><category>todo</category></item><item><title>Too much curry!</title><description>&lt;p&gt;What does a compiled curried method in Scala look like in java? Compile this:

&lt;pre&gt;object Concat {
  def concat(s: String)(ss: String) = s + ss
  def concat(s: String)(i: Int) = i + s
}&lt;/pre&gt;

Now inspect the resulting class with &lt;code&gt;javap&lt;/code&gt;:

&lt;pre&gt;Compiled from "Concat.scala"
public final class Concat extends java.lang.Object{
    public static final java.lang.String concat(java.lang.String, java.lang.String);
}&lt;/pre&gt;

After compilation curried methods look like normal, multi-parameter methods. It doesn’t matter how many parameter lists you define, nor how many parameters each list takes; in the end you get a plain old method. Knowing this we should expect overloading to work fine, so let’s overload our curried method:

&lt;pre&gt;object Concat {
  def concat(s: String)(ss: String) = s + ss
  def concat(s: String)(i: Int) = i + s
}&lt;/pre&gt;

As expected, this compiles, and &lt;code&gt;javap&lt;/code&gt; gives expected output:

&lt;pre&gt;public final class Concat extends java.lang.Object{
    public static final java.lang.String concat(java.lang.String, int);
    public static final java.lang.String concat(java.lang.String, java.lang.String);
}&lt;/pre&gt;

Great, let’s use it!

&lt;pre&gt;object Concat {
  def concat(s: String)(ss: String) = s + ss
  def concat(s: String)(i: Int) = i + s
}

object Concatter {
  def greet(who: String, msg: String) = Concat.concat(who)(msg)
}&lt;/pre&gt;

And compile:

&lt;pre&gt;Concat.scala:7: error: ambiguous reference to overloaded definition,
both method concat in object Concat of type (s: String)(i: Int)String
and  method concat in object Concat of type (s: String)(ss: String)java.lang.String
match argument types (String)
  def greet(who: String, msg: String) = Concat.concat(who)(msg)
                                               ^
one error found&lt;/pre&gt;

Well, that was unexpected! The compiler let us overload a curried method, but it won’t let us use either of them. The error tells us that the compiler is unable to disambiguate the method. This seems strange as we’re explicitly passing values for all arguments. Try as we may, there simply is no way to use these methods as defined. As it turns out, this is a problem of scope. Let’s try different access modifiers:

&lt;pre&gt;object Concat {
  def concat(s: String)(ss: String) = s + ss
  protected def concat(s: String)(i: Int) = i + s
}

object Concatter {
  def greet(who: String, msg: String) = Concat.concat(who)(msg)
}
&lt;/pre&gt;

This compiles because &lt;code&gt;Concat.concat(s: String)(i: Int)&lt;/code&gt; is not in scope at the call site (it’s not accessible). However, within &lt;code&gt;Concat&lt;/code&gt; both are in scope:

&lt;pre&gt;object Concat {
  def concat(s: String)(ss: String) = s + ss
  protected def concat(s: String)(i: Int) = i + s
  def con = concat("")("")
}&lt;/pre&gt;

&lt;pre&gt;Concat.scala:4: error: ambiguous reference to overloaded definition,
both method concat in object Concat of type (s: String)(i: Int)String
and  method concat in object Concat of type (s: String)(ss: String)java.lang.String
match argument types (java.lang.String)
  def con = concat("")("")
            ^
one error found&lt;/pre&gt;

If you’re overloading curried methods, remember that you must demarcate their potential scopes with access modifiers. Two overloaded curried methods with the same access levels will never be callable; they will compile unless you try to use them. Perhaps a more fundamental lesson to learn from this is to eschew method overloading in general, always.&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/9476257598</link><guid>http://blog.thegodcode.net/post/9476257598</guid><pubDate>Sat, 27 Aug 2011 19:38:00 -0400</pubDate><category>scala</category></item><item><title>"There’s always a “best” tool for any job, but if programmers don’t know how..."</title><description>“There’s always a “best” tool for any job, but if programmers don’t know how to use it, they’ll choose an inferior tool because they think their schedule doesn’t permit a learning curve. In the long run they’re hurting their schedules, but it’s hard to see that when you’re down in the trenches.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;a href="http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html"&gt;Steve Yegge&lt;/a&gt;&lt;/em&gt;</description><link>http://blog.thegodcode.net/post/7930763992</link><guid>http://blog.thegodcode.net/post/7930763992</guid><pubDate>Fri, 22 Jul 2011 12:13:05 -0400</pubDate></item><item><title>Moving to New York City</title><description>&lt;p&gt;For several years now I’ve felt that, at some point, I’d like to move to New York City. I visited the city for the first time in 2009, and I was just taken. Its history, strength, character, diversity, and cultural wealth captivated me.&lt;/p&gt;

&lt;p&gt;Still, there was something else. I had just recently discovered the &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; programming language. I’m a &lt;a href="http://polyglotprogramming.com/"&gt;polyglot programmer&lt;/a&gt; but I’ve spent a lot of time on the jvm. When I was exposed to Scala, my first reaction was merely: “Wow, this is cool!” And then the paradigm shift began as I inadvertently embarked on a mind-altering journey into the world of &lt;a href="http://en.wikipedia.org/wiki/Functional_programming"&gt;functional programming&lt;/a&gt;. It’s been frustrating, illuminating, continuous, and it just keeps going.&lt;/p&gt;

&lt;p&gt;But I digress. One of the reasons that I’m moving to New York is that there are many bright, passionate people that make up a vibrant community of programmers; functional and aspiring-to-be alike. It is a place where exploring concepts with others is easier; a place where cross-pollination of ideas and paradigms common. What’s more, there are companies building great products that are harnessing this energy (like &lt;a href="https://www.novus.com"&gt;the one I’m joining&lt;/a&gt;). I’ll be making the switch from full-time Java to full-time Scala, and while I’m new to the company’s industry, I’m &lt;a href="https://github.com/chrislewis"&gt;no newbie to Scala&lt;/a&gt;. I’m full of an excitement laced with a bit of healthy fear, and I can’t wait to start.&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/7551840101</link><guid>http://blog.thegodcode.net/post/7551840101</guid><pubDate>Tue, 12 Jul 2011 19:54:41 -0400</pubDate><category>nyc</category><category>scala</category><category>functional</category></item><item><title>"If anything were actually “immeasurable,” it would bear no relationship of any kind to..."</title><description>“If anything were actually “immeasurable,” it would bear no relationship of any kind to the rest of the universe, it would not affect nor be affected by anything else in any manner whatever, it would enact no causes and bear no consequences - in short, it would not exist.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Ayn Rand&lt;/em&gt;</description><link>http://blog.thegodcode.net/post/7011909584</link><guid>http://blog.thegodcode.net/post/7011909584</guid><pubDate>Tue, 28 Jun 2011 11:11:27 -0400</pubDate></item><item><title>Making Meetup: Real-life Meetups Deserve Real-time APIs</title><description>&lt;a href="http://making.meetup.com/post/2929945070/real-life-meetups-deserve-real-time-apis"&gt;Making Meetup: Real-life Meetups Deserve Real-time APIs&lt;/a&gt;: &lt;p&gt;&lt;a href="http://making.meetup.com/post/2929945070/real-life-meetups-deserve-real-time-apis" class="tumblr_blog"&gt;makingmeetup&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;First things first, open a linux or mac terminal and tap into the RSVPs stream:&lt;/p&gt; &lt;pre&gt;curl http://&lt;span&gt;stream.meetup.com/2/rsvps&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;What you’re seeing now is each RSVP to a public Meetup group as we receive it. When a user clicks an RSVP button or an application calls the RSVP method, the server handling the…&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://blog.thegodcode.net/post/2930173490</link><guid>http://blog.thegodcode.net/post/2930173490</guid><pubDate>Tue, 25 Jan 2011 17:21:20 -0500</pubDate></item><item><title>Best. Gift. Ever.</title><description>&lt;p&gt;
Last night, Erin and I opened presents that had arrived in the mail from my parents. One of them was a folded, unassuming manilla envelope, and inside was another folded manilla envelope. I unfolded the inner envelope to reveal a 5x5 floppy disk which, at first, confounded me. Looking at its label I immediately recognized my mother’s handwriting; it identified the disk as “Chris’ Game.” It was accompanied by a note from her, stating “The floppy disk may have Chris’s first programming efforts - a game in Q-basic, I think.” These simple words are precious.
&lt;/p&gt;
&lt;div class="center"&gt;
&lt;img src="http://media.tumblr.com/tumblr_lecpteyhQQ1qzwr1q.jpg"/&gt;&lt;/div&gt;
&lt;p&gt;
My family is not one of technophiles, and in fact they might be better described as technophobic. They worked at a small college with a quaint computer lab, and one day a friend told me I could use that to make a game. My mind was blown! He then introduced me to the computer that would become my first workstation: an IBM (compatible) 80286 with a turbo switch. It was running a later version of DOS with a menu-driven UI called DosShell. Having almost no previous exposure to computers, I didn’t know how to be picky.
&lt;/p&gt;
&lt;div class="center"&gt;
&lt;img src="http://www.mvps.org/serenitymacros/images/dosshell.png"/&gt;&lt;/div&gt;
&lt;p&gt;
My friend didn’t know much, but he knew that much more than I did. He showed me how to navigate the system, how to start the QBasic environment, and how to evaluate simple statements (print to the screen, basic operators and math functions). I didn’t know what any of this stuff was, but I soaked it up with wonder and learned quickly.

I don’t remember the exact date, but this must have been in the summer of 1992 or 1993. In the following weeks I spent countless solitary hours exploring the computer, spending most of my time in QBasic. A day or two in, I started writing my game.
&lt;/p&gt;
&lt;div class="center"&gt;
&lt;img src="http://upload.wikimedia.org/wikipedia/en/0/01/QBasic_Opening_Screen.png"/&gt;&lt;/div&gt;

&lt;p&gt;
I remember this game. I remember its source code. It was a text-based game of monsters, magic, and battle. I remember the depression I felt when I concluded that it was impossible to randomize the output of a fight, a problem on which I had spent a day or two hacking to no avail. And then I discovered the interpreter’s help system, which eventually led me to the RAND function (a pseudo-random number generator). This proved to be a solution to my problem, and behold, nondeterministic battles! Anything was possible! I did what I wanted, coding with pure joy and blissfully ignorant of any consequence other than bugs in my game. Everything I did, I did to make the game better: I added stronger monsters, magic, shops, items, weaponry, and commerce (you could buy and sell items and weapons). It was awesome! More than awesome, it revealed a lasting passion in me at a fairly young age: I loved to program.
&lt;/p&gt;

&lt;p&gt;
I’ve received many gifts of sentimental value in my life, but never one I’d describe as “grounding.” In a month I’ll be 30 years old. 30! We change and we grow, and sometimes an analysis of who we are compared to who we were leaves us puzzled. To hold this 5x5 floppy disk in my hand; to reflect on the stumbling, clueless child that I was as I elatedly copied my source code to and from its magnetic memory some 17 years ago is truly grounding. What lies encoded on this disk is an immutable fragment of me; a reminder of who I am. I cannot imagine a better gift!
&lt;/p&gt;
&lt;p&gt;
Happy New Year to all. To open 2011 I impart this simple declaration to myself and you all alike:&lt;/p&gt;&lt;p&gt;
Do what you love and do it with passion. If you aren’t happy, do something else.
&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/2555283264</link><guid>http://blog.thegodcode.net/post/2555283264</guid><pubDate>Sat, 01 Jan 2011 12:45:40 -0500</pubDate></item><item><title>Anxiety 2.0</title><description>&lt;p&gt;Last weekend I attended the first annual &lt;a href="http://usa10.webdirections.org/"&gt;WebDirections USA&lt;/a&gt; conference, in Atlanta, GA. My primary takeaway is that JavaScript on the server side will continue to make enormous gains comparable to those made by rails when it first appeared on the scene.&lt;/p&gt;

&lt;p&gt;Yeah, that’s huge.&lt;/p&gt;

&lt;p&gt;Of all the sessions, NodeJS and the expert panel resonated with me over the rest. The node session was a simple, thorough primer; nothing of any apparent philosophical value. However, the expert panel, and specifically a bold assertion made by &lt;a href="http://tantek.com/"&gt;Tantek Çelik&lt;/a&gt;, sent my head into overdrive. His thesis: in 5 years, every server side language will be dead or dying, being replaced by JavaScript. For a split second I merely giggled, and then I went somber; pensively spiraling into a future far different than the one I had imagined, and certainly different from the one I wanted to see.&lt;/p&gt;

&lt;p&gt;This assertion did not sit well with me. The rails phenomenon shocked the web world, in many ways for the better, but it is a phenomenon I have never fully embraced.&lt;/p&gt;

&lt;p&gt;On Friday night I engaged Tantek about this issue, hoping to gain a better understanding of his perspective, as well as my own, since he had dealt me a debasing blow. He clarified his prediction: in 5 years, all other language communities will have ceased to grow, and likely be in recession. Why? Ubiquity. JavaScript is the most widely deployed language on the planet, and huge numbers of people have at least some knowledge of it, as well as CSS and HTML. It is also a comparatively productive and relatively simple language, converging on what seems to be critical mass.&lt;/p&gt;

&lt;p&gt;What do you get when you mix ubiquity, simplicity, and productivity? World takeover. Well, maybe.&lt;/p&gt;

&lt;p&gt;I take issue with this idea. I have not yet fully formed my opinion, but the thought does cause me a bit of anxiety, and I shared this with Tantek.&lt;/p&gt;

&lt;p&gt;What’s my deal? Am I sour that my &lt;a href="http://www.scala-lang.org/"&gt;preferred&lt;/a&gt; &lt;a href="http://www.haskell.org/"&gt;languages&lt;/a&gt; are not on the forefront of the next apparent revolution in web development? Maybe a little, but that’s hardly the crux of it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Typing (integer.sayHello())&lt;/b&gt;&lt;br/&gt;
This is a battle that goes back decades. Dynamic interpreted languages are popular for at least two reasons: they are, or rather, they seem to be, simpler to learn and use, and they are more productive in the day-to-day development cycle. I am perhaps a minority among my fellow generation Yers in that I prefer static typing. No, I don’t like superfluous typing and no, I don’t merely want a static hint at what an argument is capable of (though you could make a fair argument for this). I prefer static typing because it is inherently more correct.&lt;/p&gt;

&lt;p&gt;Static typing guarantees that, in terms of the expressed capabilities of a program’s abstractions, the calls made on these abstractions are logically sound. That is, during the compilation phase, if you call a method &lt;code&gt;sayHello&lt;/code&gt; on an instance of &lt;code&gt;Integer&lt;/code&gt;, the compilation will fail. It simply isn’t possible to make this mistake.&lt;/p&gt;

&lt;p&gt;Further, many dynamic languages (PHP, ruby, python, javascript, …) allow arbitrary changes to arbitrary references throughout a program’s abstractions. That is, given an object &lt;code&gt;Math&lt;/code&gt; which contains a reference &lt;code&gt;PI&lt;/code&gt;, such that &lt;code&gt;Math.PI&lt;/code&gt; equals 3.14159, it is possible for any piece of code to change that reference, such that &lt;code&gt;Math.PI&lt;/code&gt; might then be 42, or 0, or “some arbitrary value of some arbitrary type.” In fact this practice, “monkey patching” as pythonistas call it, is embraced in many of these communities. You might call this flexibility, but on a good day I call this a poor facility for abstraction, and on other days insanity.&lt;/p&gt;

&lt;p&gt;In a static language, this is simply not allowed. Some languages, specifically &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; (via &lt;a href="http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-6"&gt;implicit conversions&lt;/a&gt;), provide this same kind of structural flexibility without sacrificing static integrity. Static typing has a historical stigma of pain, but it is has improved in leaps and bounds. I only hope we haven’t lost the attention of developers.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Null (i = null; i.toString())&lt;/b&gt;&lt;br/&gt;
JavaScript, like most languages, supports the notion of a null pointer (or null reference). In most languages, including JavaScript, no tool will help you avoid this. Any object can be assigned this value, even in most static languages, in which it is viewed as a subtype of all types. In such an environment, a null pointer is a ticking time bomb. You get no help from tooling, so if you forget to check something (and we do this all the time), you get a runtime error. Seasoned developers struggle with this; it is a non-intuitive problem. On the other hand, many functional languages (like &lt;a href="http://www.scala-lang.org/"&gt;Scala&lt;/a&gt; and &lt;a href="http://www.haskell.org/"&gt;Haskell&lt;/a&gt;), employ &lt;a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)"&gt;monadic types&lt;/a&gt; to remove this problem.&lt;/p&gt;

&lt;p&gt;The problem of null is neither new or superficial. The inventor of this concept, Sir Charles Antony Richard Hoare, &lt;a href="http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare"&gt;spoke to an audience&lt;/a&gt; about the history of null, regarding it as a “billion dollar mistake.” Incidentally, he also says quite a lot about static typing in that talk, so I do suggest watching it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Concluding&lt;/b&gt;&lt;br/&gt;
I think Tantek might be right, even if in a slightly lessened fashion. Further, while the future I want to see differs from the one I feel impending, I’m not in despair. Rather, I’m currently enjoying hacking on NodeJS, all the while seeking out what I see as the ideal future.&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/1203079505</link><guid>http://blog.thegodcode.net/post/1203079505</guid><pubDate>Tue, 28 Sep 2010 01:14:00 -0400</pubDate><category>future</category><category>web</category><category>nodejs</category></item><item><title>"By investigating the logical properties of your programming language and finding out how difficult..."</title><description>“By investigating the logical properties of your programming language and finding out how difficult it would be to prove correctness if you wanted to, you would get an objective measurement of how easy the language was to use correctly.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Tony Hoare&lt;/em&gt;</description><link>http://blog.thegodcode.net/post/1189858304</link><guid>http://blog.thegodcode.net/post/1189858304</guid><pubDate>Sun, 26 Sep 2010 01:59:38 -0400</pubDate><category>type-systems</category><category>compsci</category><category>hoare</category></item><item><title>"If the proof of program correctness requires a very large number of different proof rules, and if..."</title><description>“If the proof of program correctness requires a very large number of different proof rules, and if each proof rule has a lot of side conditions, in particular, if the validity of the local applciation of a rule to a small bit of program depends on properties which can only be established by a scan of the program as a whole, then you know that you’ve done a bad job as a language designer and you do not have to get your customers to tell you that.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Tony Hoare&lt;/em&gt;</description><link>http://blog.thegodcode.net/post/1189841694</link><guid>http://blog.thegodcode.net/post/1189841694</guid><pubDate>Sun, 26 Sep 2010 01:55:00 -0400</pubDate><category>compsci</category><category>type-systems</category><category>hoare</category></item><item><title>erinlewis:

last month’s snow in Florence.
1:15 looks down our...</title><description>&lt;iframe src="http://player.vimeo.com/video/8614220" width="400" height="300" frameborder="0"&gt;&lt;/iframe&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;a href="http://erinlewis.tumblr.com/post/323826711/last-months-snow-in-florence-1-15-looks-down" class="tumblr_blog"&gt;erinlewis&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;last month’s &lt;a href="http://www.florence-on-line.com/video/florence-snow-storm.html"&gt;snow&lt;/a&gt; in Florence.&lt;/p&gt;
&lt;p&gt;1:15 looks down our old street.&lt;/p&gt;
&lt;p&gt;bellissima.&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://blog.thegodcode.net/post/323862296</link><guid>http://blog.thegodcode.net/post/323862296</guid><pubDate>Fri, 08 Jan 2010 16:25:42 -0500</pubDate></item><item><title>A Look at How Scala Compiles to Java</title><description>&lt;p&gt;
Consider this contrived example, based on an example from &lt;a href="http://www.amazon.com/Beginning-Scala-David-Pollak/dp/1430219890/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1257878604&amp;sr=8-1"&gt;Beginning Scala&lt;/a&gt;. The point of the snippet was to demonstrate the congruency between using the &lt;a href="http://en.wikipedia.org/wiki/Higher-order_function"&gt;higher-order functions&lt;/a&gt; &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;flatMap&lt;/code&gt;, &lt;code&gt;foreach&lt;/code&gt;, and &lt;code&gt;filter&lt;/code&gt; (see &lt;a href="http://www.scala-lang.org/docu/files/api/scala/Iterable.html"&gt;Iterable&lt;/a&gt;), and performing the same operations inside a &lt;a href="http://www.scala-lang.org/node/111"&gt;for comprehension&lt;/a&gt;.
&lt;/p&gt;

&lt;pre&gt;
object App {
	
	def isEven(i: Int) = i % 2 == 0
	
	def isOdd(i: Int) = i % 2 == 1
	
	def main(args: Array[String]): Unit = {
		val n = (1 to 10).toList
		n.filter(isEven).flatMap(i =&gt; n.filter(isOdd).map(j =&gt; i * j))
	}
	
}
&lt;/pre&gt;

&lt;p&gt;
Save this code to a file named &lt;code&gt;App.scala&lt;/code&gt;, or anything you want (scala doesn’t have the same file/class name restrictions as java). Assuming you chose App.scala, compile it:
&lt;/p&gt;

&lt;pre&gt;
clewis$ scalac App.scala
&lt;/pre&gt;

&lt;p&gt;
Now check out the generated class files (&lt;code&gt;ls App*class&lt;/code&gt;). You should see the following:
&lt;/p&gt;

&lt;pre&gt;
App$$anonfun$main$1.class
App$$anonfun$main$2$$anonfun$apply$1.class
App$$anonfun$main$2$$anonfun$apply$2.class
App$$anonfun$main$2.class
App$.class
App.class
&lt;/pre&gt;

&lt;p&gt;
Why were 6 classes compiled from this one singleton definition? Let’s start with &lt;code&gt;App.class&lt;/code&gt; and &lt;code&gt;App$.class&lt;/code&gt;.
&lt;/p&gt;

&lt;h3&gt;The Singleton Object&lt;/h3&gt;

&lt;p&gt;
Scala does away with Java’s statics. Instead we get singleton objects, which are declared with a syntax similar to that used for class declarations, but using the &lt;code&gt;object&lt;/code&gt; keyword instead. When a singleton object shares the same name as a class, it becomes that classes “companion” object. Companion objects have special privileges, including access to private instance fields on instances of the companion type. In this example, &lt;code&gt;App&lt;/code&gt; is simply a singleton object, since we haven’t also defined a class named App.
&lt;/p&gt;

&lt;p&gt;
The Scala compiler implements companion objects by generating an anonymous class inside the companion class (the class of the same name as the object). The same is done for singleton objects, but the compiler also generates the containing class. Like the Java compiler, Scala names anonymous the class as [class name]$. Because we created an object named App, we get 2 classes: &lt;code&gt;App&lt;/code&gt; and &lt;code&gt;App$&lt;/code&gt;.
&lt;/p&gt;

&lt;h3&gt;Higher Order Functions and Their Function Arguments&lt;/h3&gt;

&lt;p&gt;
In Scala, functions are objects; instances of one of the Function&lt;i&gt;N&lt;/i&gt; traits. Of course this knowledge isn’t obvious by the Scala syntax, and that’s part of the beauty: it just feels right. The compiler compiles any functions down to anonymous classes inside the containing class. If you understand Java’s scoping rules for inner classes, then you should now have some understanding of how Scala implements closures.
&lt;/p&gt;

&lt;p&gt;
Methods in Scala are also different from functions. Methods are functions defined as part of a class definition with the the &lt;code&gt;def&lt;/code&gt; keyword. Functions are instances of one of the Function&lt;i&gt;N&lt;/i&gt; traits, and the Scala syntax provides several different ways to express them tersely. Methods are the only primitives (non-objects) in Scala, but the compiler makes it easy to promote methods to function instances. Back to our example, notice that we define the methods &lt;code&gt;isEven&lt;/code&gt; and &lt;code&gt;isOdd&lt;/code&gt; in our singleton object. They are method primitives, not functions. Because the compiler recognizes that methods often want to be treated as functions, it makes provisions; one such provision is that we can pass a method to a higher-order function.
&lt;/p&gt;

&lt;h3&gt;Method Promotions&lt;/h3&gt;

&lt;p&gt;
In order for the compiler to promote a method to a function instance, it must create an anonymous class for that instance. In our example, we first pass the method &lt;code&gt;isEven&lt;/code&gt; as an argument to the higher-order function &lt;code&gt;filter&lt;/code&gt;, and so the compiler generated the class &lt;code&gt;App$$anonfun$main$1&lt;/code&gt;, which is our promoted method. Note that we also pass the &lt;code&gt;isOdd&lt;/code&gt; to a subsequent call to &lt;code&gt;filter&lt;/code&gt;, a promotion for which the compiler generated the class &lt;code&gt;App$$anonfun$main$2$$anonfun$apply$1&lt;/code&gt;.
&lt;/p&gt;

&lt;h3&gt;Function Literals&lt;/h3&gt;

&lt;p&gt;
We’ve covered all but 2 of the generated classes. As it turns out, there are still 2 functions we haven’t discussed in the series of transformations. Take another look:
&lt;/p&gt;

&lt;pre&gt;
n.filter(isEven).flatMap(i =&gt; n.filter(isOdd).map(j =&gt; i * j))
&lt;/pre&gt;

&lt;p&gt;
Did you see them? If you’re new to Scala, you may not spot them at first because they are using function-literal syntax. The functions &lt;code&gt;flatMap&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt; also take functions as arguments, and so we define them literally, embedding the one passed to &lt;code&gt;map&lt;/code&gt; inside the one passed to &lt;code&gt;flatMap&lt;/code&gt;. For these two function literals, the compiler generated the anonymous classes &lt;code&gt;App$$anonfun$main$2&lt;/code&gt; and &lt;code&gt;App$$anonfun$main$2$$anonfun$apply$2&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
To seal in what’s happening here, fire up the Scala REPL and run this code:
&lt;/p&gt;

&lt;pre&gt;
List(1,2,3).map(j =&gt; j + 1)
&lt;/pre&gt;

&lt;p&gt;Now run this code:&lt;/p&gt;

&lt;pre&gt;
List(1,2,3).map(new Function1[Int, Int] {
	override def apply(j: Int) = j + 1
})
&lt;/pre&gt;

&lt;p&gt;
The two do the exact same thing, because they are exactly the same.
&lt;/p&gt;

&lt;p&gt;
The literal syntax may look strange, and even non-obvious at first. I thought so too, but it didn’t take long for me to greatly appreciate and recognize it as easily as you might recognize a class definition.
&lt;/p&gt;

&lt;h3&gt;A Word on the Class Names&lt;/h3&gt;

&lt;p&gt;
You may have noticed a pattern in the anonymous function class names. For starters, they’re prefixed with &lt;code&gt;App$$anonfun$main$&lt;/code&gt; and then continue in with &lt;code&gt;1.class&lt;/code&gt; and &lt;code&gt;2.class&lt;/code&gt;. There’s also another level nested inside the first level. If you inspect each of these using &lt;code&gt;javap -c [class]&lt;/code&gt;, you’ll notice the ordering and nesting follows the order in which we use function objects (by passing them as arguments to other functions).
&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;
&lt;code&gt;isEven&lt;/code&gt; is the first function we pass, so it is the first function for which an anonymous class is generated, yielding &lt;code&gt;App$$anonfun$main$1&lt;/code&gt;.
&lt;/li&gt;

&lt;li&gt;
The second is the literal function passed to &lt;code&gt;flatMap&lt;/code&gt;, so &lt;code&gt;App$$anonfun$main$2&lt;/code&gt; is generated for it.
&lt;/li&gt;
&lt;li&gt;
The third is &lt;code&gt;isOdd&lt;/code&gt;, yielding the class &lt;code&gt;App$$anonfun$main$2$$anonfun$apply$1&lt;/code&gt;. This function is used inside the function passed to &lt;code&gt;flatMap&lt;/code&gt;. It’s the first function in this scope, and it’s nested, so its name reflects its nesting and order in the sequence.
&lt;/li&gt;
&lt;li&gt;
Finally we arrive at the literal function passed to &lt;code&gt;map&gt;&lt;/code&gt;, resulting in &lt;code&gt;App$$anonfun$main$2$$anonfun$apply$2&lt;/code&gt;. It’s the second function referenced inside the function passed to &lt;code&gt;flatMap&lt;/code&gt;, and so its name also reflects its nesting and order.
&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;
This ordering and nesting is deliberate. In Java, an inner class has access to its parent’s scope, which includes class instance variables and local variables (if the inner class was created in a method). This is what makes closures in Scala possible. Look again at the literal function passed to &lt;code&gt;map&lt;/code&gt;:
&lt;/p&gt;

&lt;pre&gt;
n.filter(isEven).flatMap(i =&gt; n.filter(isOdd).map(j =&gt; i * j))
&lt;/pre&gt;

&lt;p&gt;
The function multiplies a “free” variable &lt;code&gt;i&lt;/code&gt; by its single argument &lt;code&gt;j&lt;/code&gt;. Where did &lt;code&gt;i&lt;/code&gt; come from? Again, this function is defined inside another literal function passed to &lt;code&gt;flatMap&lt;/code&gt;. That function receives a single argument, which it labels &lt;code&gt;i&lt;/code&gt;. Because the function passed to &lt;code&gt;map&lt;/code&gt; is nested inside the one passed to &lt;code&gt;flatMap&lt;/code&gt;, it has access to that scope.
&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/239967776</link><guid>http://blog.thegodcode.net/post/239967776</guid><pubDate>Wed, 11 Nov 2009 00:43:24 -0500</pubDate><category>scala</category><category>java</category></item><item><title>Lift Off</title><description>&lt;p&gt;I arrived at the FGM building by way of my couchsurfing host, Jenny. At the moment, Reston looks a bit like London (at least if you look straight up). Fall has painted the leaves brilliantly, a sight I’m left wanting in Florida.&lt;/p&gt;

&lt;p&gt;Jenny graciously dropped me off at the FGM building on her way to school (she’s a school teacher). I gladly accepted arriving an hour early for a free ride, and so I just started wandering the office park. I stumbled on this office “cafe” as it were, and here I sit. I seem to be just outside a free guest wifi network, but that’s ok. For a first-timer at an unconference, or any tech conference for that matter, I feel fairly prepared.&lt;/p&gt;

&lt;p&gt;I’ve just made my way into the FGM suite. So far we’re about 32-30 strong!&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/227895642</link><guid>http://blog.thegodcode.net/post/227895642</guid><pubDate>Fri, 30 Oct 2009 08:54:35 -0400</pubDate><category>scala</category><category>lift</category></item><item><title>Functional Content Negotiation in Scala</title><description>&lt;p&gt;Today I needed some code to assess if an HTTP request matched certain criteria, based on its declared content type. Essentially, I needed to know if the user-agent wanted XML (or JSON), and that it wanted only that type.&lt;/p&gt;
&lt;p&gt;Requirements:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Check the &lt;code&gt;Accept&lt;/code&gt; request header for a given content type.&lt;/li&gt;
&lt;li&gt;Support a non-standard &lt;code&gt;X-Accept&lt;/code&gt; header serving the same purpose as &lt;code&gt;Accept&lt;/code&gt; (IE doesn’t let XmlHttpRequest specify the Accept header).&lt;/li&gt;
&lt;li&gt;The header value is specifically required to be one, and only one MIME type, without an explicit “q” value.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Assuming we have an &lt;code&gt;HttpServletRequest&lt;/code&gt; instance that only accepts &lt;code&gt;application/xml&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;scala&gt; req.getHeader("Accept")&lt;br/&gt;res6: String = application/xml&lt;/pre&gt;
&lt;p&gt;and that we have an object with a method implementing the match algorithm, we should see this behavior:&lt;/p&gt;
&lt;pre&gt;scala&gt; matcher accepts(req, "application/xml")&lt;br/&gt;res7: Boolean = true&lt;br/&gt;&lt;br/&gt;scala&gt; matcher accepts(req, "application/json")&lt;br/&gt;res8: Boolean = false&lt;/pre&gt;
&lt;p&gt;The complete code:&lt;/p&gt;
&lt;pre&gt;type HttpServletRequest = {&lt;br/&gt;  def getHeader(h: String): String&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;object req {&lt;br/&gt;  def getHeader(h: String) = h match {&lt;br/&gt;    case "Accept" =&gt; "application/xml"&lt;br/&gt;    case "X-Accept" =&gt; "text/html"&lt;br/&gt;    case _ =&gt; null&lt;br/&gt;  }&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;trait Acceptor {&lt;br/&gt;&lt;br/&gt;  val headers = "Accept" :: "X-Accept" :: Nil&lt;br/&gt;&lt;br/&gt;  def accepts(request: HttpServletRequest, contentType: String) =&lt;br/&gt;    headers map (request.getHeader) filter(_ != null) map {&lt;br/&gt;      _ == contentType&lt;br/&gt;    } reduceLeft(_ || _)&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;object matcher extends Acceptor&lt;/pre&gt;
&lt;p&gt;Not much, but let’s look at it more closely.&lt;/p&gt;
&lt;pre&gt;type HttpServletRequest = {&lt;br/&gt;  def getHeader(h: String): String&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;A type alias. This is a structural type, aliased as &lt;code&gt;HttpServletRequest&lt;/code&gt;, with a method &lt;code&gt;getHeader(String): String&lt;/code&gt;. This makes it easy to prototype the code in the scala REPL, or compile it against &lt;code&gt;javax.servlet.http.HttpServletRequest&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next, a singleton instance of the aliased type:&lt;/p&gt;
&lt;pre&gt;object req {&lt;br/&gt;  def getHeader(h: String) = h match {&lt;br/&gt;    case "Accept" =&gt; "application/xml"&lt;br/&gt;    case "X-Accept" =&gt; "text/html"&lt;br/&gt;    case _ =&gt; null&lt;br/&gt;  }&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;This conforms to our requirements above, and also behaves like a real HttpServletRequest in that it returns the dreaded null if no header by the provided name exists.&lt;/p&gt;
&lt;p&gt;Next is the trait:&lt;/p&gt;
&lt;pre&gt;trait Acceptor {&lt;br/&gt;&lt;br/&gt;  val headers = "Accept" :: "X-Accept" :: Nil&lt;br/&gt;&lt;br/&gt;  def accepts(request: HttpServletRequest, contentType: String) =&lt;br/&gt;    headers map (request.getHeader) filter(_ != null) map {&lt;br/&gt;    _ == contentType&lt;br/&gt;  } reduceLeft(_ || _)&lt;br/&gt;&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;Notice the headers val. This is a list of strings containing the header names to read from the request and interpret as &lt;code&gt;Accept&lt;/code&gt; header values.&lt;/p&gt;
&lt;p&gt;The fun part is the accepts method, which takes a servlet request (conveniently aliased for REPL play), and a content (MIME) type. This is purely functional; there are no side-affects, it is deterministic, and the algorithm is a series of four transformations occurring on one collection after another. Through the transformations, the collection is reduced to a single boolean value. Let’s start with the first line:&lt;/p&gt;
&lt;pre&gt;headers map (request.getHeader) filter(_ != null)&lt;/pre&gt;
&lt;p&gt;Starting with the list of header names to inspect in the request,  we map it with &lt;code&gt;request.getHeader&lt;/code&gt;. This creates a new list, where each element is the result of applying &lt;code&gt;request.getHeader&lt;/code&gt; to each element in the original headers list. The original list contains 2 elements, “Accept” and “X-Accept”, and so the resulting list now looks like this: &lt;code&gt;List(application/xml, text/html)&lt;/code&gt;. Our request object responded to the query for “Accept” with “application/xml”, and “text/html” to “X-Accept.” Had either of those header names not been present, a &lt;code&gt;null&lt;/code&gt; value would now be present in the list.&lt;/p&gt;
&lt;p&gt;Now we filter this resulting list. Filter, like map, applies a function to each element in the collection and results in a new collection. The function receives each element and returns a boolean value. For each invocation that results in true, that element is added to the resulting list (you filter a collection using a function). We use a literal shorthand to assert that the element is not null, and so the resulting list will contain non-null strings. This filtering is simply protection against null. Our transformed list doesn’t contain any nulls, so the new list looks the same as the old one: &lt;code&gt;List(application/xml, text/html)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next we have another mapping operation, using brackets instead of parentheses.&lt;/p&gt;
&lt;pre&gt;map {&lt;br/&gt;  _ == contentType&lt;br/&gt;}&lt;/pre&gt;
&lt;p&gt;The use of brackets here is a style choice, and in this case is the same as writing &lt;code&gt;map(_ == contentType)&lt;/code&gt;. This part also evolved from a &lt;i&gt;partial function&lt;/i&gt;, where the body is a case statement, matching on the single argument. The function being mapped simply checks if each element is equal to the &lt;code&gt;contentType&lt;/code&gt; argument. Remember that == in scala, contrary to java, is an object equality test and not an identity test (eq is for identity tests). We left off at &lt;code&gt;List(application/xml, text/html)&lt;/code&gt;, and after this mapping transformation, arrive at List(true, false).&lt;/p&gt;
&lt;p&gt;The final transformation, simple as it is, actually took me a bit to realize. The function itself had undergone a few iterations, each representing in some way, a group of header values in the request that did or didn’t match a given content type. I couldn’t seem to reach a solution that felt natural. I thought about filtering on true, and then checking that the result list wasn’t empty, but that didn’t really express the essence of the computation. Same for a find (and then matching on Option(true)). They would have worked, but they missed the point and, as all point-missers do, cluttered the algorithm.&lt;/p&gt;
&lt;p&gt;Then it hit me:&lt;/p&gt;
&lt;pre&gt;reduceLeft(_ || _)&lt;/pre&gt;
&lt;p&gt;Exactly what I wanted. &lt;a href="http://www.scala-lang.org/docu/files/api/scala/List.html#reduceLeft%28%28B%2CA%29%3D%3EB%29"&gt;reduceLeft&lt;/a&gt; receives a 2 argument function, walks the contents of the collection, and returns the accumulated result. On each invocation, it passes the current and the next element as arguments, progressing from left to right, until the collection is exhausted. The result of one invocation becomes the input to the next. As a simple example, consider list summations:&lt;/p&gt;
&lt;pre&gt;scala&gt; List(1,2,3) reduceLeft(_ + _)&lt;br/&gt;res14: Int = 6&lt;br/&gt;&lt;br/&gt;scala&gt; List(1,2,3,4) reduceLeft(_ + _)&lt;br/&gt;res15: Int = 10&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;Back to our exmaple, using function-literal shorthand, I OR the two arguments. This means that if either of the two are true, the resulting value is true. Reducing a list of booleans with this function results in a single boolean value. Because I’m using OR, a single true value in the list will result in a true result at the end of the reduction. We left off at a &lt;code&gt;List(true, false)&lt;/code&gt;, and after our reduction, arrive at a single &lt;code&gt;true&lt;/code&gt; result. Perfect.&lt;/p&gt;
&lt;p&gt;Last of all, a singleton that mixes in the Acceptor trait.&lt;/p&gt;
&lt;pre&gt;object matcher extends Acceptor&lt;/pre&gt;
&lt;p&gt;This just gives us an object with Acceptor behavior mixed in, so we can play in the REPL.&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/214321293</link><guid>http://blog.thegodcode.net/post/214321293</guid><pubDate>Thu, 15 Oct 2009 22:44:00 -0400</pubDate><category>scala</category><category>functional</category></item><item><title>"Great minds discuss ideas; Average minds discuss events; Small minds discuss people."</title><description>“Great minds discuss ideas; Average minds discuss events; Small minds discuss people.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Eleanor Roosevelt&lt;/em&gt;</description><link>http://blog.thegodcode.net/post/211562734</link><guid>http://blog.thegodcode.net/post/211562734</guid><pubDate>Mon, 12 Oct 2009 21:59:25 -0400</pubDate><category>quotes</category><category>culture</category></item><item><title>A Solution to the Set Combinations Problem</title><description>&lt;p&gt;&lt;pre&gt;def combine[T](sets: List[List[T]]): List[List[T]] = sets match {
  case List(l1, l2) =&gt; l1.flatMap(i1 =&gt; l2.map(List(i1, _)))
  case head :: tail =&gt; combine(tail).flatMap(set =&gt; head.map(_ :: set))
  case _ =&gt; sets
}
&lt;/pre&gt;&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/211561726</link><guid>http://blog.thegodcode.net/post/211561726</guid><pubDate>Mon, 12 Oct 2009 21:58:13 -0400</pubDate><category>scala</category><category>recursion</category></item><item><title>Recursion with an Arbitrary Number of Lists</title><description>&lt;p&gt;{ apple, banana }&lt;br/&gt;{ milk, cheese, yogurt }&lt;br/&gt;{ nuts, legumes }&lt;/p&gt;
&lt;p&gt;— would result in —&lt;/p&gt;
&lt;p&gt;{ apple, milk, nuts }&lt;br/&gt;{ apple, milk, legumes }&lt;br/&gt;{ apple, cheese, nuts }&lt;br/&gt;{ apple, cheese, legumes }&lt;br/&gt;{ apple, yogurt, nuts }&lt;br/&gt;{ apple, yogurt, legumes }&lt;br/&gt;{ banana, milk, nuts }&lt;br/&gt;{ banana, milk, legumes }&lt;br/&gt;{ banana, cheese, nuts }&lt;br/&gt;{ banana, cheese, legumes }&lt;br/&gt;{ banana, yogurt, nuts }&lt;br/&gt;{ banana, yogurt, legumes }&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/200207603</link><guid>http://blog.thegodcode.net/post/200207603</guid><pubDate>Tue, 29 Sep 2009 13:00:18 -0400</pubDate><category>recursion</category><category>func</category><category>Functional</category></item><item><title>Dependency Injection Using Language-only Constructs</title><description>&lt;p&gt;I’ve been exploring dependency injection in Scala. Digesting &lt;a href="http://debasishg.blogspot.com/2008/02/scala-to-di-or-not-to-di.html"&gt;this article&lt;/a&gt; by Debasish Ghosh, I was inspired to play with the code. At the time of the article’s posting it needed a few pieces to compile. You can find the inferred working source &lt;a href="http://bit.ly/discala"&gt;here&lt;/a&gt;. As an exploratory exercise, I transposed it to &lt;a href="http://bit.ly/dijava"&gt;java&lt;/a&gt;. Chew on it a bit; it clarified some things for me about both the technique and the workings of Scala abstractions.&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/164543087</link><guid>http://blog.thegodcode.net/post/164543087</guid><pubDate>Sun, 16 Aug 2009 22:29:40 -0400</pubDate><category>scala</category><category>java</category><category>dependencyinjection</category><category>ioc</category></item><item><title>Vote for ROSA LOVES!</title><description>&lt;a href="http://www.nau.com/collective/grant-for-change/rosa-loves;-mike-fretto-381.html.share"&gt;Vote for ROSA LOVES!&lt;/a&gt;</description><link>http://blog.thegodcode.net/post/143006531</link><guid>http://blog.thegodcode.net/post/143006531</guid><pubDate>Thu, 16 Jul 2009 16:36:15 -0400</pubDate></item><item><title>Super quick start with Lift's ProtoUser</title><description>&lt;p&gt;&lt;a href="http://liftweb.net/"&gt;Lift&lt;/a&gt; provides a trait called &lt;a href="http://scala-tools.org/scaladocs/liftweb/1.0/net/liftweb/mapper/ProtoUser.html"&gt;ProtoUser&lt;/a&gt;, which helps you in quickly adding base functionality to your application for user-specific data. The &lt;a href="http://groups.google.com/group/the-lift-book"&gt;Lift book&lt;/a&gt; mentions this class very briefly, but it makes the assumption that the reader will be to intuit how to complete the example code to have a base starting point. Here’s what you must have (assuming this is in the model directory):&lt;/p&gt;
&lt;p&gt;package app.model&lt;br/&gt;&lt;br/&gt;class User extends ProtoUser[User] with LongKeyedMapper[User] {&lt;br/&gt; def getSingleton = User&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;object User extends User with LongKeyedMetaMapper[User]&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;You need to mix in the LongKeyedMapper trait, and you need the User singleton.&lt;/p&gt;</description><link>http://blog.thegodcode.net/post/141132296</link><guid>http://blog.thegodcode.net/post/141132296</guid><pubDate>Mon, 13 Jul 2009 21:44:00 -0400</pubDate><category>scala</category><category>lift</category></item></channel></rss>

