http://qs1969.pair.com?node_id=1110309


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) ?
class 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
With usage
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]