Created
January 31, 2010 20:21
-
-
Save kidd/291222 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use List::Util qw(sum); | |
| sub foreachLine { | |
| my ($const, $finalize) = @_; | |
| while (<DATA>) { | |
| $const->($_); | |
| } | |
| $finalize->(); | |
| } | |
| { | |
| #closure | |
| my @keep; | |
| sub gen_fun { | |
| my $par = shift; | |
| my $k = \@keep; | |
| return sub { | |
| my $student = shift; # gets a line like "Raimon Grau 6.4" | |
| my ($qualif) = $student =~ m/ ([^ ]*)$/; | |
| push @$k, $student if ($qualif > $par); | |
| } | |
| } | |
| sub end_do { | |
| print "@keep\n"; | |
| my @a = map{ | |
| my ($qualif) = m/ ([^ ]*)$/; | |
| $qualif | |
| } @keep; | |
| print sum(@a)/@a , "\n"; | |
| } | |
| } | |
| my $a = gen_fun(shift); | |
| foreachLine($a , \&end_do); | |
| __DATA__ | |
| foo 6.3 | |
| bar 4.3 | |
| baz 8.3 | |
| quux 8.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment