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
| { | |
| my %cache = {}; | |
| our sub long_running_thing( $x ) is export | |
| { # imagine your own long running method here | |
| return %cache{$x} if ( %cache.exists($x) ); | |
| my $result; | |
| # do some long running thing, generate result | |
| %cache{$x} = $result; |
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
| { | |
| my %cache = {}; | |
| sub long_running_thing { # imagine your own long running method here | |
| my $x = shift; | |
| return $cache{$x} if( exists $cache{$x} ); | |
| my $result; | |
| # do some long running thing, generate result | |
| return $result; | |
| } | |
| } |
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
| my $to_the_power_of = -> $x, $y { return $x ** $y} | |
| $to_the_power_of(2,3) |
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
| my $sub = { return 'hello world' } |
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
| my $sub = sub { return 11; }; | |
| $sub.() # returns 11 | |
| $sub() # also returns 11 |
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
| sub compound_interest( Real $present_value, Real $iterations, Real $rate where ( * > 1)) | |
| { | |
| return $present_value * ( 1 + $iterations ) ** $rate; | |
| } |
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
| sub compound_interest( Real $present_value, Real $iterations, Real $rate where ( * > 1)) | |
| { | |
| return $present_value * ( 1 + $iterations ) ** $rate; | |
| } |
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
| sub compound_interest( Real $present_value, Real $iterations, Real $rate) | |
| { | |
| return $present_value * ( 1 + $iterations ) ** $rate; | |
| } |
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
| sub compound_interest | |
| { | |
| my ($present_value, $iterations, $rate) = @_; | |
| return $present_value * ( 1 + $iterations ) ** $rate; | |
| } |
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
| 1..* # a list 1 to infinity | |
| @array[*] # all the elements in the list | |
| (*, *, $x) = (1,2,3) # $x = 3, 1 & 2 are discarded. This is similar to (undef, undef, $x) = (1,2,3) in Perl5 | |
| %hash{*} # all the values in the hash | |