<?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>Aparajita’s World</title>
	<atom:link href="http://www.aparajitaworld.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aparajitaworld.com</link>
	<description>Rock-solid web apps and development tools</description>
	<lastBuildDate>Fri, 01 Jun 2012 00:48:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>The joys of for each</title>
		<link>http://www.aparajitaworld.com/2006/05/the-joys-of-for-each/</link>
		<comments>http://www.aparajitaworld.com/2006/05/the-joys-of-for-each/#comments</comments>
		<pubDate>Wed, 31 May 2006 13:08:04 +0000</pubDate>
		<dc:creator>aparajita</dc:creator>
				<category><![CDATA[Active4D]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://www.aparajitaworld.com/blog/?p=4</guid>
		<description><![CDATA[<p>One advantage of writing my own interpreter is the ability to add new features to the language. I have taken quite a bit of inspiration from other languages I have used, such as C++ and python. If you have used collections in Active4D, you may have used for each to iterate through a collection. Starting with v4.0, for each can ...</p><p>The post <a href="http://www.aparajitaworld.com/2006/05/the-joys-of-for-each/">The joys of for each</a> appeared first on <a href="http://www.aparajitaworld.com">Aparajita’s World</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>One advantage of writing my own interpreter is the ability to add new features to the language. I have taken quite a bit of inspiration from other languages I have used, such as C++ and python.</p>
<p>If you have used collections in Active4D, you may have used <code>for each</code> to iterate through a collection. Starting with v4.0, for each can be used to iterate through much more than collections.<span id="more-13"></span> Before going into the new ways of using <code>for each</code>, let us start with a bit of terminology.</p>
<p>A <strong>sequence</strong> is a collection of ordered elements which can be traversed from the beginning to the end. In Active4D, sequences include collections, arrays, strings and selections of records. Note that although the elements of a sequence are ordered, the order may only be known internally, in which case the elements have no numeric index. This is the case with collections. With the other sequence types the elements have a numeric index.</p>
<p>How does this relate to <code>for each</code>? In v4.0, <code>for each</code> is a generic means of iterating over <em>any</em> sequence type. As with the ~ (in) operator, it allows you to unify the syntax for iterating over different types.</p>
<h4>Examples</h4>
<p>Okay, now let&#8217;s see some examples so it will hopefully start to make sense. First off we&#8217;ll start by looking at <code>for each</code> with collections, since we have (hopefully) already used that:</p>
<pre class="fancy_pre_box">
for each($c; $key; $value)
    // Within the loop, $key and $value are set
    // for the current element
end for each
</pre>
<p>Now let&#8217;s extend the use of <code>for each</code> to other sequence types, starting with arrays:</p>
<pre class="fancy_pre_box">
// Here's the old way:
for ($i; 1; size of array($array))
    writebr('$i. $array{$i}')
end for</p>

<p>// Here's the new way:
for each ($array; $value; $i)
    writebr('$i. $value')
end for each
</pre>
<p>You may be wondering what the big deal is. There are a few advantages:</p>
<ul>
<li>The code is more concise.</li>
<li>If you don&#8217;t need <code>$i</code> you can leave it out of the <code>for each</code>.</li>
<li>If you need to reference <code>$array{$i}</code> several times, using a variable like <code>$value</code> is easier to read and faster in execution.</li>
</ul>
<p>In a similar manner we can iterate through strings:</p>
<pre class="fancy_pre_box">
// Here's the old way:
for ($i; 1; length($s))
    writebr('$i. $s[[$i]]')
end for</p>

<p>// Here's the new way:
for each ($s; $char; $i)
    writebr('$i. $char')
end for each</pre>
<p>Finally, we can iterate through a selection of records as well:</p>
<pre class="fancy_pre_box">
// Here's the old way:
first record([contacts])</p>

<p>while (not(end selection([contacts])))
    $i := selected record number([contacts])
    writebr('$i. [contacts]name')
    next record([contacts])
end while</p>

<p>// Here's the new way:
for each ([contacts]; $i)
    writebr('$i. [contacts]name')
end for each
</pre>
<p>Of course, you never iterate through a selection of records to display a set of records, you use a RowSet, right? <img src='http://www.aparajitaworld.com/site/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>The post <a href="http://www.aparajitaworld.com/2006/05/the-joys-of-for-each/">The joys of for each</a> appeared first on <a href="http://www.aparajitaworld.com">Aparajita’s World</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.aparajitaworld.com/2006/05/the-joys-of-for-each/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More concise coding: using the &#8220;in&#8221; operator</title>
		<link>http://www.aparajitaworld.com/2006/05/more-concise-coding-using-the-in-operator/</link>
		<comments>http://www.aparajitaworld.com/2006/05/more-concise-coding-using-the-in-operator/#comments</comments>
		<pubDate>Thu, 04 May 2006 17:41:15 +0000</pubDate>
		<dc:creator>aparajita</dc:creator>
				<category><![CDATA[Active4D]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://www.aparajitaworld.com/blog/?p=3</guid>
		<description><![CDATA[<p>I was coding the other day and wrote these kinds of tests for the millionth time: So I said to myself, “Self, there has got to be a better and faster way to do these tests.” In some other languages (notably python, from which I have stolen a lot of ideas), there is a keyword called in that tests for ...</p><p>The post <a href="http://www.aparajitaworld.com/2006/05/more-concise-coding-using-the-in-operator/">More concise coding: using the &#8220;in&#8221; operator</a> appeared first on <a href="http://www.aparajitaworld.com">Aparajita’s World</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>I was coding the other day and wrote these kinds of tests for the millionth time:</p>
<pre class="fancy_pre_box">
if (position("foo"; $bar) > 0)
if (find in array($bar; "foo") > 0)
if (collection has($bar; "foo"))
if ($bar{"foo"} # "")
</pre>
<p>So I said to myself, “Self, there has got to be a better and faster way to do these tests.”<span id="more-12"></span></p>
<p>In some other languages (notably python, from which I have stolen a lot of ideas), there is a keyword called <code>in</code> that tests for the existence of some value within a sequence of values. Looking at all of the tests above, I realized that they are all <code>in</code> tests.</p>
<p>In python, you say things like <code>if "foo" in bar</code>, where <code>bar</code> is some kind of sequence. This syntax is extremely intuitive, but unfortunately I could not use <code>in</code> in 4D, because in 4D an identifier can consist of multiple words separated by spaces, leading to a situation like this:</p>
<pre class="fancy_pre_box">
if (mylib.getValue in $myCollection)
</pre>
<p>Can you guess what goes wrong? The parser sees <code>mylib.getValue in</code> as a single identifier and the interpreter chokes because no such identfier exists. This can be worked around by surrounding <code>mylib.getValue</code> in parentheses, but that is way ugly and prone to be forgotten.</p>
<p>So I had to use a non-identifier character to represent <code>in</code>, and I chose <code>~</code>. Now you can replace all of the tests above with one expression:</p>
<pre class="fancy_pre_box">
if ("foo" ~ $bar)
</pre>
<p>To test for <code>not in</code>, you use the new <code>!~</code> operator. Much faster and to me more intuitive (if you see <code>~</code> as <code>in</code>). This operator made its appearance in v4.0rc7. I hope you take the time to use it!</p>
<p>The post <a href="http://www.aparajitaworld.com/2006/05/more-concise-coding-using-the-in-operator/">More concise coding: using the &#8220;in&#8221; operator</a> appeared first on <a href="http://www.aparajitaworld.com">Aparajita’s World</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.aparajitaworld.com/2006/05/more-concise-coding-using-the-in-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
