Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Higher order perl

3,479 views

Published on

Hokkaido.pm #4

  • mement とか strategy とかもクロージャを使うと簡単にかけるようになるものの、たいていメンテしにくくなるのでやめた方が無難でござる(経験的に)。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Higher order perl

  1. 1. 2011 2 19
  2. 2. 2011 2 19
  3. 3. 2011 2 19
  4. 4. 2011 2 19
  5. 5. 2011 2 19
  6. 6. 2011 2 19
  7. 7. 2011 2 19
  8. 8. 2011 2 19
  9. 9. 2011 2 19
  10. 10. 2011 2 19
  11. 11. 2011 2 19
  12. 12. 2011 2 19
  13. 13. 2011 2 19
  14. 14. 2011 2 19
  15. 15. 2011 2 19
  16. 16. 2011 2 19
  17. 17. 2011 2 19
  18. 18. 2011 2 19
  19. 19. 2011 2 19
  20. 20. 2011 2 19
  21. 21. 2011 2 19
  22. 22. 2011 2 19
  23. 23. 2011 2 19
  24. 24. 2011 2 19
  25. 25. 2011 2 19
  26. 26. 2011 2 19
  27. 27. 2011 2 19
  28. 28. 2011 2 19
  29. 29. 2011 2 19
  30. 30. 2011 2 19
  31. 31. 2011 2 19
  32. 32. 2011 2 19
  33. 33. 2011 2 19
  34. 34. use strict; # my $add = sub { $_[0] + $_[1] }; # $add->(3, 4); # => 7 use strict; sub add { $_[0] + $_[1]; } add(2,4);2011 2 19
  35. 35. use strict; my $c = 3 my $add = sub { $_[0] + $c}; $add->(4); # => 72011 2 19
  36. 36. use strict; my $c = 3 my $proc = sub { $c++ }; $proc->(); # 3 $proc->(); # 4 $proc->(); # 52011 2 19
  37. 37. use strict; sub add { $_[0] + $_[1]; } my $func = ¥&add; $func->(3, 4); # => 72011 2 19
  38. 38. use strict; # sub generate { my ($a, $b) = @_; sub { $a + $b }; } generate(3, 4)->(); # => 72011 2 19
  39. 39. use strict; use feature qw(say); # sub generate { my ($num) = @_; sub { $num++ }; } my $fun = generate(2); say $fun->(); # => 2 say $fun->(); # => 3 my $fun2 = generate(2); say $fun2->(); # => 2 say $fun2->(); # => 32011 2 19
  40. 40. 2011 2 19
  41. 41. 2011 2 19
  42. 42. use strict; sub each_line { my ($file, $proc) = @_; open my $fh “<$file” or return; while(my $line = <$fh>) { $proc->($line); } close($fh); } each_line(‘aaa.txt’, sub { print $_[0] } );2011 2 19
  43. 43. use strict; sub each_line (&@) { my ($proc, $file) = @_; open my $fh, "<$file" or return; while(my $line = <$fh>) { $proc->($line); } close($fh); } each_line { print $_[0]; } (lex.pl) ;2011 2 19
  44. 44. 2011 2 19
  45. 45. use strict; my $table = { ‘left’ => sub { print ‘left’ }, ‘right’ => sub { print ‘right’ }, }; sub dispatch { my ($mode) = @_; my $func = $table->{$mode}; $func && $func->(); } dispatch(‘left’);2011 2 19
  46. 46. 2011 2 19
  47. 47. use strict; use Log::Dispatch; my $logger = Log::Dispatch->new; sub generate_logger { my ($ip) = @_; sub { $logger->info(“$ip : $_[0]“) }; } $custom_logger = generate_logger (‘192.168.1.1’); $custom_logger->(“msssssg”) # => “192.168.1.1 : msssssssg”2011 2 19
  48. 48. sub patial_add { my ($a) = @_; sub { add( $a + $_[0] ) }; } sub add { $_[0] + $_[1]; } my $func = patial_add(2); $func->(5); # => 72011 2 19
  49. 49. 2011 2 19
  50. 50. use strict; package Collection; sub new { my $class = shift; bless {data => @_}, $class; } sub iter { my $self = shift; my $counter = 0; sub { $self->{data}[$counter++] }; } package main; my $iter = Collection->new(1, 2)->iter; say $iter->(); # => 1 say $iter->(); # => 22011 2 19
  51. 51. 2011 2 19
  52. 52. use strict; use feature qw(say); # 1 .. my $infinite_list = sub { my $c = 1; sub { $c++; } }; # 100 my $num_list = $infinite_list->(); for my $i (0..100) { say $num_list->(); }2011 2 19
  53. 53. 2011 2 19
  54. 54. 2011 2 19
  55. 55. my $var = 12; sub func { say $var; } sub test { my $var = 19; func(); } test(); # => 12 func(); # => 122011 2 19
  56. 56. local $var = 12; sub func { say $var; } sub test { local $var = 19; func(); } test(); # => 19 func(); # => 122011 2 19
  57. 57. 2011 2 19
  58. 58. 2011 2 19
  59. 59. 2011 2 19
  60. 60. 2011 2 19
  61. 61. 2011 2 19
  62. 62. 2011 2 19
  63. 63. 2011 2 19
  64. 64. 2011 2 19
  65. 65. 2011 2 19
  66. 66. 2011 2 19
  67. 67. 2011 2 19

×