Home > Software engineering >  How to split a list in two over a condition
How to split a list in two over a condition

Time:06-22

Is there a grep-like function that splits a list into both the part that matches the expression, as grep does, and also its complement?

Of course it would be very simple to write such a function, but I'm interested to know if it already exists and would be more conveniently included from any of the List::Util-like modules.

CodePudding user response:

I found the answer browsing through List::MoreUtils right after posting the question (my apologies). It's part:

Partitions LIST based on the return value of BLOCK which denotes into which partition the current value is put. Returns a list of the partitions thusly created. Each partition created is a reference to an array.

my $i = 0;
my @part = part { $i   % 2 } "a".."h";   # Returns [qw( a c e g )], [qw( b d f h )]

CodePudding user response:

If you're in an environment that won't let you install modules, here's a version of List::MoreUtils::part that doesn't depend on anything:

#!/usr/bin/env perl
use warnings;
use strict;
use feature qw/say/;

sub part(&@) {
    my $f = shift;
    my @parts;
    for (@_) {
        my $bucket = $f->();
        push @{$parts[$bucket]}, $_;
    }
    return @parts;
}

my ($evens, $odds) = part { $_ % 2 } 1..10;
say "Even numbers: @$evens";
say " Odd numbers: @$odds";
  • Related