Is “map” a loop?

后端 未结 15 1969
自闭症患者
自闭症患者 2020-12-14 14:29

While answering this question, I came to realize that I was not sure whether Perl\'s map can be considered a loop or not?

On one hand, it quacks/walks l

15条回答
  •  抹茶落季
    2020-12-14 15:09

    Here is a definition of map as a recurrence:

    sub _map (&@) {
        my $f = shift;
    
        return unless @_;
    
        return $f->( local $_ = shift @_ ),
               _map( $f, @_ );
    }
    
    my @squares = _map { $_ ** 2 } 1..100;
    

提交回复
热议问题