Philip R Brenan
and 1 contributors

Name

Math::Transform::List - Generate specified transformations of a list.

Synopsis

 use Math::Transform::List;

 transform {say "@_"} [qw(a b c)],   [1..3];

 #  a b c
 #  b c a
 #  c b a

 transform {say "@_"} [qw(a b c d)], [1..2], [3..4];

 #  a b c d
 #  b a c d
 #  a b d c
 #  b a d c

 transform {say "@_"} [qw(a b c d)], [[1, 3], [2, 4]];

 #  a b c d
 #  c d a b

Description

Generate and process all the all the transformations of a list using the standard Perl metaphor.

transform returns the number of transformations in both scalar and array context.

Please note that the order in which the transformations are generated is not guaranteed, so please do not rely on it.

The parameters to transform are:

1: The code to be executed for each transformation.

2: A reference to the list to be transformed. This list is transformed as specified by the transformations. Each transformation of the list is handed to the code supplied in parameter 1 to be processed.

3: One or more transformations to be applied to the list. The transformations are applied repeatedly in all orders until no new transformations of the list are generated. Each new transformation of the list is handed to the code supplied in parameter 1 for processing.

Transformations are represented as permutations in cyclic format based from 1 not 0. Two representations can be used to specify transformations.

3a: Single cycle.

  [1,2,3]

The first element of the list will be replaced by the second, the second by the third, and the third by the first.

3a: Multi cycle.

  [[1,3], [2,4]]

The first element of the list will be replaced by the third and vice versa, while simultaneously the second element is replaced by the fourth and vice versa.

  transform {say "@_"} [qw(a b c d)], [[1, 3], [2, 4]];

  #  a b c d
  #  c d a b

If you want to produce all possible transformations of a list you should consider Math::Permute::List as it is faster and easier to use than the equivalent:

  transform {} [1..$n], [1,2], [1..$n];

transform is fast and easy to use. It is written in 100% Pure Perl so it is is easy to read, install, use and modify.

Export

The transform function is exported.

Installation

Standard Module::Build process for building and installing modules:

  perl Build.PL
  ./Build
  ./Build test
  ./Build install

Acknowledgements

With much help and good natured advice from Philipp Rumpf to whom I am indebted.

Changes

2017.08.25 04:09:29 MM: hashes instead of sparse arrays

See Also

Math::Cartesian::Product
Math::Disarrange::List
Math::Permute::List
Math::Subsets::List

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2009-2017 Philip R Brenan

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.