<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Kotan Code　枯淡コード</title>
	
	<link>http://www.kotancode.com</link>
	<description>In search of simple, elegant code</description>
	<lastBuildDate>Tue, 15 May 2012 15:15:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/KotanCode" /><feedburner:info uri="kotancode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Upgraded my iPhone to a Nokia Lumia 900 Windows Phone</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/sgmWTsJoYK8/</link>
		<comments>http://www.kotancode.com/2012/05/15/upgraded-my-iphone-to-a-nokia-lumia-900-windows-phone/#comments</comments>
		<pubDate>Tue, 15 May 2012 15:15:11 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[lumia]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[windowsphone]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=466</guid>
		<description><![CDATA[Those of you who have been following my blog and my tweets know that I&#8217;m something of a polyglot and polyphone - I program in many different languages and I&#8217;ve used many different phones. I used the Palm Pre and it&#8217;s unified contacts and messaging systems years ago, before such a feature was even a twinkle [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignright" style="width: 358px"><img class=" " title="Nokia Lumia 900" src="http://cdn.slashgear.com/wp-content/uploads/2012/01/nokia_lumia_900_black-580x471.jpg" alt="Nokia Lumia 900" width="348" height="283" /><p class="wp-caption-text">Nokia Lumia 900</p></div>
<p>Those of you who have been following my blog and my tweets know that I&#8217;m something of a polyglot and <em>polyphone</em> - I program in many different languages and I&#8217;ve used many different phones. I used the Palm Pre and it&#8217;s unified contacts and messaging systems years ago, before such a feature was even a twinkle in the eye of either Apple or Microsoft. I&#8217;ve owned and developed for the original iPhone, the 3, the 3G, the 4, and the 4S. I&#8217;ve owned an iPad 1, an iPad 2, and &#8220;the new iPad formerly called the iPad 3&#8243;. I&#8217;ve owned the Samsung Windows Phone and now own the Nokia Lumia 900.</p>
<p>The first thing to notice about the Lumia 900 is that it is <em>big</em>. It&#8217;s not just that it looks big, but it <em>feels</em> big. It&#8217;s heavier than the iPhone and takes up substantially more palm space. Back when I first held one of the Lumias in my hands, I was originally turned off by this &#8211; the last thing I wanted was a <em>bigger</em> phone. Now that I&#8217;ve had a chance to use the phone for more than just a quick in-store demo, I have completely changed my mind on that. The size, weight, and overall feel of this device is <em>absolutely perfect</em>.</p>
<p>The next thing I noticed about this phone is that it&#8217;s <em>fast</em>. Not just kinda fast or faster than my iPhone but it is <em>blazingly</em> fast. No matter what I do I can&#8217;t get any of the UI to skip, jitter, pause, or have any of the other weird issues that my Samsung used to have. Couple that with the brilliant, giant display and this phone is a freaking <em>joy</em> to use.</p>
<p>Even with all of iOS&#8217; recent advances, I still hate the walled-off silo feeling you get from each application where even the ones that have been modified to work together still feel like a hack. This is in stark contrast to the smooth, effortless way I can sift through Facebook, LinkedIn, and Twitter updates from all of my contacts in a single place. As mentioned in previous blog posts, Windows Phone is designed specifically to get out of my way and give me what I want, when I want it, in an elegantly minimalist way.</p>
<p>Specific to the Nokia is the &#8220;Nokia Blue&#8221; color scheme, which is by far my favorite of any of the stock color options. Other things specific to the Nokia are a pile of awesome ring tones and notification sounds that are far better than the stock Microsoft options. Finally, the other thing that Nokia did that they deserve <strong><em>huge</em></strong> credit for is the &#8220;App Highlights&#8221; app.</p>
<p>I mentioned in a <a title="Windows Phone 7 Marketplace Continues to Disappoint" href="http://www.kotancode.com/2012/04/08/wp7-marketplace-disappoint/" target="_blank">previous blog post</a> that the state of the Windows Phone marketplace is disgusting at the moment. It is littered with crap, more crap, porn, some more crap, first party games, and a few gems struggling to shine in a sea of even more crap. Nokia has human curators who troll the marketplace and find the best of the best, the stuff we&#8217;re all looking for but don&#8217;t know we&#8217;re looking for it how to find it. Until the Marketplace gets better, I&#8217;ll be using the App Highlights app to find new stuff, not the Marketplace. I trust Nokia&#8217;s curators more than I do the automated ranking/relevance algorithm currently screwing the marketplace.</p>
<p>Something that Windows Phones have been doing for a while now that <em>no</em> iOS device does is bluetooth audio text-to-speech reading aloud of your incoming text messages. In addition, you can dictate outbound text messages, which makes for an <em>awesome</em> experience in a car that has bluetooth. I can make and receive calls and texts smoothly using nothing but audio and speech controls. The Lumia has this feature the same as other Windows Phone 7+ devices.</p>
<p>If you are an &#8220;app addict&#8221; like so many iOS users, where your smart phone experience has nothing to do with the phone and everything to do with the apps, then you may still find Windows Phone lacking. However, if you actually want to use the &#8220;smart&#8221; in your &#8220;smart phone&#8221;, then you&#8217;ll not find a better device than the Nokia Lumia 900.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/sgmWTsJoYK8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/05/15/upgraded-my-iphone-to-a-nokia-lumia-900-windows-phone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/05/15/upgraded-my-iphone-to-a-nokia-lumia-900-windows-phone/</feedburner:origLink></item>
		<item>
		<title>To Actor or not to Actor, that is the Question</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/eqIF_NjsPuw/</link>
		<comments>http://www.kotancode.com/2012/05/01/to-actor-or-not-to-actor-that-is-the-question/#comments</comments>
		<pubDate>Tue, 01 May 2012 15:15:27 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Kotan]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[actors]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[pop]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=459</guid>
		<description><![CDATA[When I first starting playing around with Akka, I was definitely swimming upstream. I was learning Scala and learning Akka and coming from a very non-functional, non-actor-oriented background. The more I learned about Scala and the more I learned about Akka, I started noticing some places where I had made rather large architectural mistakes. One [...]]]></description>
			<content:encoded><![CDATA[<p>When I first starting playing around with Akka, I was definitely swimming upstream. I was learning Scala and learning Akka and coming from a very non-functional, non-actor-oriented background. The more I learned about Scala and the more I learned about Akka, I started noticing some places where I had made rather large architectural mistakes.</p>
<p>One thing that I did with Akka 1.x was I exposed public state properties on various actors. While this fixed my problem, it was a bad smell that I didn&#8217;t notice at the time. In my ScalaMUD sample, I switched and used an implicit that wraps an external hash map to maintain state of actors. I now even think this is a bad idea and in a new project (yet to be disclosed) I am currently trying to avoid exposing -any- public state on any actors.</p>
<p>The current challenge that I seem to be running into is in figuring out the level of granularity of my actor system. In other words, how do I decide when to create more actors or when to do the work necessary within a <strong>def</strong>  as a plain Scala function?</p>
<p>I&#8217;ve come up with the following set of questions that I ask myself now:</p>
<ol>
<li><strong>Should this work take place concurrently?</strong>  If the work in question is something that I think should be fired off in the background, then this is a huge argument leaning toward the use of an Actor.</li>
<li><strong>Do multiple other actors need this service performed?</strong> If the code I&#8217;m about to put into a function is something that other actors in the system also need done, then I should really consider using an Actor that performs the work on demand by receiving a message rather than a function. That said, keep in mind that multiple Actors can all make use of shared Scala objects/classes &#8211; so re-evaluate the problem against other criteria in this list as well.</li>
<li><strong>Is this operation purely synchronous or request/response?</strong> If so, then you might not need or want to use an actor for it. Sure, there is plenty of syntactical support for request-and-await-reply blocking calls in Akka but the more of those you do, the uglier and harder to read your code gets. If you really need to perform a synchronous, blocking functional call then it might be better as a function and not an Actor.</li>
<li><strong>Is this really a data storage/retrieval operation?</strong> If the work you&#8217;re considering offloading to an Actor is actually data storage like putting and getting from hash tables or something similar then you might not need an Actor for it. That said, if the act of storing or retrieving might take a while and you don&#8217;t want to block during this, sending a message to a &#8220;data actor&#8221; and going about your business might actually be the best thing for it.</li>
<li><strong>Does my current Actor respond to too many messages?</strong> One thing I&#8217;ve noticed during my work with ScalaMUD is that I&#8217;ve created Actors that do too much. Actors, in my experience, seem to be work better the smaller and more purpose-built they are. The actor system has a robust supervisory system that makes actors that control other actors not only possible, but a best practice. I&#8217;ve gotten tremendous value from refactoring one big Actor into multiple smaller actors.</li>
<li><strong>Do I need supervisory control over a service that is being performed?</strong> As mentioned above, if there is some service that needs to be performed but you want control over how your application reacts to failures like exceptions, then using a supervisor and wrapping that service as an Actor and setting a control policy can do you a world of good. For example, you can set it so the supervisor receives messages when the child throws an exception, or you can have Akka automatically restart dead child actors. The possibilities are as powerful as they are varied.</li>
<li><strong>Does this operation need to be thread-safe?</strong> If the answer is yes, then you might want to use an actor. The reason for this is that actors pull their messages out of queues in a single thread, which means no work you do in response to receiving a message is ever re-entrant, so you don&#8217;t need locks or critical sections. This alone is quite possibly one of the single biggest advantages of actor-based programming and design.</li>
</ol>
<p>Obviously your mileage may vary and I am still an Akka/Scala newbie but what I have found, especially with my newest project, is that my application almost designs itself automatically when I think in terms of autonomous, supervisory-linked actors rather than thinking in terms of traditional OOP.</p>
<p>I recently tweeted the following statement and I still believe it is true: <em><strong>Actors are to OOP what OOP was to procedural programming.</strong></em></p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/eqIF_NjsPuw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/05/01/to-actor-or-not-to-actor-that-is-the-question/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/05/01/to-actor-or-not-to-actor-that-is-the-question/</feedburner:origLink></item>
		<item>
		<title>Initial thoughts on the new iPad</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/44PXzUakM5U/</link>
		<comments>http://www.kotancode.com/2012/04/08/initial-thoughts-on-the-new-ipad/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 14:09:55 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[ipad3]]></category>
		<category><![CDATA[tablet]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=456</guid>
		<description><![CDATA[Yesterday I received an early birthday present, a white 32GB WiFi iPad 3. To give you some proper background, I have owned at least one of all generations of the iPhone except the current 4s (the &#8220;s&#8221; being for &#8220;Siri&#8221;). I have also owned a first-generation iPad and, until yesterday, my sole tablet device was [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I received an early birthday present, a white 32GB WiFi iPad 3. To give you some proper background, I have owned at least one of all generations of the iPhone except the current 4s (the &#8220;s&#8221; being for &#8220;Siri&#8221;). I have also owned a first-generation iPad and, until yesterday, my sole tablet device was an iPad 2. I love my iPad 2. I use it for nearly everything that I can and only resort to the laptop when I plan on doing many hours of writing or if I need to do some actual coding. In other words, you can pry my iPad from my cold, dead hands.</p>
<p>When the &#8220;shiny new&#8221; iPad was announced my first reaction was &#8220;meh&#8221;. I really wasn&#8217;t all that impressed. Sure, it had a shiny new retina display but the difficulty Apple has to overcome isn&#8217;t convincing people that an iPad is awesome, it&#8217;s convincing owners of an iPad 2 that an iPad 3 is awesome enough to warrant forking over what I believe is a flaming truckload of cash. While these things are awesome, the only reason I can even condone purchasing one for myself is that they are tax deductions for my freelance development business and technical writing.</p>
<p>Case in point &#8211; I wouldn&#8217;t buy it for myself, it had to be given to me as an early birthday present. So, is the hype on the iPad 3 worth it?</p>
<p>The first thing I noticed when I unboxed the new iPad is that it&#8217;s heavy. Typically you have to put new devices on a scale to notice the different in weight but, even just holding one iPad in either hand, it&#8217;s <em>very</em> obvious that the new iPad weighs a bit more. It&#8217;s not enough so that it&#8217;s going to stop me from using it but it does have a more noticeable heft to it.</p>
<p>The next thing I noticed was that when setting up the new iPad, it asked me if I wanted to enable dictation. Dictation? I&#8217;d never seen this option on the iPad 2 and I didn&#8217;t remember hearing about it in the rumor mill. As a writer, the idea of getting dictation services on my iPad is pretty appealing. While dictation is there, iPad 3 has no &#8220;Siri&#8221; on it, which is fine by me. In another blog post, I will post a review of the dictation ability from an author&#8217;s point of view rather than just someone wanting to dictate an e-mail message.</p>
<p>Everyone raves about the new horsepower of the device &#8211; it has a better processor in it with more cores blah blah&#8230; For most applications that I tried I couldn&#8217;t tell the difference between how they ran on the iPad 2 and how they ran on the new device. The only ones that showed noticeable differences were the higher-end apps like 3d games and video streaming apps. A large portion of what I use my iPad for is watching video so this is a good thing for me. I&#8217;ve actually seen some videos of people doing speed tests where the iPad 2 comes out ahead in most situations. My experience is that the 3 has a much better display and is no slower, and visibly faster in video rendering, so that&#8217;s a plus.</p>
<p>All in all I&#8217;m really happy with the device and can&#8217;t wait to try out the dictation feature &#8211; of course you can&#8217;t use dictation on a train or a plane without annoying your neighbors so I wonder about where I might be able to use it. Is the new iPad 3 worth all the hype? Maybe, it&#8217;s an upgrade but a minor one. Is it worth the money? I don&#8217;t think so, but that hasn&#8217;t stopped me from owning it <img src='http://www.kotancode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/44PXzUakM5U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/04/08/initial-thoughts-on-the-new-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/04/08/initial-thoughts-on-the-new-ipad/</feedburner:origLink></item>
		<item>
		<title>Windows Phone 7 Marketplace Continues to Disappoint</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/B8UJ0fFn61Y/</link>
		<comments>http://www.kotancode.com/2012/04/08/wp7-marketplace-disappoint/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 13:20:35 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[appstore]]></category>
		<category><![CDATA[marketplace]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[store]]></category>
		<category><![CDATA[windowsphone]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=453</guid>
		<description><![CDATA[When Windows Phone 7 first came out, I remember all of the skeptics and the members of the &#8220;cult of Apple&#8221;, and even the Android fans, all telling me that WP7 was a passing fad and  that Microsoft had wasted its chance to enter the mobile phone market with its failed mobile Operating Systems, &#8220;PocketPC&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>When Windows Phone 7 first came out, I remember all of the skeptics and the members of the &#8220;cult of Apple&#8221;, and even the Android fans, all telling me that WP7 was a passing fad and  that Microsoft had wasted its chance to enter the mobile phone market with its failed mobile Operating Systems, &#8220;PocketPC&#8221; and &#8220;Windows Mobile&#8221;. I told them all they were insane, because Windows Phone was awesome.</p>
<p>I still firmly believe this. From an overall experience point of view, Windows Phone allows me smoother, faster, more efficient access to everything I need than iOS. My common everyday workflows on a Windows Phone are faster and less intrusive than they are on an iOS device (I won&#8217;t even talk about Android&#8230; I can barely stomach 30 seconds of interaction with most droids).</p>
<p>So, if I still think that the core of Windows Phone 7 is so awesome, why is the word <em>disappoint</em> in the title of this blog post? The answer is simple: the <em>marketplace</em> or the &#8220;app store&#8221;. Today, long after the &#8220;it&#8217;s just a v1 product&#8221; excuse has expired, I can go to the Windows Phone marketplace and I become nauseous with what I see there.</p>
<p>Firstly, if you look at the list of top games in the Marketplace, they are either first-party (Microsoft made them or paid someone else to make them under their iron fist) or they are made by giant third-parties like Electronic Arts or any of it&#8217;s 80 bajilion subsidiaries. You still see a few good examples of non-MS games but, the point remains &#8211; Microsoft and the other gaming giants pretty much own that segment of the Marketplace. This is completely explainable &#8211; making a game for a mobile platform is a pure and simple ROI (Return on Investment) calculation. How many potential buyers are there * price per game &#8211; production cost total / potential buyers = potential profit. The reason we don&#8217;t see as many ports of ridiculously popular iOS games is simple: the ports would <em>probably</em> cost more to make than they would earn. Note <em>probably</em> there, because that notion is very subjective and easily influenced by the paranoia of any given decision maker. Bottom line? Developers are still skeptical.</p>
<p>Secondly is the <strong>experience</strong> of the marketplace. When I flip through Apple&#8217;s App Store, I see a wide variety of great stuff &#8211; so much stuff, in fact, that I can&#8217;t possibly sift through it all. However, I feel somewhat confident that Apple&#8217;s &#8220;featured&#8221;, &#8220;popular&#8221;, and other filtering and sorting systems will allow me to find relatively good examples of the type of application I am looking for.</p>
<p>Today, when I go sifting through the Windows Phone marketplace, I feel like I&#8217;m walking through the &#8220;old times square&#8221;. You know, the one that used to be peppered on all sides by strip clubs, peep shows, adult video stores, with a drug dealer on every corner. I&#8217;m not exaggerating &#8211; In the &#8220;most popular&#8221; filter for most of the apps in the marketplace I regularly skim past 4 or 5 apps with half-naked women on their icon. I understand that Microsoft may not have full control over this because, after all, if boobs are popular, then boobs are popular and what can they do about it??</p>
<p>The problem is that I can&#8217;t see the forest for the trees in their marketplace. I cannot find <em>anything</em> with ease and often some of the most amazing Windows Phone apps don&#8217;t show up anywhere &#8211; I have to search for them by obtuse keywords that friends have given me who told me about these great applications. Not only is the marketplace diluted with immature fart apps, &#8220;boobs&#8221; apps, and other garbage for which I have no use, but the worst offense of all is that the marketplace is <em>sinking under a deluge of irrelevant stuff</em> that they could easily filter out. It would literally take them a few hours. But they don&#8217;t do it.</p>
<p>Every single time I go to the marketplace to check out what&#8217;s new, I see countless apps written entirely in foreign languages that are obviously designed for foreign audiences. If I have to scroll past 150 apps (this is also not an exaggeration, this happened to me recently) just to see something that actually pertains to my search, <em>I am going to give up looking</em>.</p>
<p>And that&#8217;s exactly what I have done. I no longer use my Windows Phone for apps. It is a sad, sad commentary because I <em>love</em> Metro, I love Windows Phone, and I absolutely adore the integration, the &#8220;hub&#8221; concept, and everything else that operating system does so well. But even for a &#8220;low app&#8221; consumer like me who only regularly uses 3 or 4 apps, the current state of the marketplace turned me off.</p>
<p>If the marketplace&#8217;s current state can turn me off, someone with an absolutely insane tolerance for shitty experiences, then I can&#8217;t even imagine how quickly standard consumers are being turned off by that same marketplace.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/B8UJ0fFn61Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/04/08/wp7-marketplace-disappoint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/04/08/wp7-marketplace-disappoint/</feedburner:origLink></item>
		<item>
		<title>Speaking at ScalaDays 2012 in London</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/r3yaUqWNuBE/</link>
		<comments>http://www.kotancode.com/2012/03/04/scaladays-2012-london/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 21:24:54 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mud]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[scaladays]]></category>
		<category><![CDATA[ScalaMUD]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=448</guid>
		<description><![CDATA[I am honored to have been invited to speak at ScalaDays 2012 in London this year. For a guy who for years was considered a &#8220;Microsoft guy&#8221; and then for a few years after that considered an &#8220;Apple guy&#8221;, this is great validation that I&#8217;m not a Microsoft guy or an Apple guy or even [...]]]></description>
			<content:encoded><![CDATA[<p>I am honored to have been invited to speak at ScalaDays 2012 in London this year. For a guy who for years was considered a &#8220;Microsoft guy&#8221; and then for a few years after that considered an &#8220;Apple guy&#8221;, this is great validation that I&#8217;m not a Microsoft guy or an Apple guy or even a Java guy &#8211; I am just a guy who spends most of his spare technical resources searching for things that exemplify Kotan &#8211; elegant simplicity, and that includes Scala.</p>
<p>The talk I will be giving is about my experience learning Scala and Akka 2.0 through my ScalaMUD project. Here is the description of my talk from the <a href="http://days2012.scala-lang.org/node/92" target="_blank">ScalaDays 2012 website</a>:</p>
<blockquote><p>When learning a new language, one of the first things I do is try and build a game in that language because games are a lot more fun than progressively complicated &#8220;hello world&#8221; samples. In this experience report, I discuss my learning process and experience, that of a developer who has spent the last 11 years in the Microsoft world. I am exploring Scala by building a simple network text adventure game that uses many language features and Akka 2.0 and encounters surprisingly real-world problems from many other industries, including finance. My focus in learning languages is on creating clean, simple, elegant code and, as always, justifying to others why this language is worth the learning curve.<br />
The code I&#8217;ve been writing as part of this experiment is open-sourced and available on GitHub.</p></blockquote>
<p>Looking forward to meeting a bunch of fellow Scala enthusiasts and being able to present on this compelling language.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/r3yaUqWNuBE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/03/04/scaladays-2012-london/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/03/04/scaladays-2012-london/</feedburner:origLink></item>
		<item>
		<title>Refactoring Imperative Code to List Comprehensions in Scala</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/GrTQ3PCAoSM/</link>
		<comments>http://www.kotancode.com/2012/02/20/refactoring-imperative-code-to-list-comprehensions-in-scala/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 17:32:48 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[idiomatic]]></category>
		<category><![CDATA[list comprehensions]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[ScalaMUD]]></category>
		<category><![CDATA[stackoverflow]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=446</guid>
		<description><![CDATA[In my other series of posts about ScalaMUD, I&#8217;ve been writing a lot of code &#8211; some of it good, some of it ugly as hell. The reason a lot of the code looks ugly is that, as a C#, Java, and Objective-C developer my gut instinct is to do things in an imperative style [...]]]></description>
			<content:encoded><![CDATA[<p>In my<a title="ScalaMUD – More Multi-Threaded Command Parsing via Akka" href="http://www.kotancode.com/2012/02/20/scalamud-commands/" target="_blank"> other series of posts about ScalaMUD</a>, I&#8217;ve been writing a lot of code &#8211; some of it good, some of it ugly as hell. The reason a lot of the code looks ugly is that, as a C#, Java, and Objective-C developer my gut instinct is to do things in an imperative style with if statements, for loops, and other such constructs that simply don&#8217;t look at home in a hybrid functional language like Scala.</p>
<p>Here&#8217;s the code I originally produced, which scans through a list of NLP-tagged words and retrieves the first verb. If the list is empty then we need to return an empty string and if the list has no verbs then we assume that the first (or only) word in the list is a verb. For example, if I type &#8220;who&#8221; or &#8220;uptime&#8221; into the game then these should be considered verbs for command dispatching, even though NLP says they&#8217;re not verbs per se.</p>
<p><strong>Ugly as Sin Code:</strong></p>
<pre class="brush: java; title: ; notranslate">
def firstVerb = {
if (words.size == 1)
words.head.value
else {
val outWords = words.filter( word =&gt; word.pos == Verb)
if (outWords == Set.empty)
words.head.value
else
outWords.head.value
}
}
</pre>
<p>And thanks to the geniuses over at <a href="http://stackoverflow.com/questions/9362952/idiomatic-scala-list-comprehension-first-item-that-matches" target="_blank">Stack Overflow</a>, they were able to guide me to this incredibly simple, elegant refactoring:</p>
<p><strong>Refactored Code:</strong></p>
<pre class="brush: java; title: ; notranslate">
words find(_.pos == Verb) orElse words.headOption map(_.value) getOrElse &quot;&quot;
</pre>
<p>My hope is that someday I will think in terms of the second, more clean and refactored version and not instinctively reach for my imperative crutches.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/GrTQ3PCAoSM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/02/20/refactoring-imperative-code-to-list-comprehensions-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/02/20/refactoring-imperative-code-to-list-comprehensions-in-scala/</feedburner:origLink></item>
		<item>
		<title>ScalaMUD – More Multi-Threaded Command Parsing via Akka</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/zb_QhTY5Mvc/</link>
		<comments>http://www.kotancode.com/2012/02/20/scalamud-commands/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 17:17:55 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[grammar]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mud]]></category>
		<category><![CDATA[nlp]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[ScalaMUD]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=440</guid>
		<description><![CDATA[In the last blog post on ScalaMUD, I showed how I was able to take advantage of a third party NLP library so that I could tag player input with the appropriate parts of speech like Verb, Noun, Adjective, etc. My goal here is to eventually use this information to match words in player input [...]]]></description>
			<content:encoded><![CDATA[<p>In the last blog post on ScalaMUD, I showed how I was able to take advantage of a third party NLP library so that I could tag player input with the appropriate parts of speech like Verb, Noun, Adjective, etc. My goal here is to eventually use this information to match words in player input sentences with physical objects in the game so that when someone types &#8216;kill the blue dragon&#8217;, I will be able to find the <strong>ActorRef</strong> for the blue dragon, and find a verb handler for <strong>kill</strong>, and then initiate combat.</p>
<p>In this blog post, I&#8217;ve come one step closer to that goal by enabling the dispatching of commands. As players enter input, that input is sent off to a <strong>Commander</strong> actor which tags up the input with parts of speech. Once the Comander is finished with it, it wraps the tagged words in an <strong>EnrichedCommand</strong> message and sends that to the player, which now has the trait <strong>Commandable</strong>, allowing the player to respond to enriched commands. This has an added benefit of allowing NPCs to be scripted in the future by allowing them to pretend to &#8216;type&#8217; things like &#8220;kill player&#8221;, etc.</p>
<p>Eventually rooms will add commands to players when they enter the room to allow them to type the name of the exit (e.g. north, south, up, window) as a verb. Mortals and Wizards alike each have a unique set of commands they need to be able to type and these commands are grouped into &#8220;command libraries&#8221; (my old alma mater MUD, <em>Genesis, an LPMUD</em>, called them <em>command souls</em>). For example, here&#8217;s the first version of the Mortal command lib:</p>
<pre class="brush: java; title: ; notranslate">
package com.kotancode.scalamud.core.cmd

import com.kotancode.scalamud.core.ServerStats
import com.kotancode.scalamud.core.TextMessage
import com.kotancode.scalamud.Game
import com.kotancode.scalamud.core.Implicits._
import akka.actor._
import akka.routing._

abstract class CommandLibMessage
case class AttachCommandLib extends CommandLibMessage

class MortalCommandLib extends Actor {
	def receive = handleMortalCommands

	def handleMortalCommands: Receive = {
		case cmd:EnrichedCommand if cmd.firstVerb == &quot;who&quot; =&gt; {
			handleWho(cmd.issuer)
		}
		case AttachCommandLib =&gt; {
			attachToSender(sender)
		}
	}

	def handleWho(issuer: ActorRef) = {
		println(&quot;player &quot;+ issuer.name + &quot; typed who.&quot;)
		var stringOut = &quot;Players logged in:\n&quot;
		for (p: ActorRef &lt;- Game.server.inventory) {
			stringOut += p.name + &quot;\n&quot;
		}
		issuer ! TextMessage(stringOut)
	}

	def attachToSender(sender:ActorRef) = {
		sender ! AddCommand(Set(&quot;who&quot;), self)
	}

}
</pre>
<p>Those of you who program in iOS or Cocoa might recognize some of the &#8220;Delegate Pattern&#8221; here&#8230; when the command lib attaches itself to something capable of issuing commands (anything that carries the <strong>Commandable</strong> trait, like a player or NPC), it just passes the implicit <em>sender</em> <strong>ActorRef</strong> so that actor can now dispatch commands to that lib.</p>
<p>Here&#8217;s the code in the <strong>Commander</strong> object that sends enriched commands to the actor that &#8220;typed&#8221; (virtually or for real) the command:</p>
<pre class="brush: java; title: ; notranslate">
package com.kotancode.scalamud.core.cmd

import akka.actor._
import akka.routing._

import com.kotancode.scalamud.core.lang.EnrichedWord
import java.util.ArrayList
import edu.stanford.nlp.ling.Sentence
import edu.stanford.nlp.ling.TaggedWord
import edu.stanford.nlp.ling.HasWord
import edu.stanford.nlp.tagger.maxent.MaxentTagger
import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import com.kotancode.scalamud.core.cmd._

class Commander extends Actor {
	def receive = {
		case s:String =&gt; {
			val words = s.split(&quot; &quot;);
			val wordList = new java.util.ArrayList[String]();
			for (elem &lt;- words) wordList.add(elem)
		    val sentence = Sentence.toWordList(wordList);
		    val taggedSentence = Commander.tagger.tagSentence(sentence).asScala.toList

			var enrichedWords = new ListBuffer[EnrichedWord]
		    for (tw : TaggedWord &lt;- taggedSentence) {
				val ew = EnrichedWord(tw)
				println(ew)
				enrichedWords += ew
			}

			sender ! HandleCommand(EnrichedCommand(enrichedWords, sender))
	}
}
}

object Commander {
	val tagger = new MaxentTagger(&quot;models/english-bidirectional-distsim.tagger&quot;)
}
</pre>
<p>The important bit is that the player gets the <strong>HandleCommand</strong> message, which then goes through a dispatch process in the <strong>Commandable</strong> trait, and eventually registered verb handlers (like those registered via attached command libraries) get invoked via messages. Here&#8217;s the Commandable trait:</p>
<pre class="brush: java; title: ; notranslate">
package com.kotancode.scalamud.core.cmd

import akka.actor._
import akka.routing._
import scala.collection.mutable.HashMap
import scala.collection.mutable.HashSet

sealed abstract class CommandMessage
case class AddCommand(verbs:Set[String], handlerTarget:ActorRef) extends CommandMessage
case class Removecommand(verb:String) extends CommandMessage
case class HandleCommand(command:EnrichedCommand) extends CommandMessage

trait Commandable {

	private val verbHandlers: HashMap[Set[String], ActorRef] = new HashMap[Set[String], ActorRef]

	def handleCommandMessages:akka.actor.Actor.Receive = {
		case AddCommand(verbs, handlerTarget) =&gt; {
			verbHandlers.put(verbs, handlerTarget)
		}

		case HandleCommand(cmd) =&gt; {
			dispatch(cmd)
		}
	}

	def dispatch(cmd:EnrichedCommand) = {
		println(&quot;handled a command &quot;+ cmd +&quot;.&quot;)
		println(&quot;command's first verb: &quot; + cmd.firstVerb)
		val targetHandlers = verbHandlers.filterKeys(key =&gt; key.contains(cmd.firstVerb))
		targetHandlers foreach {case (key, value) =&gt; value ! cmd}
	}
}
</pre>
<p>The dispatching actually happens in the <strong>targetHandlers foreach &#8230;</strong> line where the command is sent to every command handler that declared interest in that verb. In our case, we have a mortal command verb <strong>who</strong> that displays the list of connected users and the wizard command verb <strong>uptime</strong> that displays the length of time the server app has been running.</p>
<p>The following is sample session output from telnetting to the game:</p>
<pre>Welcome to ScalaMUD 1.0

Login: Kevin
Welcome to ScalaMUD, Kevin
who
Kevin: who
Players logged in:
Bob
Kevin
Kevin
uptime
Kevin: uptime
Server has been up for 6 mins 39 secs.</pre>
<p>Now that I can log in with multiple players, see who is online, and dispatch commands to handlers as well as differentiate between mortal and wizard abilities, this game is finally starting to feel like the beginnings of a real MUD.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/zb_QhTY5Mvc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/02/20/scalamud-commands/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/02/20/scalamud-commands/</feedburner:origLink></item>
		<item>
		<title>iOS and Mac OS X Convergence Continues with Mountain Lion</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/jIfbUVrGaCU/</link>
		<comments>http://www.kotancode.com/2012/02/17/ios-mountain-lion-convergence-mac/#comments</comments>
		<pubDate>Fri, 17 Feb 2012 12:57:08 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[gamekit]]></category>
		<category><![CDATA[iCloud]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[lion]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mountain lion]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=435</guid>
		<description><![CDATA[Just the other day, Apple announced the major features coming up in their new operating system release, Mac OS X Mountain Lion. Before going into details on Mountain Lion, it&#8217;s worth remembering here that less than a year ago, Apple released Lion, which included a host of features that it borrowed from the iPad, including [...]]]></description>
			<content:encoded><![CDATA[<p>Just the other day, Apple announced the major features coming up in their new operating system release, <em>Mac OS X Mountain Lion</em>. Before going into details on Mountain Lion, it&#8217;s worth remembering here that less than a year ago, Apple released <em>Lion</em>, which included a host of features that it borrowed from the iPad, including <strong>Popovers</strong>, the option to use &#8220;natural&#8221; scrolling the way you do on the iPad, iOS-style invisible scroll bars, <strong>iCloud</strong> support, full-screen applications, and even some great new enhancements for developers using Objective-C.</p>
<p>So what is Apple planning to do with Mac OS X <em>Mountain Lion</em>? According to the information I can find publicly, here are some of the highlights:</p>
<ul>
<li><strong>Game Center</strong> &#8211; Apple&#8217;s game service that provides everything from matchmaking to leader boards, achievements, and even in-game multiplayer voice chat is coming to the Mac. This also means that developers writing games for the Mac will be able to participate in all that amazing goodness that iOS developers have been enjoying since iOS 4.1.</li>
<li><strong>iCloud</strong> &#8211; I don&#8217;t really know all the details here only that iCloud is even more integrated into the OS now and I&#8217;m assuming the developer experience around iCloud is getting better. I&#8217;m actually <em>hoping</em> that experience gets better because writing iCloud code on Lion is a pain in the ass.</li>
<li><strong>Sharing</strong> &#8211; More iOS goodness comes to the Mac with sharing &#8220;sheets&#8221;. Every application on Mountain Lion now gets a little sharing icon that uses sharing services, allowing you to share whatever you&#8217;re looking at in the application with your buddies via e-mail or whatever other services are registered on the Mac, including <strong>Twitter</strong>. I would imagine that it wouldn&#8217;t take much work, assuming the sharing APIs are good, to add other sharing targets like Facebook, Dropbox, LinkedIn, etc to your Mac &#8211; turning it into just as much of a social hub as your phone.</li>
<li><strong>Twitter</strong> - Twitter is a first-class citizen on the Mac just like it became first-class on iOS 5<em>. </em>This means that Mountain Lion users can tweet from pretty much anywhere and using the &#8220;sharing&#8221; services, they can share pretty much whatever they&#8217;re working on in any application (that supports it) via Twitter. Not only is twitter a first-class citizen, but applications can send Tweets on behalf of their users <em>without any extra work</em>. Twitter appears to be accessible in API form to developers without them having to figure out which of the 10 open-source Objective-C twitter libraries they want to shoehorn into their app. This is fantastic news for developers and I hope we get similar integration possibilities with Facebook, Dropbox, etc via Sharing &#8220;sheet&#8221; APIs.</li>
<li><strong>Notifications</strong> - In what looks to be <em>very</em> similar to the notification center on iOS 5, Apple now has a unified display of messages that are pertinent to the user, including IMs, iMessages (also now a fully integrated part of Mountain Lion), and application-level notifications. In classic Apple style, they are aiming to push popular 3rd parties out of this market with their 1st party offering. Applications like <strong>Growl</strong> may find it hard to compete with the unified interface, but time will tell how the 1st-vs-3rd party battles are settled.</li>
<li><strong>Gatekeeper</strong> - Again I don&#8217;t know all the details, but this feature aims to make your application more secure and, more importantly, provides developers with a secure way of distributing <em>verified, signed</em> applications <em>outside the app store</em>. For most developers, the App Store is absolutely the best way to go. But for some applications, it makes no sense to put in the app store and many people don&#8217;t need Apple&#8217;s distribution channel and so aren&#8217;t comfortable sharing 30% of their profit with the company. Gatekeeper and Developer IDs and signing give developers a way of distributing applications that users can feel comfortable downloading, installing, and using.</li>
<li><strong>64-Bit</strong> &#8211; You simply cannot run Mountain Lion on a 32-bit machine. The kernel is 64-bit and an entire host of 32-bit &#8220;wrappers&#8221; are missing in Mountain Lion, further drawing a line in the sand against 32-bit apps. If only some other software giant whose name starts with <strong>M</strong> that makes an operating system would also draw a similar line. In words from one of my favorite movies of all time (Zombieland), &#8220;It&#8217;s time to nut up or shut up.&#8221;</li>
<li><strong>SSO</strong> &#8211; In order to support seamless sharing and such, there appears to be some kind of single sign-on facility in OS X much like Windows has been allowing you to link your &#8220;LiveID&#8221; to your user account for quite some time &#8230; only with the Twitter integration I would imagine Apple&#8217;s SSO allows for more than just Apple ID storage.</li>
</ul>
<p>All in all Mountain Lion is looking pretty promising. Again, there&#8217;s absolutely nothing groundbreaking in here, but starting with <strong>Lion</strong> and continuing with <strong>Mountain Lion</strong>, it appears that Mac developers are finally getting access to the same amount of lovin&#8217; that the iOS developers have been enjoying since iOS 4.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/jIfbUVrGaCU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/02/17/ios-mountain-lion-convergence-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/02/17/ios-mountain-lion-convergence-mac/</feedburner:origLink></item>
		<item>
		<title>ScalaMUD – Consuming Java from Scala and NLP Tagging</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/XiNLaCYBZi8/</link>
		<comments>http://www.kotancode.com/2012/02/15/scalamud-scala-java-nlp/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 12:40:38 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[mud]]></category>
		<category><![CDATA[nlp]]></category>
		<category><![CDATA[parsers]]></category>
		<category><![CDATA[scala]]></category>
		<category><![CDATA[ScalaMUD]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=428</guid>
		<description><![CDATA[Last night I upgraded ScalaMUD&#8217;s POM file to point to the recently-available Akka 2.0-RC1. I was previously using M3 an was happy to note that all of my Akka 2.0 code continued working just fine without change from M3 to RC1. If the Akka RC is like most other RCs then there should be no [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I upgraded ScalaMUD&#8217;s POM file to point to the recently-available Akka 2.0-RC1. I was previously using M3 an was happy to note that all of my Akka 2.0 code continued working just fine without change from M3 to RC1. If the Akka RC is like most other RCs then there should be no further API changes, only fixes and tightening.</p>
<p>While I had the MUD code open I decided to start working on the problem of accepting player input. Sure, I have a socket reader that accepts text from players but what does one do with this text?</p>
<p>In the old days, I would&#8217;ve tokenized the string. By tokenized here I mean just splitting it blindly on spaces. Then I would have considered the first word in the array to be the verb and then dispatched the remaining parameters to some function in the MUD code that knows how to respond to that verb. For example, if I typed <em>kill dragon</em> then I would&#8217;ve tagged <em>kill</em> as the <strong>verb</strong> and <em>dragon</em> as the &#8220;rest&#8221;. This would have eventually found its way to some <em>kill()</em> method on a player that takes an array of strings as parameters.</p>
<p>Thankfully this isn&#8217;t the old days.</p>
<p>Instead what I did was declare a Maven dependency on the <a href="http://nlp.stanford.edu/software/tagger.shtml" target="_blank">Stanford NLP (Natural Language Processing) project</a>. To be specific, I wanted to use the Stanford non-linear Parts of Speech tagger. Why should I deal with parsing strings in a dumb way when someone else has spent <em>years</em> creating a powerful, well-trained NLP engine that can tag every sentence my player types with parts of speech?</p>
<p>This way, instead of relying on forcing players to type in pidgin dialects (e.g. <em>kill dragon</em> or <em>cast spell </em>or <em>move north</em>) I can let them type complete English sentences (if they want). I will then tag those sentences with the appropriate parts of speech and infer what they wanted to do from that.</p>
<p>I want this parsing to take place in the background. Once the player&#8217;s sentence has been <em>enriched</em> with parts of speech, I want to send the enriched sentence back to whatever typed it so that the command can be dispatched. To do this, I created a new Actor called <strong>Commander</strong>:</p>
<pre class="brush: scala; title: ; notranslate">
package com.kotancode.scalamud.core

import akka.actor._
import akka.routing._

import com.kotancode.scalamud.core.lang.EnrichedWord
import java.util.ArrayList
import edu.stanford.nlp.ling.Sentence
import edu.stanford.nlp.ling.TaggedWord
import edu.stanford.nlp.ling.HasWord
import edu.stanford.nlp.tagger.maxent.MaxentTagger
import scala.collection.JavaConverters._

class Commander extends Actor {
	def receive = {
		case s:String =&gt; {
			val words = s.split(&quot; &quot;);
			val wordList = new java.util.ArrayList[String]();
			for (elem &lt;- words) wordList.add(elem)
		    val sentence = Sentence.toWordList(wordList);
		    val taggedSentence = Commander.tagger.tagSentence(sentence).asScala.toList

			var enrichedWords = new ArrayList[EnrichedWord]
		    for (tw : TaggedWord &lt;- taggedSentence) {
		//		println(tw.value + &quot;/&quot; + tw.tag)
				val ew = EnrichedWord(tw)
				println(ew)
				enrichedWords.add(ew)
			}
	}
  }
}

object Commander {
	val tagger = new MaxentTagger(&quot;models/english-bidirectional-distsim.tagger&quot;)
}
</pre>
<p>At this point I&#8217;m just building the array of enriched words and I&#8217;m not actually sending the command back to the player (I&#8217;ll do that tonight or tomorrow, time permitting .. as always, you can check out the <a href="https://github.com/kotancode/ScalaMud" target="_blank">GitHub repo</a> for the latest changes to the MUD). One of the interesting bits here is how I&#8217;m using a Java library from Scala. This is usually a pretty painless task but sometimes there are issues. In this case, the Stanford NLP library class <strong>Sentence</strong> has a bunch of overloads for the <strong>toWordList</strong> method. Java knows how to pick which overload but Scala doesn&#8217;t if I just use type inference and default Scala types. To get it to pick the right <strong>toWordList</strong> method I had to manually construct an <strong>ArrayList[String]</strong> because passing an <strong>Array[String]</strong> doesn&#8217;t let Scala know which overload to pick. It&#8217;s a little annoying but if I can keep the Scala-&gt;Java bridge points like this minimal then it&#8217;s not bad.</p>
<p>The flip side of this is that I&#8217;m getting back a regular Java array list in response to <strong>toWordList</strong>, which doesn&#8217;t support pretty Scala-native iteration because it doesn&#8217;t contain a <strong>foreach</strong> method, which is the underpinning that supports all the syntactic sugar around iteration. To deal with that, I imported the Java converters package implicits so that I could get the <strong>asScala</strong> function, which lets me call <strong>toList</strong>, which gives me a nice Scala list that I can use for easy iteration.</p>
<p>Here&#8217;s some sample output when I connect to the MUD and enter a sample sentence, which is then tokenized and tagged with parts of speech:</p>
<pre>[EnrichedWord: word=attack, tag=VB, pos=Verb]
[EnrichedWord: word=the, tag=DT, pos=DontCare]
[EnrichedWord: word=green, tag=JJ, pos=Adjective]
[EnrichedWord: word=dragon, tag=NN, pos=Noun]
[EnrichedWord: word=with, tag=IN, pos=DontCare]
[EnrichedWord: word=the, tag=DT, pos=DontCare]
[EnrichedWord: word=yellow, tag=JJ, pos=Adjective]
[EnrichedWord: word=sword, tag=NN, pos=Noun]</pre>
<p>The real goal here is that I will be taking the tagged nouns in the sentence and scanning through the player&#8217;s inventory and the environment in which the player stands for objects which have names that match the nouns and then using adjectives to disambiguate them if collisions occur. That way, when I type &#8220;kick blue bottle&#8221; I will be able to scan the surroundings for objects called &#8220;bottle&#8221; and if I find more than one, I&#8217;ll only gather up the ones that are blue.</p>
<p>In case you&#8217;re wondering what the <strong>EnrichedWord</strong> class looks like, which has some helper code that identifies only the parts of speech I care about, here it is:</p>
<pre class="brush: scala; title: ; notranslate">
package com.kotancode.scalamud.core.lang

import edu.stanford.nlp.ling.TaggedWord

case class PartOfSpeech
case object Noun extends PartOfSpeech
case object Verb extends PartOfSpeech
case object Adjective extends PartOfSpeech
case object DontCare extends PartOfSpeech

class EnrichedWord(value:String, tag:String, val pos:PartOfSpeech) extends TaggedWord(value, tag) {

	override def toString = &quot;[EnrichedWord: word=&quot; + value +&quot;, tag=&quot; + tag + &quot;, pos=&quot; + pos + &quot;]&quot;
}

object EnrichedWord {
	def apply(hw: TaggedWord) = {
		val ew = new EnrichedWord(hw.value, hw.tag, rootTypeOf(hw.tag))
		ew
	}

	def rootTypeOf(s:String) = {
		s match {
			case &quot;VB&quot; | &quot;VBD&quot; | &quot;VBG&quot; | &quot;VBN&quot; | &quot;VBP&quot; | &quot;VBZ&quot; =&gt; Verb
			case  &quot;NN&quot; | &quot;NNS&quot; | &quot;NNP&quot; | &quot;NNPS&quot; =&gt; Noun
			case &quot;JJ&quot; | &quot;JJR&quot; | &quot;JJS&quot; =&gt; Adjective
			case _ =&gt; DontCare
		}
	}
}
</pre>
<p>Note that the <strong>EnrichedWord</strong> Scala class inherits from the Stanford <strong>TaggedWord</strong> Java class.</p>
<p>I really, <em>really</em>, love the syntax of the string pattern matcher I use to obtain the POS root (adjective, verb, noun, don&#8217;t care) from the Penn Treebank Tags that are used by the Stanford NLP POS tagger.</p>
<p>The takeaway I got from this exercise is further reinforcement of my rule to never re-invent the wheel because there are wheel experts out there who have dedicated their lives and careers to building wheels more awesome than I could ever hope to build. Hence I declare a Maven dependency on the NLP library and in a single night, I&#8217;ve got a MUD that can <em>intelligently</em> POS-tag player sentences which I can then use to identify potential targets of player commands. In addition, I don&#8217;t have to create a pidgin dialect for interacting with the MUD. English works and the MUD should be able to deal with &#8220;<em>I kill the dragon with the blue sword because I am the shizzle&#8221;</em> with the same ease as &#8220;<em>kill dragon with sword&#8221;</em>.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/XiNLaCYBZi8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/02/15/scalamud-scala-java-nlp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/02/15/scalamud-scala-java-nlp/</feedburner:origLink></item>
		<item>
		<title>Regular Expressions and Parsing in Scala</title>
		<link>http://feedproxy.google.com/~r/KotanCode/~3/O_3BFlQIuZg/</link>
		<comments>http://www.kotancode.com/2012/02/10/scala-regex/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 15:11:56 +0000</pubDate>
		<dc:creator>Kevin Hoffman</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[pattern matching]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.kotancode.com/?p=426</guid>
		<description><![CDATA[Last night I was messing around with some code and I wanted to rip a couple of pieces of information out of a free-from string. This is typically where people reach for regular expressions. Not me, though. No, my experience with regular expression support and parsing in other languages like C# and Java has left [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I was messing around with some code and I wanted to rip a couple of pieces of information out of a free-from string. This is typically where people reach for regular expressions. Not me, though.</p>
<p>No, my experience with regular expression support and parsing in other languages like C# and Java has left a terrible taste in my mouth. I hate it. When I want to parse stuff, I want to use pattern matching and rich syntax like I can get from languages like F# or Scala.</p>
<p>This made me wonder if Scala didn&#8217;t make regex parsing easier. Turns out, it makes it brain-dead simple with the clever use of the concept of an <em>unapply</em>. The same capability that makes Scala so good at constructing, zipping, and traversing list structures also makes it just as good at cracking apart structures into their component parts.</p>
<p>As a sample, I created a simple string in Scala and then put the &#8220;.r&#8221; postfix on it, turning it into a regular expression:</p>
<pre class="brush: java; title: ; notranslate">
scala&gt; val zombieLog = &quot;&quot;&quot;(d\+) zombies spotted in (\w+).&quot;&quot;&quot;.r
</pre>
<p>This created a regular expression with two groups. If you&#8217;re used to doing this kind of thing in Java or C# then you&#8217;re probably thinking that you now need to write 10 lines of code to set up the regex compiler, call some method to feed the string into the regex compiler, then access groups by numeric, 0-based indexes.</p>
<p>Fortunately, this is Scala and more often than not, things suck less in Scalaville.</p>
<p>First, I&#8217;ll create an input string that I want to crack apart with my regex:</p>
<pre class="brush: java; title: ; notranslate">
scala&gt; val inputText = &quot;200 zombies spotted in Mexico.&quot;
</pre>
<p>Now I&#8217;ll use unapply and syntactic sugar and Scala goodness to pull out the right values:</p>
<pre class="brush: java; title: ; notranslate">
scala&gt; val zombieLog(zombieCount, location) = inputText // UNAPPLY magic
zombieCount: String = 200
location: String = Mexico
</pre>
<p>And that&#8217;s it, Bob&#8217;s your uncle. With the plethora of online regular expression builders available, it should only take you a few minutes to create the regex you need and slap it into your Scala code.</p>
<img src="http://feeds.feedburner.com/~r/KotanCode/~4/O_3BFlQIuZg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.kotancode.com/2012/02/10/scala-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.kotancode.com/2012/02/10/scala-regex/</feedburner:origLink></item>
	</channel>
</rss>

