Class: Hamster::Partitioner
- Inherits:
-
Object
- Object
- Hamster::Partitioner
- 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
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Instance Method Summary collapse
- #done? ⇒ Boolean
-
#initialize(list, block) ⇒ Partitioner
constructor
A new instance of Partitioner.
- #next_item ⇒ Object
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
#left ⇒ Object (readonly)
Returns the value of attribute left.
1427 1428 1429 |
# File 'lib/hamster/list.rb', line 1427 def left @left end |
#right ⇒ Object (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
1440 1441 1442 |
# File 'lib/hamster/list.rb', line 1440 def done? @list.empty? end |
#next_item ⇒ Object
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 |