develooper Front page | | Postings from September 2000

RFC 90 (v3) Arrays: Builtins: merge() and demerge()

Thread Next
Perl6 RFC Librarian
September 8, 2000 12:11
RFC 90 (v3) Arrays: Builtins: merge() and demerge()
Message ID:
This and other RFCs are available on the web at

=head1 TITLE

Arrays: Builtins: merge() and demerge()

=head1 VERSION

  Maintainer: Jeremy Howard <>
  Date: 10 August 2000
  Last Modified: 8 September 2000
  Mailing List:
  Number: 90
  Version: 3
  Status: Developing


It is proposed that two new functions, C<merge>, and C<demerge>, be added
to Perl. C<merge(@list1, @list2, ...)> would return a list that
interleaved its arguments. C<demerge($num_lists, @list)> would reverse
this operation. Both functions would return an alias into the original
list, not a copy of the elements.

=head1 CHANGES

=head2 Since v2

=over 4

=item *

Described aliasing behaviour of C<merge> and <demerge>

=head2 Since v1

=over 4

=item *

Moved list to

=item *

Changed name from zip/unzip

=item *

Pass lists directly in examples, not references

=item *

Change 2nd argument from $list_size to $num_lists



It is proposed that Perl implement a function called C<merge> that
interleaves the arguments of arrays together, and is evaluated lazily.
For instance:

  @a = (1,3,5);
  @b = (2,4,6);
  @merged_list = merge(@a,@b);   # (1,2,3,4,5,6)

This makes it easy to operate on multiple lists using flexible reduction

  $sum_xy = sub {reduce ^last+^x*^y, merge($_[0], $_[1])};
  print $sum_xy->(@a, @b);   # Prints '44', i.e. 1*2+3*4+5*6

In order to reverse this operation we need an C<demerge> function:

  @merged_list = merge(@a,@b);   # (1,2,3,4,5,6)
  @demerged_list = demerge(2, @merged_list);   # ([1,3,5], [2,4,6])

The second argument to C<demerge> is the number of lists that are to be
created (i.e. the number of lists that would have been C<merge>d for this
to reverse the operation).

If the list to be demerged is not an exact multiple of the partition size,
the final list references are not padded--their length is one less than
the list size. For example:

  @list = (1..7);
  @demerged_list2 = demerge(3, @list);   # ([1,4,7], [2,5], [3,6])

Both C<merge> and <demerge> do not make a copy of the elements of their
arguments; they simply create an alias to them:

  @a = (1,3,5);
  @b = (2,4,6);
  @merged_list = merge(@a,@b);   # (1,2,3,4,5,6)
  $merged_list[1] = 0;
  @b == (0,4,6);                 # True

The C<merge> and C<demerge> functions should be evaluated lazily.

C<merge> and C<demerge> return an alias into the original list, not a copy
of the elements.

Effectively, C<merge> creates an iterator over multiple lists. If used as
part of a reduction, the actual interleaved list need never be created.
For instance:

  $sum_xy = sub {reduce ^last+^x*^y, merge($_[0], $_[1])};
  $answer = $sum_xy->(@a, @b);
should be evaluated as if it read:

  $answer = 0;
  $answer += $a[$_] * $b[$_] for (0..$#a-1));
which does not need to create an intermediate list.


RFC 23: Higher order functions

RFC 76: Builtin: reduce

RFC 91: Builtin: partition

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About