Class: Hamster::Partitioner

Inherits:
Object
  • Object
show all
Defined in:
lib/hamster/list.rb

Overview

This class can divide a collection into 2 ‘List`s, one of items

for which the block returns true, and another for false

At the same time, it guarantees the block will only be called ONCE for each item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list, block) ⇒ Partitioner

Returns a new instance of Partitioner.



1428
1429
1430
# File 'lib/hamster/list.rb', line 1428

def initialize(list, block)
  @list, @block, @left, @right = list, block, [], []
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



1427
1428
1429
# File 'lib/hamster/list.rb', line 1427

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



1427
1428
1429
# File 'lib/hamster/list.rb', line 1427

def right
  @right
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


1440
1441
1442
# File 'lib/hamster/list.rb', line 1440

def done?
  @list.empty?
end

#next_itemObject



1432
1433
1434
1435
1436
1437
1438
# File 'lib/hamster/list.rb', line 1432

def next_item
  unless @list.empty?
    item = @list.head
    (@block.call(item) ? @left : @right) << item
    @list = @list.tail
  end
end