|
|
|
|
|
|
|
|
|
|
|
|
|
|
Recursion: the good, the bad, and the alternatives (continued)
That's why the big search engines use the queue method, because if they see the work queue getting bigger, they can either add more resources to process the queue quicker, prioritise items in the queue, or even dump stuff they deem irrelevant. Another advantage is that you can track where you have been and not re-visit places that have already been processed.
This is pretty important in regular recursion too, if there is a risk of circular referencing. If you are recursively searching Domino Directory groups (and nested groups), then this would be a must, as there is every likelihood that someone has put circular references somewhere.
A work queue in Domino could be, for example, a global list variable or a set of documents. The advantages of the document scenario is that it some code could continue where it left off on the next run and you have a persistent record of where your code has been, assuming you don't delete entries from the queue, but merely mark them as having been processed. You can decide which is the best approach.
Here's an example we'll discuss over the next few paragraphs:
1 Sub PrintAscii(sToPrint)
2 QueueEmpty = False
3 Erase masterlist
4 masterlist(sToPrint)=sToPrint
5 While Not QueueEmpty
6 QueueEmpty=True
7 Forall x In masterlist
8 Print Asc(Left$(x,1))
9 If Len(x)>1 Then
10 masterlist(Mid$(x,2))=Mid$(x,2)
11 QueueEmpty = False
12 End If
13 Erase masterlist(x)
14 End Forall
15 Wend
16 End Sub
|
This example uses a work queue called "Masterlist" to maintain what is next on the list of activities, as it works through the queue. As it progresses through the queue, it itself adds entries to the queue. Once it has gotten through the queue that it first started with, it checks the queue again in case any further items where added. It repeats that until the queue is empty and stays empty.
In line 3, we initialise a LotusScript list variable, masterlist. This will be the queue or list of work to be done. In the Web searching example, this would hold a list of Web addresses to be searched. As we search an address, we may find more links to search, which we would add to our list of things to do, but not immediately try to deal with it, as we would with recursion. A LotusScript list datatype is like a dynamic array, but we don't need to redim to expand it. Line 3 sets up the initial queue item. Then, we set up a loop to make sure the queue is processed until empty.
We use a Forall in line 7, but this isn't strictly necessary if we maintained our work queue using a data structure where we could easily pick the first item.
Lines 9 to 12 deal with adding a new entry to the queue to cover the "rest" of the string.
Line 13 removes the item from the work queue, as it has been processed. Then we loop round until the queue is empty. This is a horribly trivial example, but I hope it illustrates the point.
Conclusion Recursion is a powerful technique but it has its limitations. These can be managed by careful programming and risks can be calculated to find out if there is a real risk of hitting them. There are also techniques to gain the effect, without the limitations. Alternatively, Java may be more appropriate for recursion that requires heavy depth.
Ian Murray is an independent Domino consultant of ten years, based in the UK. He has extensive experience of large scale infrastructure management, serving the most demanding of Domino applications. You can contact Ian on +44 7973 135939 or at ian.murray@dlcc-ltd.co.uk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Advertisement --
Find unused Lotus Notes groups and clean up your address book
Have you ever wanted to get rid of old Lotus Notes groups that were cluttering up your address book, but you weren't sure if they were used? Find Unused Groups can help.
Find Unused Groups will check your ACL, mail, multi purpose and server groups to help you determine if they are used, and who uses them.
Learn how to easily clean up your address book. |
-- Advertisement --
Mark your calendar for in-depth Lotus training, May 12-14, Boston
Join experts and peers May 12-14 in Boston for educational and networking events that deliver real-world Lotus training so you can increase productivity and efficiency in your company, advance your skills, and squeeze the most from your current environment. One registration gets you into THE VIEW's Admin2010 and Lotus Developer2010.
Register by April 10 to save $200. |
|
|
|
|
|
|
|
|
|
|