Class: Immutable::Splitter
- Inherits:
-
Object
- Object
- Immutable::Splitter
- Defined in:
- lib/immutable/list.rb
Overview
This class can divide a list up into 2 ‘List`s, one for the prefix of elements for which the block returns true, and another for all the elements after that. It guarantees that the block will only be called ONCE for each item
Defined Under Namespace
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) ⇒ Splitter
constructor
A new instance of Splitter.
- #next_item ⇒ Object
Constructor Details
#initialize(list, block) ⇒ Splitter
Returns a new instance of Splitter.
1486 1487 1488 |
# File 'lib/immutable/list.rb', line 1486 def initialize(list, block) @list, @block, @left, @right = list, block, [], EmptyList end |
Instance Attribute Details
#left ⇒ Object (readonly)
Returns the value of attribute left.
1485 1486 1487 |
# File 'lib/immutable/list.rb', line 1485 def left @left end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
1485 1486 1487 |
# File 'lib/immutable/list.rb', line 1485 def right @right end |
Instance Method Details
#done? ⇒ Boolean
1503 1504 1505 |
# File 'lib/immutable/list.rb', line 1503 def done? @list.empty? end |
#next_item ⇒ Object
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 |
# File 'lib/immutable/list.rb', line 1490 def next_item unless @list.empty? item = @list.head if @block.call(item) @left << item @list = @list.tail else @right = @list @list = EmptyList end end end |