NAME
Set::Functional - set operations for functional programming
VERSION
Version 0.01
SYNOPSIS
This module provides basic set operations for native lists. The primary goal is to take advantage of Perl's native functional programming capabilities while relying solely on Pure Perl constructs to perform the set operations as fast as possible. All of these techniques have been benchmarked against other common Perl idioms to determine the optimal solution. These benchmarks can be found in this package (shortly).
Each function is provided in two forms. The first form always expects simple flat data structures of defined elements. The second form expects a BLOCK to evaluate each member of the list to a defined value to determine how the element is a set member. These can be identified by the suffix "_by". None of these functions check definedness inline so as to eliminate the costly O(n) operation. All functions have been prototyped to give them a native Perl-ish look and feel.
Example usage:
use Set::Functional ':all';
my @deduped_numbers = setify(1 .. 10, 2 .. 11);
my @deduped_objects_by_name = setify_by { $_->{name} } ({name => 'fred'}, {name => 'bob'}, {name => 'fred'});
my @all_permutations = cartesian \@arr1, \@arr2, \@arr3, \@arr4;
my @only_arr1_elements = difference \@arr1, \@arr2, \@arr3, \@arr4;
my @only_arr1_elements_by_name = difference_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4;
my @unique_per_set = disjoint \@arr1, \@arr2, \@arr3, \@arr4;
my @unique_per_set_by_name = disjoint_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4;
my @unique_elements = distinct \@arr1, \@arr2, \@arr3, \@arr4;
my @unique_elements_by_name = distinct_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4;
my @shared_elements = intersection \@arr1, \@arr2, \@arr3, \@arr4;
my @shared_elements_by_name = intersection_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4;
my @odd_occuring_elements = symmetric_difference \@arr1, \@arr2, \@arr3, \@arr4;
my @odd_occuring_elements_by_name = symmetric_difference_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4;
my @all_elements = union \@arr1, \@arr2, \@arr3, \@arr4;
my @all_elements_by_name = union_by { $_->{name} } \@arr1, \@arr2, \@arr3, \@arr4;
EXPORT
A list of functions that can be exported. You can delete this section if you don't export anything, such as for a purely object-oriented module.
SUBROUTINES
setify(@)
Given a list, return a new set. Order is not guaranteed.
setify 1 .. 10, 6 .. 15 => 1 .. 15
setify_by(&@)
Given a choice function and a list, return a new set defined by the choice function. Order is not guaranteed.
cartesian(@)
Given multiple set references, return multiple sets containing all permutations of one element from each set. If the empty set is provided, the empty set is returned. If no sets are provided then none are returned.
cartesian [1 .. 3], [1 .. 2] => [1,1],[1,2],[2,1],[2,2],[3,1],[3,2]
difference(@)
Given multiple set references, return a new set with all the elements in the first set that don't exist in subsequent sets.
difference [1 .. 10], [6 .. 15] => 1 .. 5
difference_by(&@)
Given a choice function and multiple set references, return a new set with all the elements in the first set that don't exist in subsequent sets according to the choice function.
disjoint(@)
Given multiple set references, return corresponding sets containing all the elements from the original set that exist in any set exactly once.
disjoint [1 .. 10], [6 .. 15] => [1 .. 5], [11 .. 15]
disjoint_by(&@)
Given a choice function and multiple set references, return corresponding sets containing all the elements from the original set that exist in any set exactly once according to the choice function.
distinct(@)
Given multiple set references, return a new set containing all the elements that exist in any set exactly once.
distinct [1 .. 10], [6 .. 15] => 1 .. 5, 11 .. 15
distinct_by(&@)
Given a choice function and multiple set references, return a new set containing all the elements that exist in any set exactly once according to the choice function.
intersection(@)
Given multiple set references, return a new set containing all the elements that exist in all sets.
intersection [1 .. 10], [6 .. 15] => 6 .. 10
intersection_by(&@)
Given a choice function and multiple set references, return a new set containing all the elements that exist in all sets according to the choice function.
symmetric_difference(@)
Given multiple set references, return a new set containing all the elements that exist an odd number of times across all sets.
symmetric_difference [1 .. 10], [6 .. 15], [4, 8, 12] => 1 .. 5, 8, 11 .. 15
symmetric_difference_by(&@)
Given a choice function and multiple set references, return a new set containing all the elements that exist an odd number of times across all sets according to the choice function.
union(@)
Given multiple set references, return a new set containing all the elements that exist in any set.
union [1 .. 10], [6 .. 15] => 1 .. 15
union_by(&@)
Given a choice function and multiple set references, return a new set containing all the elements that exist in any set according to the choice function.
AUTHOR
Aaron Cohen, <aarondcohen at gmail.com>
BUGS
Please report any bugs or feature requests to bug-set-functional at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Set-Functional. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
TODO
Add benchmarking scripts as mentioned above.
Add validation functions: is_disjoint, is_empty, is_equal, is_null, is_pairwise_disjoint, is_proper_subset, is_proper_superset, is_subset, is_superset
Add SEE ALSO section
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Set::Functional
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
LICENSE AND COPYRIGHT
Copyright 2011 Aaron Cohen.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.