<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nathanpowell.org &#187; programming</title>
	<atom:link href="http://nathanpowell.org/blog/archives/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://nathanpowell.org/blog</link>
	<description>Bad running advice, boring family stuff, and technology few find interesting</description>
	<lastBuildDate>Thu, 26 Jan 2012 01:25:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Reading/Printing a file</title>
		<link>http://nathanpowell.org/blog/archives/803</link>
		<comments>http://nathanpowell.org/blog/archives/803#comments</comments>
		<pubDate>Sat, 08 Aug 2009 13:41:47 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/?p=803</guid>
		<description><![CDATA[A friend of mine at work, also bitten by the functional bug, has been hacking on some Scala. He showed me some code yesterday and I thought it might be fun to convert it. I am not going to post &#8230; <a href="http://nathanpowell.org/blog/archives/803">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A friend of mine at work, also bitten by the functional bug, has been hacking on some <a href="http://www.scala-lang.org/">Scala</a>.</p>
<p>He showed me some code yesterday and I thought it might be fun to convert it.  I am not going to post his code, as that would be presumptuous, but I have mine to show.</p>
<p>The goal is to read up a file, and then print that file prepending each line with the number of characters in that line, as well as padding the beginning so that everything lines up.</p>
<p>The usual caveats about my newbness.<br />
<code><br />
; Get the name of the document from the cli<br />
(def arg (first *command-line-args*))</p>
<p>; Convert the doc to a vector<br />
(def lines (vec (.split (slurp arg) "\n")))</p>
<p>; Takes a string, and finds out how long<br />
; the length, of the length is<br />
(defn length-of-length[s]<br />
  (.length (str (.length s))))</p>
<p>; Returns the longest element from a collection<br />
(defn get-longest-line[lines]<br />
    (reduce (fn [x y]<br />
              (if (>(.length x)(.length y))<br />
                x y))lines))</p>
<p>; Repeat s string t times.<br />
(defn pad[s t]<br />
  (take t (repeat s)))</p>
<p>; Find the maximum width of a line from the doc<br />
(def max-width (length-of-length (get-longest-line lines)))</p>
<p>; Take the vector of the file, iterate over the lines and<br />
; print each one prepended with the number of characters that<br />
; appear in that line<br />
(doseq [line lines]<br />
  (let [num-spaces (- max-width (length-of-length line))]<br />
    (println (apply str " " (pad " " num-spaces)) (.length line) " | " line)))<br />
</code> </p>
<p>Tip of the hat to &#8216;Chousuke&#8217; in #clojure for the clue on &#8216;apply&#8217;.</p>
<h2>Example</h2>
<p><code><br />
$ java -cp /opt/clojure/clojure.jar clojure.main scala.clj test.txt<br />
  50  |  This is a text file with lines that vary in length<br />
   0  |<br />
  13  |  ^^ Blank line<br />
  34  |  Some other text that is mid length<br />
   9  |  Oh hai!!!<br />
</code></p>
<p>Thanks for playing!</p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/803/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dwarf Name Generator</title>
		<link>http://nathanpowell.org/blog/archives/794</link>
		<comments>http://nathanpowell.org/blog/archives/794#comments</comments>
		<pubDate>Sun, 02 Aug 2009 23:07:49 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/?p=794</guid>
		<description><![CDATA[The local ruby group sends out the Ruby Quiz (with some regularity). This week it was to create a Dwarf Name Generator. I thought of a fairly simple solution in Clojure pretty quickly, but then it took me a while &#8230; <a href="http://nathanpowell.org/blog/archives/794">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://ruby.meetup.com/102/">local ruby group</a> sends out the <a href="http://rubyquiz.strd6.com/">Ruby Quiz</a> (with some regularity).  This week it was to create a Dwarf Name Generator.</p>
<p>I thought of a fairly simple solution in Clojure pretty quickly, but then it took me a while to implement it.  Anyway, here is what I came up with.</p>
<p><code><br />
; Create a couple of Maps with keys being Integers<br />
(def firstnames {1 "Danby" 2 "Tamreil" 3 "Vash" 4 "Gobu"})<br />
(def lastnames {1 "Dongledorp" 2 "Floopydoo" 3 "Goran" 4 "Zypher"})</p>
<p>; Return a random Integer with a value > 0 but < = the upper bounds<br />
(defn randomize [upper]<br />
		(let [randy (. Math round(* (. Math random) upper))]<br />
			(if (= randy 0)<br />
				(int (+ 1 randy))<br />
				(int randy)))) </p>
<p>; Since Maps, can be used as Functions that take a key as an argument<br />
; pass those to println<br />
(println (firstnames (randomize (.size firstnames))) (lastnames (randomize (.size lastnames))))<br />
</code></p>
<p>The reason it took me so long is that initially I wasn't casting 'randy' to a Java Integer, and using it as a argument to a Map (which causes the Map to act as a function) was causing the Map to not find it while looking it up.  After a bit of head scratching, I asked in #clojure.</p>
<p>Obviously, if I were a linguist, I would have created a lot of maps with just parts of names in them (I am sure that is called something), and then randomized that further, but I am not, and since the underlying mechanics would be more or less the same, I left it as is.</p>
<p>Happy hacking!</code></p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/794/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure/Java Interop</title>
		<link>http://nathanpowell.org/blog/archives/781</link>
		<comments>http://nathanpowell.org/blog/archives/781#comments</comments>
		<pubDate>Sat, 25 Jul 2009 11:15:29 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/?p=781</guid>
		<description><![CDATA[Hello again. I picked up the book Programming Clojure and have made my way through the first two chapters which were great. Last night I started the 3rd chapter, on Java Interoperability. I was hoping to toss together a single &#8230; <a href="http://nathanpowell.org/blog/archives/781">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hello again.</p>
<p>I picked up the book <a href="http://pragprog.com/titles/shcloj/programming-clojure">Programming Clojure</a> and have made my way through the first two chapters which were great.</p>
<p>Last night I started the 3rd chapter, on Java Interoperability.</p>
<p>I was hoping to toss together a single Java class that I could then try to rewrite in Clojure to help me learn.  Following is what I came up with.  (Note, I am not a Java ninja by any stretch so beware).</p>
<p>This is not intended as any sort of comparison of the languages per se.  It&#8217;s simply, &#8220;Given X, I had to do Y to get the same functionality&#8221;.</p>
<p>My idea was to find a Java library for parsing rss and then parse out the rss for this site, prining the Title of the Post, as well as the link (this is the <a href="http://java.sun.com/developer/technicalArticles/javaserverpages/rss_utilities/rss_utils_1.1.zip">lieberry</a> I used).  </p>
<p>First the Java:</p>
<p><script src="http://gist.github.com/265130.js?file=gistfile1.java"></script></p>
<p>And next the Clojure:</p>
<p><script src="http://gist.github.com/265131.js?file=gistfile1.clj"></script></p>
<p>While I am not sure the Clojure code is as succinct as it could be since I am still pretty new, I do like the looks of it.   I agree with the notion that it&#8217;s an expressive language, and that one can do quite a bit with a minimum of fuss.   I think that practically the syntax is idiomatic, I am not entirely sold on the dot form, and dot dot macro. I don&#8217;t think they are as visually appealing as would be a symbol in this context.  But they do make sense.</p>
<p>Happy learning!</p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/781/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>n00b</title>
		<link>http://nathanpowell.org/blog/archives/773</link>
		<comments>http://nathanpowell.org/blog/archives/773#comments</comments>
		<pubDate>Fri, 17 Jul 2009 12:12:43 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/archives/773</guid>
		<description><![CDATA[I am a total Clojure n00b. But I am having a lot of fun learning it. It&#8217;s not like anything else I have learned to date. I hadn&#8217;t really done much with it, other than watch presentations, mess around in &#8230; <a href="http://nathanpowell.org/blog/archives/773">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I am a total <a href="http://clojure.org">Clojure</a> n00b.  But I am having a lot of fun learning it.  It&#8217;s not like anything else I have learned to date.</p>
<p>I hadn&#8217;t really done much with it, other than watch presentations, mess around in the REPL, and with the help of <a href="http://hexmode.com">Hexmode</a>, get Clojure working with Slime in Emacs.</p>
<p>Yesterday and this morning during stolen minutes of solitude, I have been trying to get it to talk to a MySQL DB.  Mostly, because I&#8217;d love to get to a point where I can, you know, actually do something with it :)</p>
<p>So this, for my fellow newbs that stumble here via google, is what I did.</p>
<p>For the purposes of demonstration, I created a 1 table db and gave it some lame data.<br />
<code><br />
$ mysqladmin -uroot create hack<br />
$ echo "CREATE TABLE hack(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);" | mysql -u root hack<br />
$ for i in $(seq 4);do echo "use hack; INSERT INTO hack VALUES()" | mysql -u root hack;done<br />
</code></p>
<p>I can assure you the following code, is likely not best practices, but I have to start somewhere.  So I whipped this together by looking at the test.clj for the clojure.contrib.sql library on <a href="http://code.google.com/p/clojure-contrib/source/browse/trunk/src/clojure/contrib/sql/test.clj">github</a> (Open Source rocks like that). And a lot of googling.  The comments are there for your benefit.<br />
<code><br />
(comment Import the Clojure Contrib SQL Library)<br />
(require '[clojure.contrib.sql :as ccsql])</p>
<p>(comment Set up the connection string)<br />
(def db { :classname "com.mysql.jdbc.Driver"<br />
          :subprotocol "mysql"<br />
          :subname "//localhost/hack"<br />
          :user "root"<br />
          :create true })</p>
<p>(comment Define a function that does a SELECT * on whatever table you pass in)<br />
(defn get-star<br />
  "Print all the rows in the table"<br />
  [table]<br />
  (ccsql/with-connection db<br />
    (ccsql/with-query-results res<br />
      [(str "SELECT * FROM " table)]<br />
      (doseq [rec res]<br />
        (println rec)))))</p>
<p>(comment Execute that function)<br />
(get-star "hack")</code></p>
<p>Then to run that code, you have to add the <a href="http://dev.mysql.com/downloads/connector/j/5.1.html">mysql</a> Driver jar to your classpath. (Obviously, manually doing all this won&#8217;t scale, and after a jar or two, you&#8217;ll want to build an executable jar of your own and adopt a build process, but for the purposes of demonstration, this is fine.  In fact, I have my clojure script in /bin, so I don&#8217;t actually call this this way&#8230;but whatever) </p>
<p><code><br />
$ java -cp "/opt/clojure/clojure.jar:/opt/clojure/clojure-contrib.jar:/opt/jars/mysql-connector-java-5.1.8-bin.jar" clojure.main test.clj<br />
{:id 1}<br />
{:id 2}<br />
{:id 3}<br />
{:id 4}<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/773/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ruby Hoedown</title>
		<link>http://nathanpowell.org/blog/archives/771</link>
		<comments>http://nathanpowell.org/blog/archives/771#comments</comments>
		<pubDate>Fri, 10 Jul 2009 10:48:52 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/archives/771</guid>
		<description><![CDATA[Signed up yesterday for the Ruby Hoedown, in Nashville. Some friends and I (and a dude named Larry) are going to be heading down there in late August. Should be a blast.]]></description>
			<content:encoded><![CDATA[<p>Signed up yesterday for the Ruby Hoedown, in Nashville.</p>
<p>Some friends and I (and a dude named Larry) are going to be heading down there in late August.  Should be a blast.</p>
<p><center><br />
<img src="http://nathanpowell.org/images/attendee.png"/><br />
</center></p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/771/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I like postgres.</title>
		<link>http://nathanpowell.org/blog/archives/764</link>
		<comments>http://nathanpowell.org/blog/archives/764#comments</comments>
		<pubDate>Wed, 13 May 2009 21:15:12 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/archives/764</guid>
		<description><![CDATA[I really do. So after spending a pretty frustrating day trying to get a development environment in windows set up, I finally got to the part where I was going to put postgres on the machine. Easy I thought. Those &#8230; <a href="http://nathanpowell.org/blog/archives/764">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I really do.  So after spending a pretty frustrating day trying to get a development environment in windows set up, I finally got to the part where I was going to put postgres on the machine.</p>
<p>Easy I thought.  Those guys do good work.  And they are cool enough to autogen you a password.</p>
<p><img src="http://nathanpowell.org/images/goodluckasshole.jpg" /></p>
<p>Um. wtf.</p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/764/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Javascript logging</title>
		<link>http://nathanpowell.org/blog/archives/736</link>
		<comments>http://nathanpowell.org/blog/archives/736#comments</comments>
		<pubDate>Thu, 16 Oct 2008 11:23:06 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/archives/736</guid>
		<description><![CDATA[In preparing for my talk this Sunday at CPOSC I have been playing around obviously with various commands, wrapper methods and utility function for jQuery. This morning I was going through my feeds when I stumbled upon, Blackbird. It&#8217;s an &#8230; <a href="http://nathanpowell.org/blog/archives/736">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In preparing for my talk this Sunday at <a href="http://cposc.org">CPOSC</a> I have been playing around obviously with various commands, wrapper methods and utility function for <a href="http://jquery.com">jQuery</a>.</p>
<p>This morning I was going through my feeds when I stumbled upon, <a href="http://www.gscottolson.com/blackbirdjs/">Blackbird</a>.  It&#8217;s an open source Javascript logger.</p>
<p>It&#8217;s a nice companion to Firebug.  I can add it to my page, and then toss some logging statements in my code.  With a hit of F2, I can then see the logger.</p>
<p>For example I was testing the grep utility function.  Previously I just tossed this in the Fireboug console, which works fine, but this way I can set up a whole bunch of code, and then look at the log as it happens in my page.  Leet.<br />
<code><br />
 log.info( $.grep([1, 2, 3], function(n){return n < 3}, true) );<br />
</code></p>
<p>I know <a href="http://search.cpan.org/~eric/OpenThought-1.99.16/lib/OpenThought.pm">OpenThought</a> has a logging component to it.  Which has been around for a while.  I am a little curious why no one had done this before now.</p>
<p>Is there already a way to do logging in Firebug?</code></p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/736/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Need to stop forgetting this</title>
		<link>http://nathanpowell.org/blog/archives/724</link>
		<comments>http://nathanpowell.org/blog/archives/724#comments</comments>
		<pubDate>Tue, 09 Sep 2008 17:24:44 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/archives/724</guid>
		<description><![CDATA[Create or append to array (Ruby): (array &#124;&#124;= []) < < 1]]></description>
			<content:encoded><![CDATA[<p>Create or append to array (Ruby):<br />
<code><br />
  (array ||= []) < < 1<br />
</code></code></p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/724/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Netbeans: Not as crappy as you might expect.</title>
		<link>http://nathanpowell.org/blog/archives/723</link>
		<comments>http://nathanpowell.org/blog/archives/723#comments</comments>
		<pubDate>Tue, 26 Aug 2008 14:40:26 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/archives/723</guid>
		<description><![CDATA[Over the last 3 days I have been using netbeans for RoR development. Honestly I expected to hate it. I figured I would use it for a few days, then crawl back to Emacs begging her to take me back &#8230; <a href="http://nathanpowell.org/blog/archives/723">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Over the last 3 days I have been using netbeans for RoR development.  Honestly I expected to hate it.  I figured I would use it for a few days, then crawl back to Emacs begging her to take me back with promises that I wouldn&#8217;t stray again.</p>
<p>Here is the truth.  I don&#8217;t hate it.  In fact, I kind of like it.</p>
<p>I like all the integration with Subversion (which we use at work) and Mercurial (which I use at home).  I like the Rails integration.  It has intellisense type functionality (though with a dynamic language is often unwieldy).  The syntax highlighting (while having a white background) is nice.</p>
<p>One of my big complaints however was a lack of really good keybindings.  I was missing my C-a, C-e and C-k.  A quick google later and I realized you can switch to Emacs keybindings.  Sweet.  So I have done that.</p>
<p>I hate to say it, but overall I am pretty happy with Netbeans.  If you are doing RoR development, and are chronically dissatisfied with software as I am, give it a shot.</p>
<p>My only complaint so far (and this might be fixable) is that tab navigation is chronological and not spatial (or serial you might call it).  It&#8217;s like using Alt-Tab to cycle through windows versus using virtual desktops to navigate.  I have to look into that further.</p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/723/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Use a specific Rails version during project creation</title>
		<link>http://nathanpowell.org/blog/archives/721</link>
		<comments>http://nathanpowell.org/blog/archives/721#comments</comments>
		<pubDate>Sun, 24 Aug 2008 14:01:25 +0000</pubDate>
		<dc:creator>Nathan Powell</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://nathanpowell.org/blog/archives/721</guid>
		<description><![CDATA[If you do any amount of Rails programming, at some point you end up with various versions of Rails installed on your system. Generally this is fine as Rails keeps what version should be used in it&#8217;s environment.rb file. However &#8230; <a href="http://nathanpowell.org/blog/archives/721">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you do any amount of Rails programming, at some point you end up with various versions of Rails installed on your system.  Generally this is fine as Rails keeps what version should be used in it&#8217;s environment.rb file.</p>
<p>However when you are creating a new project, the system rails command will use the most recent gem installed.</p>
<p>Also usually fine unless you know the latest version breaks something you want to use. <a href="http://www.riderx.net/">Andy</a> had mentioned to me before that it was possible to specify the version Rails should use on the command line when generating the initial project.  However I forgot what it was.  Lately I have been on a code reading kick so I decided to start at the start and see if I could remember what he had told me.</p>
<p>Well it didn&#8217;t take long.  The 3rd line of actual code in the system `rails` command is:</p>
<p><code><br />
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then<br />
</code></p>
<p>You can see, if you specify:<br />
<code><br />
rails _2.0.2_ project_name<br />
</code><br />
And that particular Rails version is correct and installed, it will use that version.  Thanks to Andy for pointing that out.</p>
]]></content:encoded>
			<wfw:commentRss>http://nathanpowell.org/blog/archives/721/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

