/
index.html
331 lines (204 loc) · 19.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Array slicing is messed up - jBoxer</title>
<meta name="author" content="Jake Boxer">
<meta name="description" content="The mechanics of array-slicing (and, by extension, string-slicing) have bothered me since my very first Computer Science class. For those of you wh...">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://jakeboxer.github.com/blog/2008/11/06/array-slicing-is-messed-up/">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="/javascripts/ender.js"></script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<link href="/atom.xml" rel="alternate" title="jBoxer" type="application/atom+xml">
<!--Fonts from Google's Web font directory at http://google.com/webfonts -->
<link href='http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic' rel='stylesheet' type='text/css'>
</head>
<body >
<header role="banner"><hgroup>
<h1><a href="/">jBoxer</a></h1>
<h2>I change the directions of small pieces of metal for a living.</h2>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:jakeboxer.github.com" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul role=main-navigation>
<li><a href="/">Blog</a></li>
<li><a href="/blog/archives">Archives</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div>
<article class="hentry" role="article">
<header>
<h1 class="entry-title">Array slicing is messed up</h1>
<p class="meta">
<time datetime="2008-11-06T15:56:00-08:00" pubdate data-updated="true" >Nov 6<span>th</span>, 2008</time>
| <a href="#disqus_thread">Comments</a>
</p>
</header>
<div class="entry-content"><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’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>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">array</span> <span class="o">=</span> <span class="p">[</span><span class="mi">99</span><span class="p">,</span> <span class="mi">72</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">61</span><span class="p">,</span> <span class="mi">88</span><span class="p">,</span> <span class="mi">32</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>
<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:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">print</span> <span class="n">array</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c"># prints '99' (the 1st element in the list)</span>
</span><span class='line'><span class="k">print</span> <span class="n">array</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="c"># prints '100' (the 5th element in the list)</span>
</span><span class='line'><span class="k">print</span> <span class="n">array</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="c"># prints '32' (the 8th and last element in the list)</span>
</span></code></pre></td></tr></table></div></figure>
<p>The concept of “array-slicing” means creating a new array out of a subsequence from a previous array. For example, a “slice” of the above array could be:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">newArray</span> <span class="o">=</span> <span class="p">[</span><span class="mi">85</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">61</span><span class="p">]</span> <span class="c"># a slice containing the 4th-6th elements of the original array</span>
</span></code></pre></td></tr></table></div></figure>
<p>Most programming languages provide a function that allows you to take a “slice” of an already-existing array. They do this by asking you to specify where in the original array you’d like to start the slice, and where you’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>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">newArray</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span> <span class="c"># creates a new array with the 4th, 5th, and 6th elements from the original array</span>
</span></code></pre></td></tr></table></div></figure>
<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’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’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>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">array</span> <span class="o">=</span> <span class="p">[</span><span class="mi">99</span><span class="p">,</span> <span class="mi">72</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">61</span><span class="p">,</span> <span class="mi">88</span><span class="p">,</span> <span class="mi">32</span><span class="p">]</span>
</span><span class='line'><span class="k">print</span> <span class="n">array</span><span class="o">.</span><span class="n">length</span><span class="p">()</span> <span class="c"># prints '8', since there are 8 elements in the array</span>
</span></code></pre></td></tr></table></div></figure>
<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>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">array</span> <span class="o">=</span> <span class="p">[</span><span class="mi">99</span><span class="p">,</span> <span class="mi">72</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">61</span><span class="p">,</span> <span class="mi">88</span><span class="p">,</span> <span class="mi">32</span><span class="p">]</span>
</span><span class='line'><span class="n">newArray</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">array</span><span class="o">.</span><span class="n">length</span><span class="p">())</span> <span class="c"># equivalent to newArray = array.slice(3, 8)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Since length() gives back the length of the list, it’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 “up to the last item in my array.”</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>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">array</span> <span class="o">=</span> <span class="p">[</span><span class="mi">99</span><span class="p">,</span> <span class="mi">72</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">61</span><span class="p">,</span> <span class="mi">88</span><span class="p">,</span> <span class="mi">32</span><span class="p">]</span>
</span><span class='line'><span class="n">newArray</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">array</span><span class="o">.</span><span class="n">length</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># equivalent to newArray = array.slice(3, 7)</span>
</span></code></pre></td></tr></table></div></figure>
<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’ve only included one index instead of two, it <em>assumes</em> that the last parameter refers to the end of the list.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">array</span> <span class="o">=</span> <span class="p">[</span><span class="mi">99</span><span class="p">,</span> <span class="mi">72</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">85</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">61</span><span class="p">,</span> <span class="mi">88</span><span class="p">,</span> <span class="mi">32</span><span class="p">]</span>
</span><span class='line'><span class="n">newArray1</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="c"># equivalent to newArray = [85, 100, 61]</span>
</span><span class='line'><span class="n">newArray2</span> <span class="o">=</span> <span class="n">array</span><span class="o">.</span><span class="n">slice</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c"># equivalent to newArray = array.slice(3, 7) = [85, 100, 61, 88, 32]</span>
</span></code></pre></td></tr></table></div></figure>
<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’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’m not suggesting that it’s straight up <em>wrong</em> and should be changed immediately; I just don’t understand why it works this way in every language.</p>
</div>
<footer>
<p class="meta">
<span class="byline author vcard">Posted by <span class="fn">Jake Boxer</span></span>
<time datetime="2008-11-06T15:56:00-08:00" pubdate data-updated="true" >Nov 6<span>th</span>, 2008</time>
</p>
<div class="sharing">
<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://jakeboxer.github.com/blog/2008/11/06/array-slicing-is-messed-up/" data-via="jake_boxer" data-counturl="http://jakeboxer.github.com/blog/2008/11/06/array-slicing-is-messed-up/" >Tweet</a>
<div class="g-plusone" data-size="medium"></div>
</div>
</footer>
</article>
<section>
<h1>Comments</h1>
<div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</section>
</div>
<aside class="sidebar">
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/blog/2012/07/09/converting-to-the-new-rspec-2-dot-11-expectation-syntax/">Converting to the new RSpec 2.11 expectation syntax</a>
</li>
<li class="post">
<a href="/blog/2012/03/15/changing-a-rails-3-project-name/">Changing a Rails 3 project name</a>
</li>
<li class="post">
<a href="/blog/2011/11/16/bostinnovations-seth-priebatsch-article-is-childish-garbage/">BostInnovation's Seth Priebatsch Article is Childish Garbage</a>
</li>
<li class="post">
<a href="/blog/2011/11/06/bashs-ps1-syntax-the-inspiration-for-brainfuck/">Bash's PS1 Syntax: The Inspiration for Brainfuck?</a>
</li>
<li class="post">
<a href="/blog/2011/10/09/using-acts-as-list-in-a-polymorphic-scope/">Using acts_as_list in a polymorphic scope</a>
</li>
</ul>
</section>
<section>
<h1>Latest Tweets</h1>
<ul id="tweets">
<li class="loading">Status updating...</li>
</ul>
<script type="text/javascript">
$.domReady(function(){
getTwitterFeed("jake_boxer", 4, false);
});
</script>
<script src="/javascripts/twitter.js" type="text/javascript"> </script>
<a href="http://twitter.com/jake_boxer" class="twitter-follow-button" data-width="208px" data-show-count="false">Follow @jake_boxer</a>
</section>
</aside>
</div>
</div>
<footer role="contentinfo"><p>
Copyright © 2012 - Jake Boxer -
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
</p>
</footer>
<script type="text/javascript">
(function () {
var disqus_shortname = 'jboxer';
// var disqus_developer = 1;
var disqus_identifier = 'http://jakeboxer.github.com/blog/2008/11/06/array-slicing-is-messed-up/';
var disqus_url = 'http://jakeboxer.github.com/blog/2008/11/06/array-slicing-is-messed-up/';
var disqus_script = 'embed.js'
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}());
</script>
<script type="text/javascript">
(function() {
var script = document.createElement('script'); script.type = 'text/javascript'; script.async = true;
script.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(script, s);
})();
</script>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9253249-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>