Created
August 6, 2012 17:27
-
-
Save swuecho/3276949 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
| #todo how to use multi? | |
| class Minstack { | |
| has @!stack; | |
| has @!min; | |
| method mpush($item) { | |
| push @.stack, $item; | |
| if @.min==0 or @.min[*-1] > $item { | |
| push @.min, $item; | |
| } | |
| } | |
| method mpop() { | |
| my $item=pop @.stack; | |
| if $item == @.min[*-1] { | |
| pop @.min; | |
| } | |
| return $item; | |
| } | |
| method mmin() { | |
| @.min[*-1]; | |
| } | |
| } | |
| my $min=Minstack.new; | |
| $min.mpush(5); | |
| $min.mpush(3); | |
| $min.mpush(10); | |
| say $min.stack; | |
| say $min.min; | |
| say $min.mmin(); | |
| say $min.mpop(); | |
| say $min.mmin(); | |
| =begin comment | |
| No such method 'stack' for invocant of type 'Minstack' | |
| in method mpush at Minstack02.pl:9 | |
| in block <anon> at Minstack02.pl:32 | |
| =end comment | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment