<?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>Artful Code &#187; newlisp</title>
	<atom:link href="http://www.artfulcode.net/tags/newlisp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Newlisp modules</title>
		<link>http://www.artfulcode.net/articles/newlisp-modules/</link>
		<comments>http://www.artfulcode.net/articles/newlisp-modules/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 02:13:30 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Site news]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/newlisp-modules/</guid>
		<description><![CDATA[The newlisp modules, including web.lsp, are now being maintained actively by Kanen Flowers on github. Thank you, Kanen. I have not had much time to work on any personal projects between family and work, so I appreciate his stewardship tremendously. You can check out his site at ScruffyThinking.com. Submit article]]></description>
			<content:encoded><![CDATA[<p>The newlisp modules, including web.lsp, are now being maintained actively by Kanen Flowers on <a href="https://github.com/LifeZero/artful-newlisp">github</a>. Thank you, Kanen. I have not had much time to work on any personal projects between family and work, so I appreciate his stewardship tremendously. You can check out his site at <a href="http://www.scruffythinking.com"  alt="ScruffyThinking.com">ScruffyThinking.com</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F&amp;title=Newlisp+modules" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F&amp;title=Newlisp+modules" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Newlisp+modules&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F&amp;title=Newlisp+modules" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F&amp;title=Newlisp+modules" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F&amp;title=Newlisp+modules" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Newlisp+modules+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fnewlisp-modules%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/newlisp-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom session storage with newLISP Web</title>
		<link>http://www.artfulcode.net/articles/custom-session-storage-with-newlisp-web/</link>
		<comments>http://www.artfulcode.net/articles/custom-session-storage-with-newlisp-web/#comments</comments>
		<pubDate>Fri, 29 May 2009 18:35:52 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=684</guid>
		<description><![CDATA[The Web module's default session storage engine uses serialized contexts in the /tmp directory. The advantage of this method is that, apart from the directory (which is customizable), it is reasonably platform independent and has low overhead. The disadvantages of this are numerous. Files are stored unencrypted, so anyone with access to the server may view them. It is therefor advantageous to design a custom storage module for sessions.]]></description>
			<content:encoded><![CDATA[<p><a href="http://static.artfulcode.net/newlisp/web.lsp.html">The Web module&#8217;s</a> default session storage engine uses serialized contexts in the <em>/tmp</em> directory. The advantage of this method is that, apart from the directory (which is customizable), it is reasonably platform independent and has low overhead. The disadvantages of this are numerous. Files are stored unencrypted, so anyone with access to the server may view them. It is therefor advantageous to design a custom storage module for sessions.<span id="more-684"></span></p>
<p>Storing sessions in a database is a good solution. Backups are easier; persistence and pruning orphaned sessions are simpler. Security is available through the database server&#8217;s security.  We will use MySQL, since it is the best supported in newLISP.</p>
<h2>Setting up the database</h2>
<p>First, we need a table in which to store session data. Using a MySQL <code>TIMESTAMP</code> column will provide us with an automatically updating time stamp on the last modification of the session, which we will use below to find old sessions to remove.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> sessions <span style="color: #66cc66;">&#40;</span>
	sid VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">DATA</span> TEXT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	ts TIMESTAMP
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>Designing the storage module</h2>
<p>Several functions need to be defined to handle storing data. First, we must ensure that the <a href="http://www.newlisp.org/code/modules/mysql.lsp.html">MySQL module</a> is loaded and initialized. We will do this in a separate module that we will call <code>DBSessions</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">context</span> 'DBSessions<span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unless</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sym</span> <span style="color: #3AA43E;">&quot;MYSQL&quot;</span> 'MySQL <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; do not reinitialize if already loaded</span>
  <span style="color: #AF0500;">&#40;</span>module <span style="color: #3AA43E;">&quot;mysql5.lsp&quot;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; with newlisp 10.1, this will change to &quot;mysql.lsp&quot;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">init</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">connect</span> <span style="color: #3AA43E;">&quot;127.0.0.1&quot;</span> <span style="color: #3AA43E;">&quot;user&quot;</span> <span style="color: #3AA43E;">&quot;secret&quot;</span> <span style="color: #3AA43E;">&quot;somedb&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">context</span> '<span style="color: #2028B8;">MAIN</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Next, we define a function to create or resume a session from the database. Sessions are stored in memory as contexts, making our job easier. Our function will query MySQL to see if the session id exists, and either create a new session and insert it or retrieve an existing session and load it. The function <code>Web:session-id</code> gives us the current session id from the session cookie or creates a new one and sends the client a cookie to store it. <code>Web:session-context</code> returns a symbol pointed to the context storing the current session.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>open-session<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">when</span> <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;SELECT data FROM sessions WHERE sid = '%s'&quot;</span> <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">let</span> <span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>data <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">fetch-row</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
      <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if</span> data
        <span style="color: #808080; font-style: italic;">;; Evaluate the serialized context into the current session context</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">eval-string</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">first</span> data<span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #808080; font-style: italic;">;; Create a new session and save it</span>
        <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;INSERT INTO sessions (sid, data) VALUES ('%s', '%s')&quot;</span>
          <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
          <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">source</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Next, we need a function to write any changes to the session to storage. Since the session is stored as a context, this is quite simple.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>close-session<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;UPDATE sessions SET data = '%s' WHERE sid = '%s'&quot;</span>
    <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">source</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>We also need a function to delete entire sessions when necessary. This is also simple.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>delete-session<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;DELETE FROM sessions WHERE sid = '%s'&quot;</span>
    <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Finally, a function to cull old sessions from the database. For this, we use the variable <code>Web:SESSION_MAX_AGE</code> to determine which sessions have expired.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>clean-sessions<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;DELETE FROM sessions WHERE NOW() &gt;= DATE_ADD(ts, INTERVAL %d SECOND)&quot;</span>
    Web:<span style="color: #2028B8;">SESSION_MAX_AGE</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h2>Registering the handler in Web</h2>
<p>Last, the handler functions need to be registered in the <code>Web</code> module.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">define-session-handlers</span>
  open-session close-session delete-session clean-sessions<span style="color: #AF0500;">&#41;</span></pre></div></div>

<h2>Putting it all together</h2>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unless</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sym</span> <span style="color: #3AA43E;">&quot;MYSQL&quot;</span> 'MySQL <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; do not reinitialize if already loaded</span>
  <span style="color: #AF0500;">&#40;</span>module <span style="color: #3AA43E;">&quot;mysql5.lsp&quot;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; with newlisp 10.1, this will change to &quot;mysql.lsp&quot;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">init</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">connect</span> <span style="color: #3AA43E;">&quot;127.0.0.1&quot;</span> <span style="color: #3AA43E;">&quot;user&quot;</span> <span style="color: #3AA43E;">&quot;secret&quot;</span> <span style="color: #3AA43E;">&quot;somedb&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">context</span> 'DBSessions<span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>open-session<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">when</span> <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;SELECT data FROM sessions WHERE sid = '%s'&quot;</span> <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">let</span> <span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>data <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">fetch-row</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
      <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if</span> data
        <span style="color: #808080; font-style: italic;">;; Evaluate the serialized context into the current session context</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">eval-string</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">first</span> data<span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #808080; font-style: italic;">;; Create a new session and save it</span>
        <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;INSERT INTO sessions (sid, data) VALUES ('%s', '%s')&quot;</span>
          <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
          <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">source</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>close-session<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;UPDATE sessions SET data = '%s' WHERE sid = '%s'&quot;</span>
    <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">source</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>delete-session<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;DELETE FROM sessions WHERE sid = '%s'&quot;</span>
    <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">escape</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session-id</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>clean-sessions<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>MySQL:<span style="color: #2028B8;">query</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;DELETE FROM sessions WHERE NOW() &gt;= DATE_ADD(ts, INTERVAL %d SECOND)&quot;</span>
    Web:<span style="color: #2028B8;">SESSION_MAX_AGE</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">define-session-handlers</span>
  open-session close-session delete-session clean-sessions<span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">context</span> '<span style="color: #2028B8;">MAIN</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F&amp;title=Custom+session+storage+with+newLISP+Web" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F&amp;title=Custom+session+storage+with+newLISP+Web" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Custom+session+storage+with+newLISP+Web&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F&amp;title=Custom+session+storage+with+newLISP+Web" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F&amp;title=Custom+session+storage+with+newLISP+Web" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F&amp;title=Custom+session+storage+with+newLISP+Web" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Custom+session+storage+with+newLISP+Web+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcustom-session-storage-with-newlisp-web%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/custom-session-storage-with-newlisp-web/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A better newLISP web library</title>
		<link>http://www.artfulcode.net/articles/a-better-newlisp-web-library/</link>
		<comments>http://www.artfulcode.net/articles/a-better-newlisp-web-library/#comments</comments>
		<pubDate>Fri, 29 May 2009 13:32:29 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=648</guid>
		<description><![CDATA[One problem from which newLISP suffers is the lack of a really useful library for web-based applications. The <a href="http://newlisp.nfshost.com/code/modules/cgi.lsp.html">official CGI module</a> has serious enough problems to justify an entirely new library. After some thought, I decided moreover that the <a href="http://static.artfulcode.net/newlisp/request.lsp.html">request</a> and <a href="http://static.artfulcode.net/newlisp/response.lsp.html">response</a> modules that I designed were neither practical nor sufficient. To that end, I have designed a new, monolithic library to provide the essential functionality required for web programming]]></description>
			<content:encoded><![CDATA[<p>One problem from which newLISP suffers is the lack of a really useful library for web-based applications. The <a href="http://newlisp.nfshost.com/code/modules/cgi.lsp.html">official CGI module</a> has serious enough problems to justify an entirely new library. After some thought, I decided moreover that the <a href="http://static.artfulcode.net/newlisp/request.lsp.html">request</a> and <a href="http://static.artfulcode.net/newlisp/response.lsp.html">response</a> modules that I designed were neither practical nor sufficient. To that end, I have designed a new, monolithic library to provide the essential functionality required for web programming.<span id="more-648"></span></p>
<p>The official CGI module has some real issues. First, it combines POST and GET variables into a single structure. This has two serious consequences: 1) the application has no way to determine the method by which a parameter is passed (that information is completely lost), and 2) name clashes between GET and POST result in the loss of one or the other parameter (in the case of the CGI module, GET information would be overwritten.)</p>
<p>Another issue in the CGI module is with the <code>put-page</code> function, which breaks if &#8220;%&gt;&#8221; is used inside of a code island, even legitimately, such as in a string.</p>
<p><a href="http://static.artfulcode.net/newlisp/web.lsp.html">Web</a> fixes both of these problems and provides a number of other features, including:</p>
<ul>
<li>ASP/PHP-style templates</li>
<li>Getting/setting cookies, GET, and POST parameters</li>
<li>Entity encoding and decoding</li>
<li>HTTP header control</li>
<li>Sessions</li>
<li>Custom session storage</li>
<li>URL building and parsing</li>
<li>URL encoding and decoding</li>
<li>Query string building and parsing</li>
</ul>
<p>Additionally, Web does not suffer from the GET/POST issues that the CGI module does, nor does it mishandle tags inside of code.</p>
<h2>HTTP Headers</h2>
<p>Headers are set using <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_header"><code>Web:header</code></a>, which accepts two parameters &#8211; the header name and the header value. By default, one header is already set: <strong>Content-type: text/html</strong>. Headers are output using <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_send-headers"><code>Web:send-headers</code></a>, which is called before any other output. The convenience function <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_redir"><code>Web:redir</code></a> redirects the browser to the passed URL.</p>
<h2>GET and POST</h2>
<p>GET and POST variables are accessed using <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_get"><code>Web:get</code></a> and <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_post"><code>Web:post</code></a>, which may be called in two ways. When called with a single parameter, these functions act like <code>lookup</code> and return the GET/POST parameter with the named key. When called with no parameters, they return an association list of the corresponding query.</p>
<h2>Cookies</h2>
<p>Cookies must be set before headers are sent. They are set using the <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_cookie"><code>Web:cookie</code></a> function, which accepts up to six parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Set a basic cookie</span>
<span style="color: #AF0500;">&#40;</span>cookie <span style="color: #3AA43E;">&quot;foo&quot;</span> <span style="color: #3AA43E;">&quot;bar&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; Access a cookie value</span>
<span style="color: #AF0500;">&#40;</span>cookie <span style="color: #3AA43E;">&quot;foo&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; Delete a cookie by setting expires to now</span>
<span style="color: #AF0500;">&#40;</span>cookie <span style="color: #3AA43E;">&quot;foo&quot;</span> <span style="color: #2028B8;">nil</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">date-value</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; Set a cookie that expires in an hour</span>
<span style="color: #AF0500;">&#40;</span>cookie <span style="color: #3AA43E;">&quot;foo&quot;</span> <span style="color: #3AA43E;">&quot;bar&quot;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">date-value</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">*</span> <span style="color: #675400;">60</span> <span style="color: #675400;">60</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>See the <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_cookie">documentation</a> for a full list of accepted parameters.</p>
<h2>Sessions</h2>
<p>By default, sessions use file-based storage (located at /tmp). They are controlled with a few simple functions. The default <code>exit</code> function is wrapped to ensure that <code>Web:close-session</code> is called at the end of a script (at least, a script that calls <code>exit</code> at its end.)</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">open-session</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session</span> <span style="color: #3AA43E;">&quot;foo&quot;</span> <span style="color: #3AA43E;">&quot;bar&quot;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; store &quot;foo&quot; as &quot;bar&quot;</span>
<span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">send-headers</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; start output</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #3AA43E;">&quot;&lt;p&gt;&lt;strong&gt;foo is:&lt;/strong&gt; &quot;</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">session</span> <span style="color: #3AA43E;">&quot;foo&quot;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #3AA43E;">&quot;&lt;/p&gt;&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">exit</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>It is a simple matter to design and use custom storage handlers. The function <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_define-session-handlers"><code>Web:define-session-handlers</code></a> allows customization of which functions are called to begin/load, close/write, delete a session, and clear old sessions. See the <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_define-session-handlers">documentation</a> for a list of helpful functions and variables for custom storage handlers.</p>
<h2>Templates</h2>
<p>Templates work almost identically to the official CGI module, with a few differences. First, <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_eval-template"><code>Web:eval-template</code></a> is called with a string, rather than a file, to permit other storage methods and programmatic building of templates. Second, &lt;%= .. %&gt; may be substituted for the default opening tag as a shortcut for &lt;% (print &#8230; ) %&gt;. Last, the opening and closing tags may be customized by setting values of <code>Web:OPEN_TAG</code> and <code>Web:CLOSE_TAG</code>. The shortcut tag will always be <code>Web:OPEN_TAG</code> appended with and equal sign.</p>
<h2>Encoding and decoding</h2>
<p>There are several functions to make encoding, decoding, and escaping strings easier for dealing with URLs, javascript, and HTML entities.</p>
<p><a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_escape"><code>Web:escape</code></a> takes a string and encodes the basic HTML character entities (apostrophe, quote, ampersand, and left and right angle brackets.) <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_unescape"><code>Web:unescape</code></a> provides the reverse. The function <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_escape-js"><code>Web:escape-js</code></a> does no encoding of entities, but instead ensures that a string may be safely output in javascript string without causing syntax errors.</p>
<p><a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_encode-entities"><code>Web:encode-entities</code></a> encodes all HTML entities, including a number of entities that are not fully supported by all browser. The full list of entities is derived from <a href="http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references">Wikipedia</a>. Its reverse, <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_decode-entities"><code>Web:decode-entities</code></a>, translates entities back into their character equivalents.</p>
<p>The functions <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_url-encode"><code>Web:url-encode</code></a> and <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_url-decode"><code>Web:url-decode</code></a> deal with hex-encoding/decoding strings for use in URLs.</p>
<p>Query strings are easily created using <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_build-query"><code>Web:build-query</code></a>, modeled after the PHP function  <code>http_build_query</code>.  Its counterpart, <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_parse-query"><code>Web:parse-query</code></a>, takes a query string and turns it into an association list.</p>
<p>For URLs, it is simpler to use <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_build-url"><code>Web:build-url</code></a>, which takes a URL and any number of association lists which it uses to build a complete URL. Each alist may overwrite parameters from the previous, including any parameters on the passed URL.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">let</span> <span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>url <span style="color: #3AA43E;">&quot;http://www.artfulcode.net/?s=newlisp&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span>Web:<span style="color: #2028B8;">build-url</span> url '<span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;s&quot;</span> <span style="color: #3AA43E;">&quot;newlisp web module&quot;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;foo&quot;</span> <span style="color: #3AA43E;">&quot;bar&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; =&gt; http://www.artfulcode.net/?s=newlisp+web+module&amp;foo=bar</span></pre></div></div>

<p>It also has a counterpart, <a href="http://static.artfulcode.net/newlisp/web.lsp.html#Web_parse-url"><code>Web:parse-url</code></a>, which breaks a URL up into an association list of its component parts.</p>
<h2>Download</h2>
<p>You can find it in the <a href="http://static.artfulcode.net/newlisp/index.html">repository</a> or download it directly <a href="http://static.artfulcode.net/newlisp/web.lsp">here</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F&amp;title=A+better+newLISP+web+library" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F&amp;title=A+better+newLISP+web+library" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=A+better+newLISP+web+library&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F&amp;title=A+better+newLISP+web+library" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F&amp;title=A+better+newLISP+web+library" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F&amp;title=A+better+newLISP+web+library" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+A+better+newLISP+web+library+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-newlisp-web-library%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/a-better-newlisp-web-library/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Updated Json module for newLISP</title>
		<link>http://www.artfulcode.net/articles/updated-json-module-for-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/updated-json-module-for-newlisp/#comments</comments>
		<pubDate>Fri, 22 May 2009 18:10:01 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=643</guid>
		<description><![CDATA[Thanks to Andrew Pennebaker for pointing out where the decoder was broken. This update includes a complete rewrite of the JSON decoder. It is now slightly more forgiving and much more accurate. You can download it here or upgrade using nl-install. Submit article]]></description>
			<content:encoded><![CDATA[<p>Thanks to Andrew Pennebaker for pointing out where the decoder was broken. This update includes a complete rewrite of the JSON decoder. It is now slightly more forgiving and much more accurate. You can download it <a href="http://static.artfulcode.net/newlisp/json.lsp.html">here</a> or upgrade using <a href="http://static.artfulcode.net/nl-install/packages.lsp">nl-install</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Updated+Json+module+for+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Updated+Json+module+for+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/updated-json-module-for-newlisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A module manager for newlisp</title>
		<link>http://www.artfulcode.net/articles/a-module-manager-for-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/a-module-manager-for-newlisp/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 18:28:02 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=551</guid>
		<description><![CDATA[nl-install is a basic module manager for newlisp. With this first release, I have set up a repository for the Artful Code modules and included it in the default settings for the application.]]></description>
			<content:encoded><![CDATA[<p>nl-install is a simple module manager for newlisp. With this first release, I have set up a repository for the Artful Code modules and included it in the default settings for the application.<span id="more-551"></span></p>
<p>There are not a huge number of features at the moment, but here is the list:</p>
<ul>
<li>install modules from multiple repositories</li>
<li>install/uninstall module dependencies</li>
<li>automatic upgrade of modules</li>
<li>newlisp version checking</li>
<li>file clash checking</li>
</ul>
<h2>Basic setup</h2>
<p>Download the archive nl-install from <a href="http://static.artfulcode.net/nl-install/">http://static.artfulcode.net/nl-install/</a>. Extract and edit the nl-install file to set up your target installation directory (/usr/share/newlips/site-lisp by default) and any (future) extra repositories you would like to add. Make the file executable and place in your path, then update:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x nl-install
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> nl-install <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin
<span style="color: #c20cb9; font-weight: bold;">sudo</span> nl-install update</pre></div></div>

<h2>Usage</h2>
<p>To see the list of all available modules or just the ones installed:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">nl-install list available
nl-install list installed</pre></div></div>

<p>To install one or more modules:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> nl-install <span style="color: #c20cb9; font-weight: bold;">install</span> acode:csv
<span style="color: #c20cb9; font-weight: bold;">sudo</span> nl-install <span style="color: #c20cb9; font-weight: bold;">install</span> acode:matching acode:util</pre></div></div>

<p>If there are other modules that must be installed first, you will be prompted for their installation.</p>
<p>To uninstall a module:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> nl-install uninstall acode:csv</pre></div></div>

<p>Re-install a module to get the latest version.</p>
<h2>Repositories</h2>
<p>Setting up your own repository is easy. Just write a package listing and put it online somewhere. A sample is included in the tarball:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span>repository
  <span style="color: #AF0500;">&#40;</span>title <span style="color: #3AA43E;">&quot;My repository&quot;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>identifier <span style="color: #3AA43E;">&quot;foo&quot;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; once chosen, this should *not* change</span>
  <span style="color: #AF0500;">&#40;</span>contact <span style="color: #3AA43E;">&quot;My Name &quot;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>version <span style="color: #675400;">1000</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; repository version number (1.0)</span>
&nbsp;
  <span style="color: #AF0500;">&#40;</span>module
    <span style="color: #AF0500;">&#40;</span>title <span style="color: #3AA43E;">&quot;module1&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>description <span style="color: #3AA43E;">&quot;A short description of module1&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>url <span style="color: #3AA43E;">&quot;http://location/of/module1.lsp&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>qwerty <span style="color: #3AA43E;">&quot;http://location/of/module1.qwerty&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>version <span style="color: #675400;">1010</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; 1.1</span>
    <span style="color: #AF0500;">&#40;</span>newlisp-min-version <span style="color: #675400;">10000</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; 10</span>
    <span style="color: #AF0500;">&#40;</span>newlisp-max-version <span style="color: #675400;">10001</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; 10.0.1</span>
    <span style="color: #AF0500;">&#40;</span>depends <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; no dependencies</span>
&nbsp;
  <span style="color: #AF0500;">&#40;</span>module
    <span style="color: #AF0500;">&#40;</span>title <span style="color: #3AA43E;">&quot;module2&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>description <span style="color: #3AA43E;">&quot;A short description of module2&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>url <span style="color: #3AA43E;">&quot;http://location/of/module2.lsp&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>qwerty <span style="color: #3AA43E;">&quot;http://location/of/module2.qwerty&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span>version <span style="color: #675400;">2011</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; 2.1.1</span>
    <span style="color: #AF0500;">&#40;</span>newlisp-min-version <span style="color: #675400;">10000</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; 10</span>
    <span style="color: #AF0500;">&#40;</span>newlisp-max-version <span style="color: #675400;">10001</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; 10.0.1</span>
    <span style="color: #AF0500;">&#40;</span>depends <span style="color: #3AA43E;">&quot;foo:module1&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Let me know about it and I will put it in the repository list.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F&amp;title=A+module+manager+for+newlisp" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F&amp;title=A+module+manager+for+newlisp" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=A+module+manager+for+newlisp&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F&amp;title=A+module+manager+for+newlisp" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F&amp;title=A+module+manager+for+newlisp" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F&amp;title=A+module+manager+for+newlisp" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+A+module+manager+for+newlisp+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-module-manager-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/a-module-manager-for-newlisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiprocessing utilities for newLisp</title>
		<link>http://www.artfulcode.net/articles/multiprocessing-utilities-for-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/multiprocessing-utilities-for-newlisp/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 17:39:26 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=533</guid>
		<description><![CDATA[newLisp's Cilk API simplifies the task for forking new processes and retrieving the results of child processes' calculations a breeze. Some patterns remain complex, particularly when dealing with shared state or managing access to resources. To formalize some of the more common patterns of usage with the Cilk API, semaphores, and shared memory, I have written a multiprocessing library for newLisp.]]></description>
			<content:encoded><![CDATA[<p>newLisp&#8217;s Cilk API simplifies the task for forking new processes and retrieving the results of child processes&#8217; calculations a breeze. Some patterns remain complex, particularly when dealing with shared state or managing access to resources. To formalize some of the more common patterns of usage with the Cilk API, semaphores, and shared memory, I have written a multiprocessing library for newLisp.<span id="more-533"></span></p>
<p>The module came out of my original locks module, which contained only the Lock and RLock classes. The new library contains the following classes:</p>
<ul>
<li><strong>Semaphore:</strong> simplifies use of semaphores</li>
<li><strong>Shared:</strong> simplifies use of shared memory</li>
<li><strong>Synchronized:</strong> shared memory made safe for use between processes</li>
<li><strong>Lock:</strong> a binary semaphore, or mutex</li>
<li><strong>RLock:</strong> a recursive Lock</li>
<li><strong>Event:</strong> a simple mechanism to signal multiple processes</li>
<li><strong>Pipe:</strong> simplifies use of pipes</li>
<li><strong>Channel:</strong> a two-way communications channel made with pipes</li>
<li><strong>Queue:</strong> a synchronized first in, first out class</li>
</ul>
<p>MP additionally includes various utilities to deal with common tasks:</p>
<ul>
<li><strong>get-pid:</strong> gets the current processes&#8217; pid</li>
<li><strong>with-lock-held:</strong> a macro that evaluates its body while holding a lock</li>
<li><strong>wait:</strong> waits for a condition with variable length polling to reduce processor load</li>
<li><strong>map and iter:</strong> versions of map and dolist evaluated asynchronously using the Cilk API</li>
</ul>
<p>Perhaps the most useful function in the module is with-lock-held, which simplifies the most common locking case: assuring that a block of code is executed atomically.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> lock <span style="color: #AF0500;">&#40;</span>RLock<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span>with-lock-held lock
  <span style="color: #AF0500;">&#40;</span>access-restricted-resource<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>In this case, no more than one process can evaluate <code>(access-restricted-resource)</code> at a time.</p>
<p>You can download the <a href="http://static.artfulcode.net/newlisp/mp.lsp.html">MP module</a> from my <a href="http://static.artfulcode.net/newlisp/index.html">newLisp module repository</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F&amp;title=Multiprocessing+utilities+for+newLisp" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F&amp;title=Multiprocessing+utilities+for+newLisp" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Multiprocessing+utilities+for+newLisp&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F&amp;title=Multiprocessing+utilities+for+newLisp" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F&amp;title=Multiprocessing+utilities+for+newLisp" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F&amp;title=Multiprocessing+utilities+for+newLisp" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Multiprocessing+utilities+for+newLisp+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmultiprocessing-utilities-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/multiprocessing-utilities-for-newlisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Updating modules for newLISP 10</title>
		<link>http://www.artfulcode.net/articles/updating-modules-for-newlisp-10/</link>
		<comments>http://www.artfulcode.net/articles/updating-modules-for-newlisp-10/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 18:11:04 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Site news]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=509</guid>
		<description><![CDATA[I am in the process of updating my module repository for newlisp 10.  In the process, I am refactoring each module and doing some updates. I am paying shockingly little attention to backward compatibility, so if you want to use the new modules, be prepared to do update your own code. A couple of notable [...]]]></description>
			<content:encoded><![CDATA[<p>I am in the process of updating my module repository for newlisp 10.  In the process, I am refactoring each module and doing some updates. I am paying shockingly little attention to backward compatibility, so if you want to use the new modules, be prepared to do update your own code.</p>
<p>A couple of notable changes are a complete replacement of the mysql module, functional has been renamed to matching, and the addition of the Element module, which provides utilities to write XML (or any structured markup) quickly and efficiently.</p>
<p>As modules are updated, I am making a note of it in their descriptions.</p>
<p><a href="http://static.artfulcode.net/newlisp/">The repository </a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F&amp;title=Updating+modules+for+newLISP+10" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F&amp;title=Updating+modules+for+newLISP+10" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Updating+modules+for+newLISP+10&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F&amp;title=Updating+modules+for+newLISP+10" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F&amp;title=Updating+modules+for+newLISP+10" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F&amp;title=Updating+modules+for+newLISP+10" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Updating+modules+for+newLISP+10+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdating-modules-for-newlisp-10%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/updating-modules-for-newlisp-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A better MySQL module for newLISP</title>
		<link>http://www.artfulcode.net/articles/a-better-mysql-module-for-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/a-better-mysql-module-for-newlisp/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 17:23:32 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[foop]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=491</guid>
		<description><![CDATA[The Mysql module has been written from scratch utilizing some of the more recent features of newLisp, such as FOOP and reference returns. One of its major design goals was to simplify use as well as broaden the features of the standard MySQL module, while at the same time allowing the creation of new, anonymous [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong><code>Mysql</code></strong> module has been written from scratch utilizing some of the more recent features of newLisp, such as FOOP and reference returns. One of its major design goals was to simplify use as well as broaden the features of the standard <a href="http://www.newlisp.org/code/modules/mysql.lsp.html">MySQL module</a>, while at the same time allowing the creation of new, anonymous instances at run-time.<span id="more-491"></span></p>
<p>The <code>Mysql</code> module differs from the distribution standard module in several important ways. Most obviously, it uses <a href="http://www.artfulcode.net/articles/using-the-newlisp-ffi/">FOOP wrappers for MySQL types</a>. It also requires clients to free results instances; in the standard module, only the base MYSQL instance itself must be freed (using <code>MySQL:close-db</code>).</p>
<p>The significance of this is that it is much simpler to create multiple connections (without having to duplicate the entire context at compile time). Result sets are completely independent of each other, and several may be maintained in any state at once. This also means that a spawned process may be given its own Mysql instance to use without having to worry about other processes&#8217; instances interfering. Using the standard module, the entire context would need to be cloned at compile time and given a static symbol reference (e.g., <code>(new 'MySQL 'db)</code>) in order to run multiple instances or connections to a server.</p>
<p>Moreover, because this module uses <code>unpack</code> and MySQL C API accessor functions, there is no need for the client to calculate member offsets in MySQL compound types. So long as newLisp was compiled for the same target as the <code>libmysqlclient</code> library (both are 32 bit or both are 64 bit), everything should work out of the box. Additionally, MySQL errors are now checked in the connect and query functions and re-thrown as interpreter errors. Instead of checking for <code>nil</code> returns and a using MySQL:error to get the error message, standard error  handling with the <code>catch</code> function may be used.</p>
<p>Several convenience functions and macros have been defined in the <code>mysql</code> context for common operations, including connecting to the database and iterating over results without having to worry about catching errors and managing memory. SQL statements may be passed as strings or a list containing a format string and its parameters, which will type-checked and automatically be escaped as needed.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>mysql<span style="color: #66cc66;">:</span><span style="color: #555;">on-connect</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;localhost&quot;</span> <span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #ff0000;">&quot;secret&quot;</span> <span style="color: #ff0000;">&quot;my_database&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>db err<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> err
      <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Error! &quot;</span> err<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>mysql<span style="color: #66cc66;">:</span><span style="color: #555;">row-iter</span> db <span style="color: #ff0000;">&quot;SELECT * FROM some_table&quot;</span> true
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>row<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>println row<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></div>

<p>This module has been tested with MySQL version 5 and 5.1 and newLisp version 10.0.1. It requires newLisp 10.0 or later. You can <a href="http://static.artfulcode.net/newlisp/mysql.lsp.html">download it</a> from the <a href="http://static.artfulcode.net/newlisp/">Artful Code newLisp module repository</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F&amp;title=A+better+MySQL+module+for+newLISP" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F&amp;title=A+better+MySQL+module+for+newLISP" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=A+better+MySQL+module+for+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F&amp;title=A+better+MySQL+module+for+newLISP" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F&amp;title=A+better+MySQL+module+for+newLISP" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F&amp;title=A+better+MySQL+module+for+newLISP" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+A+better+MySQL+module+for+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fa-better-mysql-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/a-better-mysql-module-for-newlisp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using the newLISP FFI</title>
		<link>http://www.artfulcode.net/articles/using-the-newlisp-ffi/</link>
		<comments>http://www.artfulcode.net/articles/using-the-newlisp-ffi/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 15:44:03 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=469</guid>
		<description><![CDATA[One of newLisp&#8217;s out-of-the-box features is its foreign function interface. It is simple to use, requiring only a minimal knowledge of C and a willingness to read documentation. Importing foreign functions Loading a function from a shared library is done with the &#8216;import&#8217; function. Once imported, the function now exists as a normal function in [...]]]></description>
			<content:encoded><![CDATA[<p>One of newLisp&#8217;s out-of-the-box features is its foreign function interface. It is simple to use, requiring only a minimal knowledge of C and a willingness to read documentation.<span id="more-469"></span></p>
<h3>Importing foreign functions</h3>
<p>Loading a function from a shared library is done with the &#8216;import&#8217; function. Once imported, the function now exists as a normal function in the namespace in which is was imported.  From the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#import">newLisp documentation</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; import in Linux</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.so.6&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; import in Mac OS X</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.dylib&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; import in CYGWIN</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;cygwin1.dll&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>printf is now a function that may be called as any other. Because functions evaluate to themselves and import returns the function value, it is simple to bind the function to a different name:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> print-f <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.dylib&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h3>Calling foreign functions</h3>
<p>Foreign functions are called in the same was any other function.  They can be applied, curried, or mapped.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span>printf <span style="color: #3AA43E;">&quot;%g %s %d %c<span style="color: #546E99; font-weight: bold;">\n</span>&quot;</span> <span style="color: #675400;">1.23</span> <span style="color: #3AA43E;">&quot;hello&quot;</span> <span style="color: #675400;">999</span> <span style="color: #675400;">65</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #675400;">1.23</span> hello <span style="color: #675400;">999</span> A</pre></div></div>

<p>The values that the foreign function receives are pointers to the values that were passed to the function in newLisp. In most cases, this is not problematic, but it is something that must be kept in mind.</p>
<h3>Return values</h3>
<p>Often, a library function will not return a value directly. Often, functions return a pointer to a value or struct in memory. newLisp cannot guess at the size of the return value or its composition, so it is up to the programmer to know what kind of value is being returned.</p>
<p>For functions that return a pointer to a single value, the functions <a href="http://www.newlisp.org/downloads/newlisp_manual.html#get-int">get-int</a>, <a href="http://www.newlisp.org/downloads/newlisp_manual.html#get-char">get-char</a>, and <a href="http://www.newlisp.org/downloads/newlisp_manual.html#get-float">get-float</a> may be used to find the value to which the pointer points. It is important to check the return value of such a function, as passing an invalid value or null pointer to these functions may crash the interpreter.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>get-pointer-from-some-c-func<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> value <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-int</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Strings may be accessed using <a href="http://www.newlisp.org/downloads/newlisp_manual.html#and">get-string</a>, although functions that return string pointers often leave it to the consuming function to free the memory. To do so, the function &#8216;free&#8217; (which frees memory) must first be imported from libc:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.dylib&quot;</span> <span style="color: #3AA43E;">&quot;free&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>get-string-pointer<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> str <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-string</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; copy the c string to a newLisp string</span>
  <span style="color: #AF0500;">&#40;</span>free ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; free the string allocated by the foreign function</span></pre></div></div>

<h3>Deconstructing structs</h3>
<p>Functions in 3rd party libraries often return custom types that newLisp cannot introspect. In these cases, the programmer must tell newLisp how to unpack the values.</p>
<p>To that end, newLisp provides the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#pack">pack</a> and <a href="http://www.newlisp.org/downloads/newlisp_manual.html#unpack">unpack</a> functions. These can be used to pack newLisp values into a structure and vice versa. pack and unpack use a specially formated string that corresponds to the members of the struct.  Take the following struct as an example:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> foo <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> int_value<span style="color: #339933;">;</span>
  <span style="color: #993333;">double</span> float_value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It could be unpacked using the format, &#8220;lu lf&#8221; (see the <a href=" http://www.newlisp.org/downloads/newlisp_manual.html#pack">documentation for pack and unpack</a> for a full explanation of the format used). Unpacking with this format will return a list of the struct&#8217;s values.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unpack</span> <span style="color: #3AA43E;">&quot;lu lf&quot;</span> foo-instance<span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; returns (42 84.5)</span></pre></div></div>

<p>A function that accepts such a struct can be passed the return value of the pack function:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span>foofunction <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">pack</span> <span style="color: #3AA43E;">&quot;lu lf&quot;</span> <span style="color: #675400;">42</span> <span style="color: #675400;">84.5</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>This makes it simple to unpack complex types into newLisp values.</p>
<h3>Putting it all together</h3>
<p>It is helpful to create a base FOOP class to work with compound types. Specific types can be prototyped from this class, changing only the pack format to match.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> Pointer:<span style="color: #2028B8;">pack-</span><span style="color: #2028B8;">format</span> <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Pointer:<span style="color: #2028B8;">Pointer</span> addr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">list</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span> addr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Pointer:<span style="color: #2028B8;">pointer</span> inst<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>inst <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Pointer:<span style="color: #2028B8;">member</span> inst n , unpacked<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> unpacked <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unpack</span> Pointer:<span style="color: #2028B8;">pack-</span><span style="color: #2028B8;">format</span> <span style="color: #AF0500;">&#40;</span>:<span style="color: #2028B8;">pointer</span> inst<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>unpacked n<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>The Pointer class gives us a few useful methods. :pointer returns the pointer address and :member returns the nth member of the struct.  So, the foo class would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">new</span> 'Pointer 'Foo<span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> Foo:<span style="color: #2028B8;">pack-</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;lu lf&quot;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>It might also be helpful to add a couple of accessor functions:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Foo:<span style="color: #2028B8;">int-val</span> inst , ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>:<span style="color: #2028B8;">member</span> inst <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-int</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Foo:<span style="color: #2028B8;">float-val</span> inst , ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>:<span style="color: #2028B8;">member</span> inst <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-float</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Functions from a foreign library that work on &#8216;foo&#8217; types can now be added directly to the Foo class as methods, and passed the return value of (:pointer my-foo). This makes working with foreign types reasonably trivial.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Using+the+newLISP+FFI&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Using+the+newLISP+FFI+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/using-the-newlisp-ffi/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using newLISP&#8217;s find-all</title>
		<link>http://www.artfulcode.net/articles/using-newlisps-find-all/</link>
		<comments>http://www.artfulcode.net/articles/using-newlisps-find-all/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 15:29:47 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/using-newlisps-find-all/</guid>
		<description><![CDATA[newLISP&#8217;s find-all utility is exceptionally powerful, especially when coupled with rich matching functions like match, regex, and unify. find-all combines search and substitution into a fast, comprehensive function for extracting data from lists and strings. Basic syntax Like many newLISP sequence functions, find-all is defined for both strings and lists. The basic syntax is: Strings: [...]]]></description>
			<content:encoded><![CDATA[<p>newLISP&#8217;s <code>find-all</code> utility is exceptionally powerful, especially when coupled with rich matching functions like <code>match</code>, <code>regex</code>, and <code>unify</code>. <code>find-all</code> combines search and substitution into a fast, comprehensive function for extracting data from lists and strings.<span id="more-15"></span></p>
<h4>Basic syntax</h4>
<p>Like many newLISP sequence functions, <code>find-all</code> is defined for both strings and lists.  The basic syntax is:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;">Strings: <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> pattern target <span style="color: #AF0500;">&#91;</span>expression <span style="color: #AF0500;">&#91;</span>option<span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#41;</span>
Lists:   <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> pattern target <span style="color: #AF0500;">&#91;</span>expression <span style="color: #AF0500;">&#91;</span>comparison-function<span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#93;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h4>String searches and regular expressions</h4>
<p>The most basic string search finds occurrences of one string inside of another.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'str <span style="color: #3AA43E;">&quot;Now is the time for all good men to come to the aid of their country.&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;">&quot;the&quot;</span> str<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (&quot;the&quot; &quot;the&quot; &quot;the&quot;)</span></pre></div></div>

<p>There are three occurrences (including one within the word, &#8216;their&#8217;).  To find only occurrences of &#8216;the&#8217; as a complete word, a regular expression is used:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\bthe\b<span style="color: #AF0500;">&#125;</span></span> str <span style="color: #2028B8;">$0</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (&quot;the&quot; &quot;the&quot;)</span></pre></div></div>

<p>The first parameter is a regular expression, defined in curly braces to eliminate the need to double-escape entities (like b).  The final parameter, <code>0</code>, is the PCRE option (a full list of options is available in the newLISP documents for <a href="http://www.newlisp.org/downloads/newlisp_manual.html#regex">regex</a>.</p>
<p>The third parameter is key to one of the more powerful features of <code>find-all</code> &#8211; substitution.  The <code>expression</code> parameter is applied to each element found.  Inside of this expression, the global variable <code>$0</code> represents the entire matched element.  In a regular expression search, captured matches are available via subsequently enumerated variables: <code>$1</code>, <code>$2</code>, etc.</p>
<p>For example, to convert all occurrences of &#8216;the&#8217; to upper case in the resulting list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\bthe\b<span style="color: #AF0500;">&#125;</span></span> str <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">upper-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;(&quot;THE&quot; &quot;THE&quot;)</span></pre></div></div>

<p>Here is a short program to count the number of occurrences of some common words in the text of War and Peace:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'text <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">read-file</span> <span style="color: #3AA43E;">&quot;war_and_peace.txt&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b\S+\b<span style="color: #AF0500;">&#125;</span></span> text <span style="color: #2028B8;">$0</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; split into words</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%12s: %6d&quot;</span> <span style="color: #3AA43E;">&quot;Total words&quot;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">length</span> words<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">letn</span> <span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
       <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words words<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">dolist</span> <span style="color: #AF0500;">&#40;</span>word counts<span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%12s: %6d&quot;</span> <span style="color: #AF0500;">&#40;</span>word <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>word <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>With the results:</p>
<pre><code> Total words: 564345
         and:  21023
          or:   1542
         the:  31702
</code></pre>
<p>This isn&#8217;t necessarily efficient; finding all words and then counting occurrences of three specific words in a list of more than half a million elements results in a lot of wasted time.  The regular expression used also includes opening and closing punctuation in the word (i.e. a quote that begins &#8220;The&#8230;&#8221; would result in a missed occurrence of &#8216;the&#8217;, because the list would contain <em>&#8220;The</em> instead.  It also does not account for case when counting occurrences.</p>
<p>Here is a more efficient version:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'occurrences <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words occurrences<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((&quot;and&quot; 22267) (&quot;or&quot; 1582) (&quot;the&quot; 34619))</span></pre></div></div>

<p>Note that <code>find-all</code> is recursive and newLISP has a limited stack size, which can be set during execution with the -s switch:</p>
<pre><code>newlisp -s 500000 common_counts.lsp
</code></pre>
<p>The previous example would hit the default stack limit of 2,048.  This can be solved by using map instead of <code>find-all</code>&#8216;s substitution expression:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'occurrences <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">lower-case</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #2028B8;">$0</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">map</span> '<span style="color: #2028B8;">list</span> common-words <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">count</span> common-words occurrences<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((&quot;and&quot; 22267) (&quot;or&quot; 1582) (&quot;the&quot; 34619))</span></pre></div></div>

<p>The substitution expression is also useful to cause side effects, squeezing even more efficiency out of the algorithm.  Here, <code>find-all</code> increments a count of each word and stores it in a dictionary.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> counts:<span style="color: #2028B8;">counts</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'common-words '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;and&quot;</span> <span style="color: #3AA43E;">&quot;or&quot;</span> <span style="color: #3AA43E;">&quot;the&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 're <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;"><span style="color: #AF0500;">&#123;</span>\b<span style="color: #AF0500;">&#40;</span>%s<span style="color: #AF0500;">&#41;</span>\b<span style="color: #AF0500;">&#125;</span></span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">join</span> common-words <span style="color: #3AA43E;">&quot;|&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> re text <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">or</span> <span style="color: #AF0500;">&#40;</span>counts <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lower-case</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">dolist</span> <span style="color: #AF0500;">&#40;</span>w common-words<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">println</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;%4s: %6d&quot;</span> w <span style="color: #AF0500;">&#40;</span>counts w<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">; and:  22267</span>
 <span style="color: #808080; font-style: italic;">;  or:   1582</span>
 <span style="color: #808080; font-style: italic;">; the:  34619</span></pre></div></div>

<p>This algorithm is quite fast, although still somewhat weighty in RAM, since it stores the entire text before doing its work.</p>
<h4>List searches</h4>
<p><code>find-all</code> list searches are (arguably) nearly as powerful as regular expressions.  By default, <code>find-all</code> compares elements of target with pattern using <code>match</code>, and the default expression is the entire matched element (<code>$0</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'target '<span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>a <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>b <span style="color: #675400;">2</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>c <span style="color: #675400;">3</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span>d <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">?</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span> target<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((a 1) (b 2) (c 3))</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span>d <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> target<span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; ((d 4 5))</span></pre></div></div>

<p>Using the substitution expression, lists may be unified or matched further:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">?</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span> target <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unify</span> '<span style="color: #AF0500;">&#40;</span>Letter Number<span style="color: #AF0500;">&#41;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">; (((Letter a) (Number 1)) ((Letter b) (Number 2)) ((Letter c) (Number 3)))</span></pre></div></div>

<p>Simple list searches using other comparators than <code>match</code> are straight-forward:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #675400;">4</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">3</span> <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span> <span style="color: #2028B8;">$0</span> &amp;gt<span style="color: #808080; font-style: italic;">;)</span>
<span style="color: #808080; font-style: italic;">; (1 2 3)</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> <span style="color: #675400;">4</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">3</span> <span style="color: #675400;">4</span> <span style="color: #675400;">5</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> &amp;gt<span style="color: #808080; font-style: italic;">;)</span>
<span style="color: #808080; font-style: italic;">; (2 3 4)</span></pre></div></div>

<p><code>find-all</code> is convenient for searching XML.  newLISP parses XML into a tree.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">xml-type-tags</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span> <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'xml <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">xml-parse</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-url</span> <span style="color: #3AA43E;">&quot;http://www.weather.gov/xml/current_obs/index.xml&quot;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">+</span> <span style="color: #675400;">1</span> <span style="color: #675400;">2</span> <span style="color: #675400;">4</span> <span style="color: #675400;">16</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>For more info on parsing XML in newLISP, see <a href="http://www.artfulcode.net/articles/working-xml-newlisp/">this article</a> and the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#XML">newLISP documentation</a>.</p>
<p>The XML at the url above lists weather stations.  Here is an example station entry, under the root element, &#8220;wx_station_index&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>TAPA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>AG<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Vc Bird Intl Airport Antigua<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>17.117<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-61.783<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.noaa.gov/weather/current/TAPA.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/TAPA.rss<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/TAPA.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><code>assoc</code> is useful for finding a path in a parsed XML list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'stations-xml <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>However, <code>assoc</code> only returns the first element of the list that matches.  In the XML article linked above, <code>pop-assoc</code> was used to collect elements iteratively:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'stations '<span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">while</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span> <span style="color: #3AA43E;">&quot;station&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>That is a handy way of collecting all elements, especially in an irregular document.  With <code>find-all</code>, elements may be found just as easily and without modifying the original list:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>From there, it is just as simple to aggregate the values from each element:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'station-pattern
    '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station_id&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;state&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station_name&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;latitude&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;longitude&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;html_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;rss_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;xml_url&quot;</span> <span style="color: #2028B8;">?</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">match</span> station-pattern <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>This returns a list of the node values for each station.  The data can be applied to a different associative structure easily using <code>unify</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unify</span> '<span style="color: #AF0500;">&#40;</span>Id State <span style="color: #2028B8;">Name</span> Lat Lon Html Rss Xml<span style="color: #AF0500;">&#41;</span>
        <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">match</span> station-pattern <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Each element in this list this creates looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #AF0500;">&#40;</span>Id <span style="color: #3AA43E;">&quot;TAPA&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>State <span style="color: #3AA43E;">&quot;AG&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">Name</span> <span style="color: #3AA43E;">&quot;Vc Bird Intl Airport Antigua&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Lat <span style="color: #3AA43E;">&quot;17.117&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Lon <span style="color: #3AA43E;">&quot;-61.783&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Html <span style="color: #3AA43E;">&quot;http://weather.noaa.gov/weather/current/TAPA.html&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Rss <span style="color: #3AA43E;">&quot;http://weather.gov/xml/current_obs/TAPA.rss&quot;</span><span style="color: #AF0500;">&#41;</span>
 <span style="color: #AF0500;">&#40;</span>Xml <span style="color: #3AA43E;">&quot;http://weather.gov/xml/current_obs/TAPA.xml&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h4>A completely impractical example</h4>
<p>There are further implications of <code>find-all</code>&#8216;s ability to cause side effects in the substitution expression.  To download the XML for each individual station, something like this could be done (<em>don&#8217;t really do this &#8211; it will attempt to spawn more than 2,000 processes</em>):</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">set</span> 'processes
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">find-all</span> '<span style="color: #AF0500;">&#40;</span><span style="color: #3AA43E;">&quot;station&quot;</span> <span style="color: #2028B8;">*</span><span style="color: #AF0500;">&#41;</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">assoc</span> <span style="color: #AF0500;">&#40;</span>xml <span style="color: #3AA43E;">&quot;wx_station_index&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
      <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">spawn</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sym</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lookup</span> <span style="color: #3AA43E;">&quot;station_name&quot;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span> 'WEATHER<span style="color: #AF0500;">&#41;</span>
             <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-url</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">lookup</span> <span style="color: #3AA43E;">&quot;xml_url&quot;</span> <span style="color: #2028B8;">$0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">sync</span> <span style="color: #675400;">30000</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; wait 30 seconds for all downloads to finish</span></pre></div></div>

<p>Assuming that the application is able to download the XML file for more than 2,000 stations in 30 seconds (and that there is no hard limit to forked processes, as there is in OSX), the context <code>WEATHER</code> will contain the XML for all stations.</p>
<p>The infeasability of this example aside, it demonstrates, as an example, how easily a user-supplied XML file could be used to script a newLISP application.</p>
<h4>Further documentation</h4>
<ul>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#find-all">find-all</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#unify">unify</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#match">match</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#regex">regex</a></li>
<li> <a href="http://www.newlisp.org/downloads/newlisp_manual.html#xml-parse">xml-parse</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Using+newLISP%26%238217%3Bs+find-all&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F&amp;title=Using+newLISP%26%238217%3Bs+find-all" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Using+newLISP%26%238217%3Bs+find-all+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-newlisps-find-all%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/using-newlisps-find-all/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

