Sidef

 view release on metacpan or  search on metacpan

lib/Sidef/Types/Array/Array.pm  view on Meta::CPAN

            }

            $table{$word} = $item;
        }

        Sidef::Types::Hash::Hash->new(\%table);
    }

    *abbreviations = \&abbrev;

    sub uniq_prefs {
        my ($self, $block) = @_;

        my $tail     = {};                # some unique value
        my $callback = defined($block);

        my %table;
        foreach my $sub_array (@$self) {
            my $ref = \%table;
            foreach my $item (@$sub_array) {
                $ref = $ref->{$item} //= {};

lib/Sidef/Types/Array/Array.pm  view on Meta::CPAN

                        $count++;
                    }
                }
            }
          }
          ->(\%table);

        bless \@abbrev;
    }

    *unique_prefixes = \&uniq_prefs;

    sub contains {
        my ($self, $obj, @extra) = @_;

        if (ref($obj) eq 'Sidef::Types::Block::Block') {
            foreach my $item (@$self) {
                if ($obj->run($item)) {
                    return (Sidef::Types::Bool::Bool::TRUE);
                }
            }

lib/Sidef/Types/Array/Array.pod  view on Meta::CPAN

Output:

    [1, 1, 2]
    [1, 2, 1]
    [2, 1, 1]

Aliases: I<unique_permutations>

=cut

=head2 uniq_prefs

    arr.uniq_prefs { ... }

Returns an array of unique prefixes based on the given block. Iterates through the array and includes an element only if the block result hasn't been seen before.

    say [1,2,2,3,3,3].uniq_prefs { _ }          #=> [1, 2, 3]
    say [1,-1,2,-2,3].uniq_prefs { .abs }       #=> [1, 2, 3]

Aliases: I<unique_prefixes>

=cut

=head2 unroll_op

    arr.unroll_op(operator, arg)

Applies the given operator between consecutive elements of the array, optionally with an initial argument.

scripts/RosettaCode/find_common_directory_path.sf  view on Meta::CPAN

#
## https://rosettacode.org/wiki/Find_common_directory_path#Sidef
#

var dirs = %w(
    /home/user1/tmp/coverage/test
    /home/user1/tmp/covert/operator
    /home/user1/tmp/coven/members
);

var unique_pref = dirs.map{.split('/')}.uniq_prefs.min_by{.len};
var common_dir  = [unique_pref, unique_pref.pop][0].join('/');
say common_dir;   # => /home/user1/tmp


assert_eq(common_dir, '/home/user1/tmp');



( run in 2.759 seconds using v1.01-cache-2.11-cpan-98e64b0badf )