What are some interesting uses of higher-order functions?

后端 未结 14 814
走了就别回头了
走了就别回头了 2021-01-30 00:55

I\'m currently doing a Functional Programming course and I\'m quite amused by the concept of higher-order functions and functions as first class citizens. However, I can\'t yet

14条回答
  •  自闭症患者
    2021-01-30 01:48

    It’s been mentioned that Javascript supports certain higher-order functions, including an essay from Joel Spolsky. Mark Jason Dominus wrote an entire book called Higher–Order Perl; the book’s source is available for free download in a variety of fine formats, include PDF.

    Ever since at least Perl 3, Perl has supported functionality more reminiscent of Lisp than of C, but it wasn’t until Perl 5 that full support for closures and all that follows from that was available. And ne of the first Perl 6 implementations was written in Haskell, which has had a lot of influence on how that language’s design has progressed.

    Examples of functional programming approaches in Perl show up in everyday programming, especially with map and grep:

    @ARGV    = map { /\.gz$/ ? "gzip -dc < $_ |" : $_ } @ARGV;
    
    @unempty = grep { defined && length } @many;
    

    Since sort also admits a closure, the map/sort/map pattern is super common:

    @txtfiles = map { $_->[1] }
                sort { 
                        $b->[0]  <=>     $a->[0]
                                  ||
                     lc $a->[1]  cmp  lc $b->[1]
                                  ||
                        $b->[1]  cmp     $a->[1]
                }
                map  { -s => $_ } 
                grep { -f && -T }
                glob("/etc/*");
    

    or

    @sorted_lines = map { $_->[0] }
                    sort {
                         $a->[4] <=> $b->[4] 
                                 ||
                        $a->[-1] cmp $b->[-1]
                                 ||
                         $a->[3] <=> $b->[3]
                                 ||
                         ...
                    }
                    map { [$_ => reverse split /:/] } @lines;
    

    The reduce function makes list hackery easy without looping:

    $sum = reduce { $a + $b } @numbers;
    
    $max = reduce { $a > $b ? $a : $b } $MININT, @numbers;
    

    There’s a lot more than this, but this is just a taste. Closures make it easy to create function generators, writing your own higher-order functions, not just using the builtins. In fact, one of the more common exception models,

    try {
       something();
    } catch {
       oh_drat();
    };
    

    is not a built-in. It is, however, almost trivially defined with try being a function that takes two arguments: a closure in the first arg and a function that takes a closure in the second one.

    Perl 5 doesn’t have have currying built-in, although there is a module for that. Perl 6, though, has currying and first-class continuations built right into it, plus a lot more.

提交回复
热议问题