Class: ScanLeft
- Inherits:
-
Object
- Object
- ScanLeft
- Defined in:
- lib/scan_left.rb,
lib/scan_left/version.rb
Overview
Wraps any Enumerable
to provide the #scan_left operation.
Please see README for details, examples, background, and further reading about this operation.
Note: if you’d prefer to use the #scan_left method directly on any Enumerable
instance, please see the optional refinement EnumerableWithScanLeft.
Constant Summary collapse
- VERSION =
"0.3.1"
Instance Attribute Summary collapse
-
#enumerable ⇒ Enumerable
readonly
Enumerable to transform via #scan_left.
Instance Method Summary collapse
-
#initialize(enumerable) ⇒ ScanLeft
constructor
A new instance of ScanLeft.
-
#scan_left(initial) {|memo, obj| ... } ⇒ Enumerable
Map the enumerable to the incremental state of a calculation by applying the given block, in turn, to each element and the previous state of the calculation, resulting in an enumerable of the state after each iteration.
Constructor Details
#initialize(enumerable) ⇒ ScanLeft
Returns a new instance of ScanLeft.
26 27 28 |
# File 'lib/scan_left.rb', line 26 def initialize(enumerable) @enumerable = enumerable end |
Instance Attribute Details
#enumerable ⇒ Enumerable (readonly)
Returns Enumerable to transform via #scan_left.
23 24 25 |
# File 'lib/scan_left.rb', line 23 def enumerable @enumerable end |
Instance Method Details
#scan_left(initial) {|memo, obj| ... } ⇒ Enumerable
Map the enumerable to the incremental state of a calculation by applying the given block, in turn, to each element and the previous state of the calculation, resulting in an enumerable of the state after each iteration.
47 48 49 50 51 |
# File 'lib/scan_left.rb', line 47 def scan_left(initial) memo = initial outs = enumerable.map { |*obj| memo = yield(memo, *obj) } prepend_preserving_laziness(value: initial, enum: outs) end |