in reply to Re^4: The future of Perl?
in thread The future of Perl?
What would be the Moose version of this (a queue that maintains a fixed size by evicting the oldest items) ?
With usageclass FixedSizeQueue def initialize(size) @max_size = size @items = [] end def pop @items.shift end def push(item) @items.push item if @items.size > @max_size pop end end end
2:16% irb -r ./fsq.rb irb(main):001:0> q = FixedSizeQueue.new(3) => #<FixedSizeQueue:0x9317c6c @max_size=3, @items=[]> irb(main):002:0> q.push 2 => nil irb(main):003:0> q.push 3 => nil irb(main):004:0> q.push 5 => nil irb(main):005:0> q => #<FixedSizeQueue:0x9317c6c @max_size=3, @items=[2, 3, 5]> irb(main):006:0> q.push 7 => 2 irb(main):007:0> q => #<FixedSizeQueue:0x9317c6c @max_size=3, @items=[3, 5, 7]> irb(main):009:0> FixedSizeQueue.instance_methods(false) => [:pop, :push]
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: The future of Perl?
by Your Mother (Archbishop) on Dec 14, 2014 at 20:26 UTC | |
by Arunbear (Prior) on Dec 15, 2014 at 15:27 UTC | |
by tye (Sage) on Dec 16, 2014 at 05:58 UTC | |
by Your Mother (Archbishop) on Dec 15, 2014 at 22:55 UTC | |
by Arunbear (Prior) on Dec 16, 2014 at 18:25 UTC | |
by BrowserUk (Patriarch) on Dec 14, 2014 at 21:07 UTC | |
by Your Mother (Archbishop) on Dec 15, 2014 at 00:30 UTC | |
by BrowserUk (Patriarch) on Dec 15, 2014 at 03:28 UTC | |
by Your Mother (Archbishop) on Dec 15, 2014 at 06:11 UTC | |
| |
Re^6: The future of Perl?
by morgon (Priest) on Dec 14, 2014 at 18:44 UTC |
In Section
Meditations