in reply to Challenge: Perl 5: lazy sameFringe()?

TIMTOWTDI

NOTE that this example iterates over HoH not AoAs (which is a bit silly b/c order matters) since I have only 5.10 installed.

With >=5.12 each should also work with ARRAYs! (testing appreciated)

It might not be the most elegant solution but

a) it's really lazy - not only for the programmer - and

b) it can iterate nested Arrays and Hashes alike for >=5.12.

c) it's quite fast.

d) I can tell the recursion "depth" of each iteration. NOTE I'm only returning 3 values for better visualization/debugging. Only $v matters for this task.

use Data::Dump 'dd'; use strict; use warnings; dd my $h1 = { a => {b => {c => 1}, d=> 2}, e=>3,f=>4 }; dd my $h2 = { a => {b => {c => 1}, d=> 2}, e=>3,f=>4 }; sub gen { my $ref=shift; my @path=(); return sub { while (1) { while (my ($k,$v) = each %$ref) { if ( ref $v eq "HASH") { push @path,$ref; $ref =$v; next; } else { return $k,$v,scalar @path; } } return unless $ref = pop @path; } } } my $iter1=gen($h1); my $iter2=gen($h2); while ((my ($k1,$v1,$l1) = $iter1->()) + (my ($k2,$v2,$l2) = $iter2->( +)) ) { die "error $v1 != $v2" if $v1 ne $v2; print "$l1: $k1=> $v1\n"; }

out

/usr/bin/perl -w /tmp/lanx_fringe.pl { a => { b => { c => 1 }, d => 2 }, e => 3, f => 4 } { a => { b => { c => 1 }, d => 2 }, e => 3, f => 4 } 0: e=> 3 2: c=> 1 1: d=> 2 0: f=> 4
a more functional solution on Monday.

Cheers Rolf

( addicted to the Perl Programming Language)

UPDATE

I just realized that it's about binary trees and that the representation of them is free.

So when choosing nested hashes with keys L and R , this algorithm already delivers the needed features. =)

UPDATE

Limitations see Re^3: Challenge: Perl 5: lazy sameFringe()?

Replies are listed 'Best First'.
Re^2: Challenge: Perl 5: lazy sameFringe()?
by BrowserUk (Patriarch) on Jul 01, 2013 at 05:36 UTC
    UPDATE I just realized that it's about binary trees and that the representation of them is free. So when choosing nested hashes with keys L and R , this algorithm already delivers the needed features.

    Have you tested your routines using the same test data as used by the rosetta challenge? If not, you haven't even begun to enter into the spirit of the challenge.

    Which makes all your posts in this thread challenging the efficacy of other peoples attempts are at best misplaced noise; and at worst, mind-blowing arrogance and misunderstanding,.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      > Have you tested your routines using the same test data as used by the rosetta challenge?

      yes and it fails (only) when comparing identical hashes.

      This resulted into this thread , which I suppose you are well aware of.

      (Demonstrating the limitations of each is the most valuable outcome for me.)

      None of the solutions so far can compete with the elegance of the idiomatic P6's gather/take resp. Py's yield which is a bit frustrating ...

      Having specialized solutions which only work for binary AoAs is no big compensation...

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        (Demonstrating the limitations of each is the most valuable outcome for me.)

        Really. That limitation is already well documented:

        "There is a single iterator for each hash, shared by all each, keys, and values function calls in the program; it can be reset by reading all the elements from the hash, or by evaluating keys HASH or values HASH. If you add or delete elements of a hash while you're iterating over it, you may get entries skipped or duplicated, so don't."

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.