<?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>nklein software &#187; objective c</title>
	<atom:link href="http://nklein.com/tags/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 22 May 2012 03:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Parser Generator released</title>
		<link>http://nklein.com/2010/04/parser-generator-released/</link>
		<comments>http://nklein.com/2010/04/parser-generator-released/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 14:58:03 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Initial Releases]]></category>
		<category><![CDATA[Releases]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[parser-generator]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1274</guid>
		<description><![CDATA[A few weeks back, I described an XML Parser Generator that I was working on. At the time, it could generate the parser it used itself. Now, it&#8217;s got Objective-C support and Lisp support. (The Lisp support is slightly better than the Objective-C support right now. With the Objective-C backend, you can create arrays of [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks back, I described an <a href="http://nklein.com/2010/03/xml-parser-generator/">XML Parser Generator</a> that I was working on.  At the time, it could generate the parser it used itself.  Now, it&#8217;s got Objective-C support and Lisp support.  (The Lisp support is slightly better than the Objective-C support right now.  With the Objective-C backend, you can create arrays of structs, but not arrays of strings or integers.)</p>
<p>Here is the <a href="http://nklein.com/software/parser-generator/">Parser Generator home page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/04/parser-generator-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML Parser Generator</title>
		<link>http://nklein.com/2010/03/xml-parser-generator/</link>
		<comments>http://nklein.com/2010/03/xml-parser-generator/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 17:10:11 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[parser-generator]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1237</guid>
		<description><![CDATA[A few years back (for a very generous few), we needed to parse a wide variety of XML strings. It was quite tedious to go from the XML to the native-language representations of the data (even from a DOM version). Furthermore, we needed to parse this XML both in Java and in C++. I wrote [...]]]></description>
			<content:encoded><![CDATA[<p>A few years back (for a very generous <q>few</q>), we needed to parse a wide variety of XML strings.  It was quite tedious to go from the XML to the native-language representations of the data (even from a DOM version).  Furthermore, we needed to parse this XML both in Java and in C++.</p>
<p>I wrote (in Java) an XML parser generator that took an XML description of how you&#8217;d like the native-language data structures to look and where in the XML it could find the values for those data structures.  The Java code-base for this was ugly, ugly, ugly.  I tried several times to clean it up into something publishable.  I tried to clean it up several times so that it could actually generate the parser it used to read the XML description file.  Alas, the meta-ness, combined with the clunky Java code, kept me from completing the circle.</p>
<p>Fast forward to last week.  Suddenly, I have a reason to parse a wide variety of XML strings in Objective C.  I certainly didn&#8217;t want to pull out the Java parser generator and try to beat it into generating Objective C, too.  That&#8217;s fortunate, too, because I cannot find any of the copies (in various states of repair) that once lurked in <q>~/src</q>.</p>
<p>What&#8217;s a man to do?  Write it in Lisp, of course.</p>
<h3>Example</h3>
<p>Here&#8217;s an example to show how it works.  Let&#8217;s take some simple XML that lists food items on a menu:</p>
<div class="codecolorer-container xml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;food</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Belgian Waffles&quot;</span> <span style="color: #000066;">price</span>=<span style="color: #ff0000;">&quot;$5.95&quot;</span> <span style="color: #000066;">calories</span>=<span style="color: #ff0000;">&quot;650&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>two of our famous Belgian Waffles with plenty of real maple syrup<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/food<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- ... more food entries, omitted here for brevity ... --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div>
<p>We craft an XML description of how to go from the XML into a native representation:</p>
<div class="codecolorer-container xml blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;parser_generator</span> <span style="color: #000066;">root</span>=<span style="color: #ff0000;">&quot;menu&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;/menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struct</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;food item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;@name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;price&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;@price&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;description&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;/description/.&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;calories&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;@calories&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struct<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struct</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;menu items&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array_element</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;food item&quot;</span> <span style="color: #000066;">from</span>=<span style="color: #ff0000;">&quot;/food&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struct<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/parser_generator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div>
<p>Now, you run the parser generator on the above input file:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> parser-generator.sh <span style="color: #660033;">--language</span>=lisp \
                           <span style="color: #660033;">--types-package</span> menu \
                           <span style="color: #660033;">--reader-package</span> menu-reader \
                           <span style="color: #660033;">--file</span> menu.xml</pre></div>
<p>This generates two files for you: <q>types.lisp</q> and <q>reader.lisp</q>.  This is what <q>types.lisp</q> looks like:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">(defpackage :menu
  (:use :common-lisp)
  (:export #:food-item
             #:name
             #:price
             #:description
             #:calories
           #:menu
             #:menu-items))
&nbsp;
(in-package :menu)
&nbsp;
(defclass food-item ()
  ((name :initarg :name :type string)
   (price :initarg :price :type string)
   (description :initarg :description :type string)
   (calories :initarg :calories :type integer)))
&nbsp;
(defclass menu ()
  ((menu-items :initarg :menu-items :type list :initform nil)))</pre></div>
<p>I will not bore you with all of <q>reader.lisp</q> as it&#8217;s 134 lines of code you never had to write.  The only part you need to worry about is the <q>parse</q> function which takes a stream for or pathname to the XML and returns an instance of the <q>menu</q> class.  Here is a small snippet though:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">;;; =================================================================
;;; food-item struct
;;; =================================================================
(defmethod data progn ((handler sax-handler) (item food-item) path value)
  (with-slots (name price description calories) item
    (case path
      (:|@name| (setf name value))
      (:|@price| (setf price value))
      (:|/description/.| (setf description value))
      (:|@calories| (setf calories (parse-integer value))))))</pre></div>
<h3>Where it&#8217;s at</h3>
<p>I currently have the parser generator generating its own parser (five times fast).  I still have a little bit more that I&#8217;d like to add to include assertions for things like the minimum number of elements in an array or the minimum value of an integer.  I also have a few kinks to work out so that you can return some type other than an instance of a class for cases like this where the <q>menu</q> class just wraps one item.</p>
<p>My next step though is to get it generating Objective C parsers.</p>
<p>Somewhere in there, I&#8217;ll post this to a public git repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/03/xml-parser-generator/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Speedy iPhone App Programming</title>
		<link>http://nklein.com/2010/02/speedy-iphone-app-programming/</link>
		<comments>http://nklein.com/2010/02/speedy-iphone-app-programming/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 19:25:11 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[iphone app]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[vb quick team stats]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1164</guid>
		<description><![CDATA[Sunday, I decided that I needed a simpler statistics tracking program to keep track of stuff while I&#8217;m coaching volleyball. I started out keeping them on paper, but felt that I was staring at the page too often to find where to put a tick mark. Next, I tried the iVolleyStats Match iPhone app. It [...]]]></description>
			<content:encoded><![CDATA[<p>Sunday, I decided that I needed a simpler statistics tracking program to keep track of stuff while I&#8217;m coaching volleyball.  I started out keeping them on paper, but felt that I was staring at the page too often to find where to put a tick mark.</p>
<p>Next, I tried the <a href="http://itunes.apple.com/us/app/ivolleystats-match/id325343491?mt=8">iVolleyStats Match</a> iPhone app.  It is pretty reasonable to use, but it&#8217;s got too many controls on the user-interface.  The only way that I could keep up with a match was to forgo half of the functionality&#8230; either ignoring who is getting credit for an act, ignoring passing stats altogether, and not recording attack or block attempts at all.</p>
<p>For the past few weeks, I have tried using the Voice Memos application on the iPhone.  I narrate the game into my phone as the game goes on.  This lets me get really fine resolution of statistics, but it doesn&#8217;t give me any information in real-time.  When I call a time-out, I am going from memory to say how we&#8217;ve been passing or hitting.  This takes away the lion&#8217;s share of the benefit one gets from gathering statistics at all.</p>
<p><a href="http://nklein.com/wp-content/uploads/2010/02/QuickStats.jpg"><img src="http://nklein.com/wp-content/uploads/2010/02/QuickStats.jpg" alt="" title="QuickStats" width="320" height="480" class="alignright size-full wp-image-1165" /></a></p>
<p>So, my team has a tournament this Saturday.  I decided Sunday night that I should try to get together an iPhone app that does what I want.  I&#8217;ve long been thinking about what I want in a volleyball stat tracking iPhone app.  What I want will be a big, big undertaking (read: longer than one week).  So, I started studying Apple&#8217;s CoreData APIs on Sunday night and Monday morning.  Then, I dove in.</p>
<p>Now, my previous application was based upon <a href="http://www.cocos2d-iphone.org/">Cocos2D-iPhone</a>.  As such, it didn&#8217;t involve any of the Apple UIKit classes or any work with Interface Builder.  This application is navigation based with table views and custom table view cells from separate NIB files.</p>
<p>Despite this being my first real foray into the UIKit and CoreData APIs, I&#8217;ve got an application that I can use on Saturday.  There are two more bits that I will try to add tomorrow, but that I don&#8217;t need for Saturday.  To package it up for sale, there&#8217;s more functionality that I&#8217;ll need to add in case you don&#8217;t like things in the order that I have them on the screen or in case you want to add your own categories of statistic.  And, I need to make some specialized visualization screens for some of the stats.</p>
<p>The screenshot here is the main stat-tracking interface.  The stats present are Penn State&#8217;s stats from my trial run watching game one of their NCAA semifinal against Hawaii from last December.  In retrospect, I think I probably gave them credit for two neutral attacks that I should have called free balls.  Other than that, I think it&#8217;s pretty good.  It&#8217;s definitely information that will do me well during time-outs.</p>
<p>I fought for a long time this morning trying to get my UIBarButtonItem to show up in my UINavigationBar for one screen.  It turns out that these two methods don&#8217;t quite do the same thing on iPhoneOS 3.1.2:</p>
<div class="codecolorer-container objc blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// working version that shows my UIBarButton in the UINavigationBar</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showStatTrackingScreen <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>navigationController setViewControllers<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span>homeViewController<span style="color: #002200;">&#93;</span> animated<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>navigationController pushViewController<span style="color: #002200;">:</span>trackStatsController animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// version made of fail that does NOT show my UIBarButton in the UINavigationBar</span>
<span style="color: #11740a; font-style: italic;">// until you go forward a screen and pop back to this one.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showStatTrackingScreenMadeOfFail <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>navigationController setViewControllers<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>homeViewController,
                                                                       trackStatsController,
                                                                       <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>
                                    animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/02/speedy-iphone-app-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spelling iPhone App sent to Beta Testers</title>
		<link>http://nklein.com/2010/01/spelling-iphone-app-sent-to-beta-testers/</link>
		<comments>http://nklein.com/2010/01/spelling-iphone-app-sent-to-beta-testers/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 03:08:33 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[spell-it]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1156</guid>
		<description><![CDATA[I am pleased to say that I just sent my first iPhone app out to some friends to beta test. I expect to forward it along to Apple for inclusion in the App Store some time in the next week or two. At this point, I am far more comfortable with Objective-C and the Cocoa [...]]]></description>
			<content:encoded><![CDATA[<p>I am pleased to say that I just sent my <a href="http://nklein.com/software/spell-it-iphone-app/">first iPhone app</a> out to some friends to beta test.  I expect to forward it along to Apple for inclusion in the App Store some time in the next week or two.</p>
<p><a href="http://nklein.com/wp-content/uploads/2010/01/spell-it-large.jpg"><img src="http://nklein.com/wp-content/uploads/2010/01/spell-it-large-150x150.jpg" alt="" title="spell-it-large" width="150" height="150" class="alignright size-thumbnail wp-image-1157" /></a> At this point, I am far more comfortable with Objective-C and the Cocoa class hierarchy than I was even a month ago.  I still think Objective-C is awful.  You take a nice functional Smalltalk-ish language, you throw away most of the <q>functional</q>, you pretend like you have garbage collection when you don&#8217;t, you strip out any form of execution control, you add some funky compiler pragma-looking things (including one called <q>synthesize</q> that only fabricates about half of what you&#8217;d want it to build), you change the semantics of <q>-&gt;</q>, and then you interleave it with C!  Wahoo!  Instant headache!</p>
<p>But, after I found the for-each sort of construction, my code got quite a bit simpler.  A whole bunch of loops like this:</p>
<div class="codecolorer-container objc blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #400080;">NSEnumerator</span><span style="color: #002200;">*</span> ee <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>myArray enumerator<span style="color: #002200;">&#93;</span>;
MyItem<span style="color: #002200;">*</span> item;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> item <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>MyItem<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>ee nextObject<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   ...
<span style="color: #002200;">&#125;</span></pre></div>
<p>went to this:</p>
<div class="codecolorer-container objc blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> MyItem<span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> myArrayOrEnumerator <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   ...
<span style="color: #002200;">&#125;</span></pre></div>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/01/spelling-iphone-app-sent-to-beta-testers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Casting to Integers Considered Harmful</title>
		<link>http://nklein.com/2009/08/casting-to-integers-considered-harmful/</link>
		<comments>http://nklein.com/2009/08/casting-to-integers-considered-harmful/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 18:46:14 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[geometry]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[rounding]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=707</guid>
		<description><![CDATA[Background Many years back, I wrote some ambient music generation code. The basic structure of the code is this: Take one queen and twenty or so drones in a thirty-two dimensional space. Give them each random positions and velocities. Limit the velocity and acceleration of the queen more than you limit the same for the [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>Many years back, I wrote <a href="http://old.nklein.com/products/rmusic/">some ambient <q>music</q> generation code</a>.  The basic structure of the code is this:  Take one <q>queen</q> and twenty or so <q>drones</q> in a thirty-two dimensional space.  Give them each random positions and velocities.  Limit the velocity and acceleration of the queen more than you limit the same for the drones.  Now, select some point at random for the queen to target.  Have the queen accelerate toward that target.  Have the drones accelerate toward the queen.  Use the average distance from the drones to the queens in the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />-th dimension as the volume of the <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" />-th note where the notes are logarithmically spaced across one octave.  Clip negative volumes to zero.  Every so often, or when the queen gets close to the target, give the queen a new target.</p>
<p>It makes for some interesting ambient noise that sounds a bit like movie space noises where the lumbering enemy battleship is looming in orbit as its center portion spins to create artificial gravity within.</p>
<p>I started working on an iPhone application based on this code.  The original code was in C++.  The conversion to Objective C was fairly straightforward and fairly painless (as I used the opportunity to try to <a href="http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/">correct my own faults</a> by breaking things out into separate functions more often).</p>
<h3>Visualization troubles</h3>
<p><a href="http://nklein.com/wp-content/uploads/2009/08/uniform.png"><img src="http://nklein.com/wp-content/uploads/2009/08/uniform-300x225.png" alt="uniform" title="uniform" width="300" height="225" class="alignright size-medium wp-image-709" /></a><br />
The original code though chose random positions and velocities from uniform distributions.  The iPhone app is going to involve visualization as well as <q>auralization</q>.  The picture at the right here is a plot of five thousand points with each coordinate selected from a uniform distribution with range [-20,+20].  Because each axis value is chosen independently, it looks very unnatural.</p>
<p style="clear: both;">
<a href="http://nklein.com/wp-content/uploads/2009/08/gauss.png"><img src="http://nklein.com/wp-content/uploads/2009/08/gauss-300x225.png" alt="gauss" title="gauss" width="300" height="225" class="alignright size-medium wp-image-710" /></a><br />
What to do?  The obvious answer is to use <a href="http://en.wikipedia.org/wiki/Normal_distribution">Gaussian random variables</a> instead of uniform ones.  The picture at the right here is five thousand points with each coordinate selected from a Gaussian distribution with a standard-deviation of 10.  As you can see, this is much more natural looking.</p>
<h3 style="clear: both;">How did I generate the Gaussians?</h3>
<p>I have usually used the Box-Muller method of generating two Gaussian-distributed random variables given two uniformly-distributed random variables:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> random-gaussian <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>u1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>u2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>mag <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">sqrt</span> <span style="color: #66cc66;">&#40;</span>* -<span style="color: #cc66cc;">2.0</span> <span style="color: #66cc66;">&#40;</span>log u1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>ang <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">2.0</span> pi u2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>* mag <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cos</span> ang<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#40;</span>* mag <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">sin</span> ang<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>But, I found an article online that <a href="http://www.taygeta.com/random/gaussian.html">shows a more numerically stable version</a>:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> random-gaussian <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>flet <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>pick-in-circle <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>loop as u1 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
                as u2 <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">random</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
                as mag-squared <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* u1 u1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* u2 u2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&lt;</span> mag-squared <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>values u1 u2 mag-squared<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>multiple-value-bind <span style="color: #66cc66;">&#40;</span>u1 u2 mag-squared<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pick-in-circle<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ww <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">sqrt</span> <span style="color: #66cc66;">&#40;</span>/ <span style="color: #66cc66;">&#40;</span>* -<span style="color: #cc66cc;">2.0</span> <span style="color: #66cc66;">&#40;</span>log mag-squared<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> mag-squared<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>values <span style="color: #66cc66;">&#40;</span>* u1 ww<span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#40;</span>* u2 ww<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>For a quick sanity check, I thought, <q>let&#8217;s just make sure it looks like a Gaussian.</q>  Here, I showed the code in Lisp, but the original code was in Objective-C.  I figured, <q>If I just change the function declaration, I can plop this into a short C program, run a few thousand trials into some histogram buckets, and see what I get.</q></p>
<h3>The trouble with zero</h3>
<p>So, here comes the problem with zero.  I had the following main loop:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#define BUCKET_COUNT 33</span>
<span style="color: #339933;">#define STDDEV       8.0</span>
<span style="color: #339933;">#define ITERATIONS   100000</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> ii<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>ii <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> bb <span style="color: #339933;">=</span> val_to_bucket<span style="color: #009900;">&#40;</span> STDDEV <span style="color: #339933;">*</span> gaussian<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">&lt;=</span> bb <span style="color: #339933;">&amp;&amp;</span> bb <span style="color: #339933;">&lt;</span> BUCKET_COUNT <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #339933;">++</span>buckets<span style="color: #009900;">&#91;</span> bb <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div>
<p>I now present you with three different implementations of the <em>val_to_bucket()</em> function.</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>_val <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> _val <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> _val <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<p>As you can probably guess, after years or reading trick questions, only the last one actually works as far as my main loop is concerned.  Why?  Every number between -1 and +1 becomes zero when you cast the double to an integer.  That&#8217;s twice as big a range as any other integer gets.  So, for the first implementation, the middle bucket has about twice as many things in it as it should.  For the second implementation, the first bucket has more things in it than it should.  For the final implementation, the non-existent bucket before the first one is the overloaded bucket.  In the end, I used this implementation instead so that I wouldn&#8217;t even bias non-existent buckets:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> val_to_bucket<span style="color: #009900;">&#40;</span> <span style="color: #993333;">double</span> _val <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>lround<span style="color: #009900;">&#40;</span>_val<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span> BUCKET_COUNT <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/08/casting-to-integers-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programming When Clarity Counts</title>
		<link>http://nklein.com/2009/05/programming-when-clarity-counts/</link>
		<comments>http://nklein.com/2009/05/programming-when-clarity-counts/#comments</comments>
		<pubDate>Wed, 06 May 2009 04:10:25 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=285</guid>
		<description><![CDATA[In my previous post, I wrote some code in Perl because I wanted the code to be clear and obvious. Wait? What? Who uses Perl when they want clarity? Perl I admit: I was surprised myself. Seriously, I tried several other languages first. Let&#8217;s just focus on the first loop here. Here&#8217;s the Perl again [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://nklein.com/2009/05/how-to-make-a-weighted-random-choice/">my previous post</a>,  I wrote some code in Perl because I wanted the code to be clear and obvious.  Wait?  What?  Who uses Perl when they want clarity?</p>
<h3>Perl</h3>
<p>I admit:  I was surprised myself.  Seriously, I tried several other languages first.  Let&#8217;s just focus on the first loop here.  Here&#8217;s the Perl again for reference:</p>
<div class="codecolorer-container perl blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@list_of_items</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$total</span> <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$item</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<h3>Objective C</h3>
<p>The actual code that I was attempting to explain was in Objective C with the NeXTStep foundation classes.  I couldn&#8217;t imagine that anyone would want to try to understand it:</p>
<div class="codecolorer-container objc blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">double</span> total <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
<span style="color: #400080;">NSEnumerator</span><span style="color: #002200;">*</span> ee <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>itemList objectEnumerator<span style="color: #002200;">&#93;</span>;
Item<span style="color: #002200;">*</span> item;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> item <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Item<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>ee nextObject<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    total <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>item weight<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div>
<h3>C</h3>
<p>My first attempt for the article was to write it in C using an array of items.</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">double</span> total <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ii<span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> ii <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> ii <span style="color: #339933;">&lt;</span> itemCount<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>ii <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    total <span style="color: #339933;">+=</span> weight<span style="color: #009900;">&#40;</span> itemArray<span style="color: #009900;">&#91;</span> ii <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div>
<p>That&#8217;s not too terrible.  There is, however, a great deal of syntax and code devoted to handling the array including an extra variable to track its length.  Beyond that, the loop counter is of little use to someone trying to understand the concept.</p>
<h3>C++</h3>
<p>My next go was C++.  My hope was that iterators would make the looping less painful.  C++ iterators, however, are the very model of pain.</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">double</span> total <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span><span style="color: #008080;">;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #000080;">&lt;</span> Item<span style="color: #000040;">*</span> <span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> it<span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> it <span style="color: #000080;">=</span> itemList.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> itemList.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> weight<span style="color: #008000;">&#40;</span> <span style="color: #000040;">*</span>it <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div>
<p>This requires the reader to wade through a syntactic thicket to get the iterator into the picture at all.  It also requires a certain comfort with pointers that one wouldn&#8217;t have coming from another language.</p>
<h3>Java</h3>
<p>My next attempt was Java.  I hate Java.  It may not be terrible for this code with the new looping constructs that came in Java 1.5.  Alas, I quit coding it before I made it through this first loop.</p>
<h3>Lisp</h3>
<p>Of course, I wanted to code this in Lisp:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>total <span style="color: #66cc66;">&#40;</span>reduce #'+ list-of-items <span style="color: #66cc66;">:</span><span style="color: #555;">key</span> #'weight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>That would be perfectly clear to someone who has used Lisp or Scheme.  Alas, not everyone has.</p>
<h3>So&#8230; Perl&#8230;</h3>
<p>If you want to write the Perl code, you have to understand the difference between scalars and arrays.  To read the code, however, you don&#8217;t have to grok that distinction to see what&#8217;s going on.  I didn&#8217;t try it in Python.  Maybe it would be clear to a wider audience.  I may have to try it next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/programming-when-clarity-counts/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Sapir-Whorf with Programming Languages</title>
		<link>http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/</link>
		<comments>http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 22:29:31 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[objective c]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=120</guid>
		<description><![CDATA[I've been coding in Objective-C for a month or so now.  It is interesting (in a <a href="http://en.wikipedia.org/wiki/Sapir-Whorf_hypothesis">Sapir-Whorf</a> sort of way), how the language changes the way that I code.  Switching from Objective-C to Lisp made me notice something that I hadn't really noticed when moving from C++/Java/Perl to Lisp.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been coding in Objective-C for a month or so now.  It is interesting (in a <a href="http://en.wikipedia.org/wiki/Sapir-Whorf_hypothesis">Sapir-Whorf</a> sort of way), how the language changes the way that I code.  Switching from Objective-C to Lisp made me notice something that I hadn&#8217;t really noticed when moving from C++/Java/Perl to Lisp.</p>
<blockquote><p>
In Lisp, I will pull something out into a separate function if it makes the current function more self-contained, more one idea.  In C++, I will only pull something out into a separate function if I need the same functionality in multiple places.
</p></blockquote>
<p>Actually, it&#8217;s even worse than that in C++.  For stuff that is less than six or so lines, I might maintain it in several functions.  Or, if I&#8217;m using some Literate Programming tool, I will just use the same chunk in multiple places.  The notable exception in C++ is when I want to use something as a loop conditional, I may bother to break it out into its<br />
own function:</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> incrementCounter<span style="color: #008000;">&#40;</span> cntr, min, max, dimensions <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// body of loop here</span>
<span style="color: #008000;">&#125;</span></pre></div>
<p>In C++ or Objective-C, I might do something like this:</p>
<div class="codecolorer-container cpp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// some portion of my function</span>
&nbsp;
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> choice <span style="color: #000080;">=</span> random<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> length<span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> currentChoice <span style="color: #000080;">=</span> options<span style="color: #008000;">&#91;</span> choice <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// yes, I know I can memcpy(), but that's not as obvious</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ii<span style="color: #000080;">=</span>choice<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> ii <span style="color: #000080;">&lt;</span> length<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>ii <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    options<span style="color: #008000;">&#91;</span> ii<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> options<span style="color: #008000;">&#91;</span> ii <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #000040;">--</span>length<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// some code using currentChoice</span></pre></div>
<p>In Lisp, I would never consider keeping that code inline.  I would put it in another function right away.</p>
<p>This, this is one of the things I meant when I said <q>Lisp is just plain fun.</q>  It&#8217;s easy to make a new function.  I can just do it.  I don&#8217;t have to fret too much over the name.  I don&#8217;t have to fret too much over the argument<br />
list.  I don&#8217;t have to pre-declare it in this header file with the same signature as that implementation file.  I<br />
don&#8217;t have to pretend its a method when it&#8217;s really just a function.  I can return multiple values if I need to do so.  I don&#8217;t have to worry much about which compilation units will need to see this to compile.</p>
<p>Part of the maze generation code that I wrote in Objective-C needs to track walls.  I don&#8217;t need the same structure during generation that I will use once it&#8217;s generated.  So, I have a Wall class declared.  It feels wrong to declare it right in the header file for the Maze.  It feels silly to break it out into its own header file.  What I should be doing is making a separate MazeFactory and have its implementation include the declaration of this Wall class.  But, that is such overkill here.  I just want the darn maze.</p>
<p>In Lisp, I would just be done with no feelings of guilt at all.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

