<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: iteration code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 19:00:02 GMT</pubDate>
    <description>DZone Snippets: iteration code</description>
    <item>
      <title>Treating first/last loop iterations differently</title>
      <link>http://snippets.dzone.com/posts/show/4353</link>
      <description>Sometimes, you need to iterate over a list of items and treat the first and last element differently from the rest. This tends to produce really messy code, which can be avoided using the following snippet.&lt;br /&gt;&lt;br /&gt;Imagine you have a list of Kittens, and you want to print out a summary of them, saying "My kittens are called Bob, James, John, and Ally." You need to treat the first and last element of the list differently from the rest, lest you end up with superfluous commas in the text. You also need to deal with the cases of there being only one kitten, or no kittens at all.&lt;br /&gt;&lt;br /&gt;To do this, you can avail yourself of a Counter class that keeps track of where you are in the iteration:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;String kittenDesc = "I have no kittens.";&lt;br /&gt;Counter kc = new Counter(kittens);&lt;br /&gt;for (Kitten k : kittens) switch(kc.next()) {&lt;br /&gt;	case one:   kittenDesc =  "My kitten is called " + k.getName() + "."; break;&lt;br /&gt;	case first: kittenDesc =  "My kittens are called " + k.getName();     break;&lt;br /&gt;	case item:  kittenDesc += ", " + k.getName();                         break;&lt;br /&gt;	case last:  kittenDesc += " and " + k.getName() + ".";                break;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;package com.zarkonnen.util;&lt;br /&gt;&lt;br /&gt;import java.util.Collection;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Used for keeping track of where you are in a collection/array being iterated over. Use by&lt;br /&gt; * initialising with the collection/array before the loop and embedding a switch on the next() Mode&lt;br /&gt; * value into the loop.&lt;br /&gt; *&lt;br /&gt; * LICENCE: This code is licenced under a BSD licence. Feel free to alter and redistribute.&lt;br /&gt; *&lt;br /&gt; * @author David Stark, http://www.zarkonnen.com&lt;br /&gt; * @version 1.0 (2007-07-11)&lt;br /&gt; */&lt;br /&gt;public class Counter {&lt;br /&gt;	/**&lt;br /&gt;	 * An enumeration of modes identifying where in the collection/array we are.&lt;br /&gt;	 */&lt;br /&gt;	public enum Where {&lt;br /&gt;		/** The only element of an array/collection of size 1. */ one,&lt;br /&gt;		/** The first element. */                                 first,&lt;br /&gt;		/** The last element of the array/collection. */          last,&lt;br /&gt;		/** Any other element somewhere in the middle. */         item&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	private int size;&lt;br /&gt;	private int nextIndex = 0;&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @param c A collection to keep track of. If its size changes between now and the&lt;br /&gt;	 * iteration, strange things will happen.&lt;br /&gt;	 */&lt;br /&gt;	public Counter(Collection c) {&lt;br /&gt;		size = c.size();&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @param a An array to keep track of. If its size changes between now and the iteration,&lt;br /&gt;	 * strange things will happen.&lt;br /&gt;	 */&lt;br /&gt;	public Counter(Object[] a) {&lt;br /&gt;		size = a.length;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @return A Where enum value for where in the array/collection we now are:&lt;br /&gt;	 * &lt;ul&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;one&lt;/strong&gt; at the only element of an array/collection of size 1&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;first&lt;/strong&gt; at the first element&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;last&lt;/strong&gt; at the last element&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;item&lt;/strong&gt; at any other element&lt;/li&gt;&lt;br /&gt;	 * &lt;/ul&gt;&lt;br /&gt;	 */&lt;br /&gt;	public Where next() {&lt;br /&gt;		return size == 1         ? Where.one   :&lt;br /&gt;		       nextIndex++ == 0  ? Where.first :&lt;br /&gt;		       nextIndex == size ? Where.last  :&lt;br /&gt;		                           Where.item  ;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @return Which index of the array/collection we're currently at.&lt;br /&gt;	 */&lt;br /&gt;	public int index() {&lt;br /&gt;		return nextIndex == 0 ? 0 : nextIndex - 1;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Jul 2007 10:10:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4353</guid>
      <author>Zarkonnen (David Stark)</author>
    </item>
    <item>
      <title>Lua: Descending Table Traversal Round 2: Iterator Object</title>
      <link>http://snippets.dzone.com/posts/show/940</link>
      <description>This is some concept code I threw together for Lua to see how feasable it was to make a lua object/table into an iterator that you can manipulate.  This iterator descends through all tables and subtables of any queued table.  It returns the "path" we're at, as a string, the current table, the key, and the value.&lt;br /&gt;&lt;br /&gt;Code:&lt;br /&gt;&lt;code&gt;traverse = {}&lt;br /&gt;function traverse:new(tname)&lt;br /&gt;	local o = {}&lt;br /&gt;	o.names = {}&lt;br /&gt;	o.queue = {}&lt;br /&gt;	o.cur = {&lt;br /&gt;		tbl = nil,&lt;br /&gt;		path = nil,&lt;br /&gt;		state = nil,&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	local mt = {}&lt;br /&gt;	function mt:__call(tn)&lt;br /&gt;		return o:iter(tn)&lt;br /&gt;	end&lt;br /&gt;	mt.__index = self&lt;br /&gt;	setmetatable(o, mt)&lt;br /&gt;&lt;br /&gt;	if tname then&lt;br /&gt;		o:enqueue(tname)&lt;br /&gt;	end&lt;br /&gt;	return o&lt;br /&gt;end&lt;br /&gt;function traverse:next()&lt;br /&gt;	local v&lt;br /&gt;	local names, queue, cur = self.names, self.queue, self.cur&lt;br /&gt;&lt;br /&gt;	local function _poptbl()&lt;br /&gt;		if cur.tbl then&lt;br /&gt;			names[cur.tbl] = nil&lt;br /&gt;		end&lt;br /&gt;		cur.tbl = table.remove(queue, 1)&lt;br /&gt;		cur.path = names[cur.tbl]&lt;br /&gt;		cur.state = nil&lt;br /&gt;	end&lt;br /&gt;&lt;br /&gt;	repeat&lt;br /&gt;		-- Find something to return to the user...&lt;br /&gt;		if not cur.state then&lt;br /&gt;			-- Pop a new table off the stack&lt;br /&gt;			_poptbl()&lt;br /&gt;			if not cur.tbl then&lt;br /&gt;				-- No more tables to process&lt;br /&gt;				return nil&lt;br /&gt;			end&lt;br /&gt;		end&lt;br /&gt;		cur.state,v = next(cur.tbl, cur.state)&lt;br /&gt;	until cur.state&lt;br /&gt;&lt;br /&gt;	if type(v) == "table" then&lt;br /&gt;		local path = cur.path.."."..cur.state&lt;br /&gt;		names[v] = path&lt;br /&gt;		table.insert(queue, v)&lt;br /&gt;	end&lt;br /&gt;	return cur.path,cur.tbl,cur.state,v&lt;br /&gt;end&lt;br /&gt;function traverse:iter(tname)&lt;br /&gt;	if tname then&lt;br /&gt;		self:enqueue(tname)&lt;br /&gt;	end&lt;br /&gt;	return function(...) return self:next(unpack(arg)) end, nil, nil&lt;br /&gt;end&lt;br /&gt;function traverse:enqueue(tname)&lt;br /&gt;	local v = _G[tname]&lt;br /&gt;	table.insert(self.queue, v)&lt;br /&gt;	self.names[v] = tname&lt;br /&gt;end&lt;br /&gt;function traverse:reset()&lt;br /&gt;	self.names = {}&lt;br /&gt;	self.queue = {}&lt;br /&gt;end&lt;br /&gt;local traverse_mt = {&lt;br /&gt;    __call = function(self, tname)&lt;br /&gt;             	if tname then&lt;br /&gt;             		return self:new(tname):iter()&lt;br /&gt;             	else&lt;br /&gt;             		return self:new()&lt;br /&gt;             	end&lt;br /&gt;             end&lt;br /&gt;}&lt;br /&gt;setmetatable(traverse, traverse_mt)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example Usage:&lt;br /&gt;&lt;code&gt;foo = {a={},b={},c={"bar"},d={e={},f={"moo"}},1,2,3,4,5}&lt;br /&gt;bar = {"alpha", "beta", "theta", omega = {}}&lt;br /&gt;&lt;br /&gt;local mytraverser = traverse()&lt;br /&gt;mytraverser:enqueue("bar")&lt;br /&gt;mytraverser:enqueue("foo")&lt;br /&gt;for p,t,k,v in mytraverser() do&lt;br /&gt;--for p,t,k,v in traverse('foo') do&lt;br /&gt;	print(string.format("%s[%s] = %s",tostring(p),tostring(k),tostring(v)))&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Output from Example Usage:&lt;br /&gt;&lt;code&gt;bar[1] = alpha&lt;br /&gt;bar[2] = beta&lt;br /&gt;bar[3] = theta&lt;br /&gt;bar[omega] = table: 0x510650&lt;br /&gt;foo[1] = 1&lt;br /&gt;foo[2] = 2&lt;br /&gt;foo[3] = 3&lt;br /&gt;foo[4] = 4&lt;br /&gt;foo[5] = 5&lt;br /&gt;foo[a] = table: 0x5102d0&lt;br /&gt;foo[c] = table: 0x510370&lt;br /&gt;foo[b] = table: 0x510320&lt;br /&gt;foo[d] = table: 0x5103c0&lt;br /&gt;foo.c[1] = bar&lt;br /&gt;foo.d[e] = table: 0x5104c0&lt;br /&gt;foo.d[f] = table: 0x510510&lt;br /&gt;foo.d.f[1] = moo&lt;/code&gt;</description>
      <pubDate>Mon, 05 Dec 2005 13:27:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/940</guid>
      <author>kergoth (Chris Larson)</author>
    </item>
  </channel>
</rss>
