diff options
author | Pascal Rigaux <pixel@mandriva.com> | 2003-01-06 21:54:43 +0000 |
---|---|---|
committer | Pascal Rigaux <pixel@mandriva.com> | 2003-01-06 21:54:43 +0000 |
commit | ee06ac214211f0809b9c11f9ff94c60745d401c4 (patch) | |
tree | 40dd65c6accedd616ead5b78df36aea9700f1f85 /MDK/Common | |
parent | 8254abd6ebbb6b763b8eefcb7c7f8becc954dd88 (diff) | |
download | perl-MDK-Common-ee06ac214211f0809b9c11f9ff94c60745d401c4.tar perl-MDK-Common-ee06ac214211f0809b9c11f9ff94c60745d401c4.tar.gz perl-MDK-Common-ee06ac214211f0809b9c11f9ff94c60745d401c4.tar.bz2 perl-MDK-Common-ee06ac214211f0809b9c11f9ff94c60745d401c4.tar.xz perl-MDK-Common-ee06ac214211f0809b9c11f9ff94c60745d401c4.zip |
MDK::Common::Func: add "find", "any" and "every"
Diffstat (limited to 'MDK/Common')
-rw-r--r-- | MDK/Common/Func.pm | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/MDK/Common/Func.pm b/MDK/Common/Func.pm index 8048da9..ab42513 100644 --- a/MDK/Common/Func.pm +++ b/MDK/Common/Func.pm @@ -53,6 +53,30 @@ mapn alike. The difference is what to do when the lists have not the same length: mapn takes the minimum common elements, mapn_ takes the maximum list length and extend the lists with undef values +=item find { CODE } LIST + +returns the first element where CODE returns true (or returns undef) + + find { /foo/ } "fo", "fob", "foobar", "foobir" + +gives "fob" + +=item any { CODE } LIST + +returns 1 if CODE returns true for an element in LIST (otherwise returns 0) + + any { /foo/ } "fo", "fob", "foobar", "foobir" + +gives 1 + +=item every { CODE } LIST + +returns 1 if CODE returns true for B<every> element in LIST (otherwise returns 0) + + every { /foo/ } "fo", "fob", "foobar", "foobir" + +gives 0 + =item map_index { CODE } LIST just like C<map>, but set C<$::i> to the current index in the list: @@ -79,7 +103,7 @@ gives (0, 2, 3) =item find_index { CODE } LIST -returns the index of the first element where CODE returns true +returns the index of the first element where CODE returns true (or throws an exception) find_index { /foo/ } "fo", "fob", "foobar", "foobir" @@ -149,7 +173,7 @@ use MDK::Common::Math; use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK); @ISA = qw(Exporter); -@EXPORT_OK = qw(may_apply if_ if__ fold_left mapn mapn_ map_index each_index grep_index find_index map_each grep_each partition before_leaving catch_cdie cdie); +@EXPORT_OK = qw(may_apply if_ if__ fold_left mapn mapn_ find any every map_index each_index grep_index find_index map_each grep_each partition before_leaving catch_cdie cdie); %EXPORT_TAGS = (all => [ @EXPORT_OK ]); @@ -193,6 +217,21 @@ sub mapn_(&@) { smapn($f, MDK::Common::Math::max(map { scalar @$_ } @_), @_); } +sub find(&@) { + my $f = shift; + $f->($_) and return $_ foreach @_; + undef +} +sub any(&@) { + my $f = shift; + $f->($_) and return 1 foreach @_; + 0; +} +sub every(&@) { + my $f = shift; + $f->($_) or return 0 foreach @_; + 1; +} sub map_index(&@) { my $f = shift; |