Easy. Change map_recursive() to not descend into recognized structures. That is probably the more sensible default behavior, agreed.

#!/usr/bin/perl -w use strict; sub map_recursive { my ($structure, $predicate) = @_; # don't descend into recognized structures if ($predicate->($structure)) { return $predicate->($structure); } # not recognized - pick apart my @result; if (UNIVERSAL::isa($structure, 'ARRAY')) { for (@{$structure}) { push @result, map_recursive($_, $predicate); } } elsif (UNIVERSAL::isa($structure, 'HASH')) { for (keys %{$structure}) { push @result, map_recursive($structure->{$_}, $predicate); } } else { push @result, $predicate->($structure); } return @result; } package Foo; sub new { bless {}, shift } sub get { $_[0]->{$_[1]} } sub set { $_[0]->{$_[1]} = $_[2] } package main; my @list = (Foo->new, Foo->new, Foo->new); $list[0]->set('a', 'b'); $list[1]->set('b', 'a'); $list[2]->set('x', 'y'); my $isa_foo = sub { UNIVERSAL::isa(shift, 'Foo') }; print "#1: ", join(',', map_recursive(\@list, $isa_foo)), "\n"; my @list2 = ( {a => 'm'}, { 1 => 55}, { Foo => 'Bar' }); my $h = sub { UNIVERSAL::isa(shift, 'HASH') }; print "#2: ", join(',', map_recursive (\@list2, $h)), "\n";

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com


In reply to Re: Re: Re: Type checker not checking out by clemburg
in thread Type checker not checking out by premchai21

Title:
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, details, 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, summary, 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.