Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'map' returns array instead of list #16044

Closed
p5pRT opened this issue Jun 25, 2017 · 11 comments
Closed

'map' returns array instead of list #16044

p5pRT opened this issue Jun 25, 2017 · 11 comments

Comments

@p5pRT
Copy link

p5pRT commented Jun 25, 2017

Migrated from rt.perl.org#131652 (status was 'resolved')

Searchable as RT131652$

@p5pRT
Copy link
Author

p5pRT commented Jun 25, 2017

From @KES777

Created by @KES777

The [DOC](https://perldoc.perl.org/functions/map.html) says​:

map ... evaluates ... and returns the list value

but it returns array​:

$x = ( 1, 7, 5 ); #5 list​: last element is returned
print $x;

@​array = ( 1, 7, 5 );
$x = @​array; #3 array​: number of elements is returned
print $x;

$x = map{ $_ } ( 1, 7, 5 ); #EXPECTED​: 5 (list), ACTUAL​: 3 (array)
print $x;

OUTPUT is​:
533

Perl Info

Flags:
    category=core
    severity=medium

Site configuration information for perl 5.24.1:

Configured by kes at Sun Apr 30 22:40:30 EEST 2017.

Summary of my perl5 (revision 5 version 24 subversion 1) configuration:
   
  Platform:
    osname=linux, osvers=4.4.0-53-generic, archname=x86_64-linux
    uname='linux kes-x751sa 4.4.0-53-generic #74-ubuntu smp fri dec 2 15:59:10 utc 2016 x86_64 x86_64 x86_64 gnulinux '
    config_args='-de -Dprefix=/home/kes/perl5/perlbrew/perls/perl-5.24.1 -Aeval:scriptdir=/home/kes/perl5/perlbrew/perls/perl-5.24.1/bin'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion='', gccversion='5.4.0 20160609', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678, doublekind=3
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16, longdblkind=3
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed /usr/include/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu /lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib
    libs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    libc=libc-2.23.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.23'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector-strong'

Locally applied patches:
    Devel::PatchPerl 1.38


@INC for perl 5.24.1:
    /home/kes/perl5/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1/x86_64-linux
    /home/kes/perl5/perlbrew/perls/perl-5.24.1/lib/site_perl/5.24.1
    /home/kes/perl5/perlbrew/perls/perl-5.24.1/lib/5.24.1/x86_64-linux
    /home/kes/perl5/perlbrew/perls/perl-5.24.1/lib/5.24.1


Environment for perl 5.24.1:
    HOME=/home/kes
    LANG=en_US.UTF-8
    LANGUAGE=en
    LC_ADDRESS=uk_UA.UTF-8
    LC_IDENTIFICATION=uk_UA.UTF-8
    LC_MEASUREMENT=uk_UA.UTF-8
    LC_MESSAGES=en_US.UTF-8
    LC_MONETARY=uk_UA.UTF-8
    LC_NAME=uk_UA.UTF-8
    LC_NUMERIC=uk_UA.UTF-8
    LC_PAPER=uk_UA.UTF-8
    LC_TELEPHONE=uk_UA.UTF-8
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/kes/perl5/perlbrew/bin:/home/kes/perl5/perlbrew/perls/perl-5.24.1/bin:/home/kes/bin:/home/kes/bin:/home/kes/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    PERLBREW=command perlbrew
    PERLBREW_BASHRC_VERSION=0.78
    PERLBREW_HOME=/home/kes/.perlbrew
    PERLBREW_MANPATH=/home/kes/perl5/perlbrew/perls/perl-5.24.1/man
    PERLBREW_PATH=/home/kes/perl5/perlbrew/bin:/home/kes/perl5/perlbrew/perls/perl-5.24.1/bin
    PERLBREW_PERL=perl-5.24.1
    PERLBREW_ROOT=/home/kes/perl5/perlbrew
    PERLBREW_VERSION=0.78
    PERL_BADLANG (unset)
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Jun 25, 2017

From Eirik-Berg.Hanssen@allverden.no

On Sun, Jun 25, 2017 at 2​:06 PM, KES <perlbug-followup@​perl.org> wrote​:

The [DOC](https://perldoc.perl.org/functions/map.html) says​:

map ... evaluates ... and returns the list value

  … and goes on to say "In scalar context, returns the total number of
elements so generated".

but it returns array​:

$x = ( 1, 7, 5 ); #5 list​: last element is returned
print $x;

  Nope; this is the comma operator in scalar context, returning the RHS
operand.
(Bugbear mantra​: There's no such thing as a list in scalar context – only
expressions are evaluated in different contexts.)

@​array = ( 1, 7, 5 );

$x = @​array; #3 array​: number of elements is returned
print $x;

$x = map{ $_ } ( 1, 7, 5 ); #EXPECTED​: 5 (list), ACTUAL​: 3 (array)
print $x;

  Since map in scalar context is documented to return the "total number of
elements so generated", 3 is to be expected.

  Works as per documentation – no bug, just my particular bugbear, the idea
of lists in scalar context.

  At most I might suggest making the documentation clearer that the return
value is context dependent, so that instead of the seemingly unconditional
"returns the list value composed of", we explicate the context​: "in list
context returns the value composed of".

Eirik

@p5pRT
Copy link
Author

p5pRT commented Jun 25, 2017

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jun 25, 2017

From @cpansprout

On Sun, 25 Jun 2017 05​:23​:34 -0700, Eirik-Berg.Hanssen@​allverden.no wrote​:

On Sun, Jun 25, 2017 at 2​:06 PM, KES <perlbug-followup@​perl.org> wrote​:

The [DOC](https://perldoc.perl.org/functions/map.html) says​:

map ... evaluates ... and returns the list value

… and goes on to say "In scalar context, returns the total number of
elements so generated".

but it returns array​:

$x = ( 1, 7, 5 ); #5 list​: last element is returned
print $x;

Nope; this is the comma operator in scalar context, returning the RHS
operand.
(Bugbear mantra​: There's no such thing as a list in scalar context – only
expressions are evaluated in different contexts.)

@​array = ( 1, 7, 5 );

$x = @​array; #3 array​: number of elements is returned
print $x;

$x = map{ $_ } ( 1, 7, 5 ); #EXPECTED​: 5 (list), ACTUAL​: 3 (array)
print $x;

Since map in scalar context is documented to return the "total number of
elements so generated", 3 is to be expected.

Works as per documentation – no bug, just my particular bugbear, the idea
of lists in scalar context.

At most I might suggest making the documentation clearer that the return
value is context dependent, so that instead of the seemingly unconditional
"returns the list value composed of", we explicate the context​: "in list
context returns the value composed of".

Or what about we eschew ‘value’ and say ‘a list composed of...’?

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Jul 8, 2017

From @jkeenan

On Sun, 25 Jun 2017 13​:08​:41 GMT, sprout wrote​:

On Sun, 25 Jun 2017 05​:23​:34 -0700, Eirik-Berg.Hanssen@​allverden.no
wrote​:

On Sun, Jun 25, 2017 at 2​:06 PM, KES <perlbug-followup@​perl.org>
wrote​:

The [DOC](https://perldoc.perl.org/functions/map.html) says​:

map ... evaluates ... and returns the list value

… and goes on to say "In scalar context, returns the total number of
elements so generated".

but it returns array​:

$x = ( 1, 7, 5 ); #5 list​: last element is returned
print $x;

Nope; this is the comma operator in scalar context, returning the RHS
operand.
(Bugbear mantra​: There's no such thing as a list in scalar context –
only
expressions are evaluated in different contexts.)

@​array = ( 1, 7, 5 );

$x = @​array; #3 array​: number of elements is
returned
print $x;

$x = map{ $_ } ( 1, 7, 5 ); #EXPECTED​: 5 (list), ACTUAL​: 3
(array)
print $x;

Since map in scalar context is documented to return the "total number
of
elements so generated", 3 is to be expected.

Works as per documentation – no bug, just my particular bugbear, the
idea
of lists in scalar context.

At most I might suggest making the documentation clearer that the
return
value is context dependent, so that instead of the seemingly
unconditional
"returns the list value composed of", we explicate the context​: "in
list
context returns the value composed of".

Or what about we eschew ‘value’ and say ‘a list composed of...’?

Please review the patch attached.

Thank you very much.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Jul 8, 2017

From @jkeenan

131652-0001-Improve-documentation-of-map.patch
From f6d32eb695407668bf1f7b5973029574a84b31f2 Mon Sep 17 00:00:00 2001
From: James E Keenan <jkeenan@cpan.org>
Date: Fri, 7 Jul 2017 19:59:47 -0400
Subject: [PATCH] Improve documentation of 'map'.

Per discussion in RT # 131652.
---
 pod/perlfunc.pod | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index b1e4ac2..dadb3d0 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -4071,12 +4071,11 @@ X<map>
 =for Pod::Functions apply a change to a list to get back a new list with the changes
 
 Evaluates the BLOCK or EXPR for each element of LIST (locally setting
-L<C<$_>|perlvar/$_> to each element) and returns the list value composed
-of the
-results of each such evaluation.  In scalar context, returns the
-total number of elements so generated.  Evaluates BLOCK or EXPR in
-list context, so each element of LIST may produce zero, one, or
-more elements in the returned value.
+L<C<$_>|perlvar/$_> to each element) and composes a list of the results of
+each such evaluation.  Each element of LIST may produce zero, one, or more
+elements in the generated list, so the number of elements in the generated
+list may differ from that in LIST.  In scalar context, returns the total
+number of elements so generated.  In list context, returns the generated list.
 
     my @chars = map(chr, @numbers);
 
-- 
2.7.4

@p5pRT
Copy link
Author

p5pRT commented Jul 8, 2017

From @cpansprout

On Fri, 07 Jul 2017 17​:02​:05 -0700, jkeenan wrote​:

Please review the patch attached.

+one

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Jul 11, 2017

From @jkeenan

On Sat, 08 Jul 2017 01​:28​:06 GMT, sprout wrote​:

On Fri, 07 Jul 2017 17​:02​:05 -0700, jkeenan wrote​:

Please review the patch attached.

+one

P5P​: Any further comments on this documentation patch?

I am taking this ticket for the purpose of applying the patch within 7 days unless there is a valid objection.

Thank you very much.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Jul 17, 2017

From @davidnicol

this is a fine example of how "array" and "list" appear to be used
interchangeably in the documentation.

that said I think the patch is great.

--
everything has to be just so or the magic won't work

@p5pRT
Copy link
Author

p5pRT commented Jul 17, 2017

From @jkeenan

On Tue, 11 Jul 2017 14​:50​:23 GMT, jkeenan wrote​:

On Sat, 08 Jul 2017 01​:28​:06 GMT, sprout wrote​:

On Fri, 07 Jul 2017 17​:02​:05 -0700, jkeenan wrote​:

Please review the patch attached.

+one

P5P​: Any further comments on this documentation patch?

I am taking this ticket for the purpose of applying the patch within 7
days unless there is a valid objection.

Thank you very much.

Applied to blead in commit e1d3549; marking ticket Resolved.

Thank you very much.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Jul 17, 2017

@jkeenan - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant