Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This falls somewhere between a meditation and a tutorial. It's about using the NestedLoops function from Algorithm::Loops, but it's also about a certain mental model I have found useful recently.

A normal set of nested loops is like the odometer on a car. The outermost loop is the leftmost digit, and the innermost loop is the rightmost digit. A normal odometer can only loop through the same set of digits on each dial, but in programming, each dial can have a completely different set of values.

Now imagine that each time one of the dials changes, all the dials to the right of it can be remade. For example, each dial could exclude any numbers that exist on any dial to its left. A whole set of dials like that would generate the permuations of a set of numbers. In any situation where you're generating a set of values, and you can build them member-by-member, you can probably use this odometer model. And NestedLoops makes it pretty easy. All you need to mess with is the first argument.

The first argument to NestedLoops is the set (arrayref) of dials it will iterate over. A dial can be either an arrayref or a coderef that, when executed, returns an arrayref. So you can generate dials dynamically. Furthermore, when the coderef is executed, @_ will contain the values that are currently on the dials to the left, so you can do the kind of filtering I talked about above. With a little bookkeeping, I was able to make an elegant derangement iterator the other day.

Today, find all paths of length n in a graph inspired yet another solution in the same vein. Simply keeping track of where you can go next, you can explore a graph. And for the OP in that thread, while you're filtering, you can check to see if the set of characters you're assembling is a prefix of any valid words, to save a whole lot of checking later.

So keep your eyes open for solutions like this. They are easy and efficient.

Caution: Contents may have been coded under pressure.

In reply to NestedLoops and the Odometer Model by Roy Johnson

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2018-01-17 15:45 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (201 votes). Check out past polls.