README.md in scan_left-0.1.0 vs README.md in scan_left-0.2.0

- old
+ new

@@ -1,8 +1,14 @@ +# scan_left ![Tests](https://github.com/panorama-ed/scan_left/workflows/Tests/badge.svg) -# scan_left +[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/github/panorma-ed/scan_left) +[![Docs Coverage](http://inch-ci.org/github/panorama-ed/scan_left.png)](http://inch-ci.org/github/panorama-ed/scan_left) + +[![Gem Version](https://img.shields.io/gem/v/scan_left.svg)](https://rubygems.org/gems/scan_left) +[![Gem Downloads](https://img.shields.io/gem/dt/scan_left.svg)](https://rubygems.org/gems/scan_left) + A tiny Ruby gem to provide the `#scan_left` operation on any Ruby `Enumerable`. ## What does it do? @@ -30,34 +36,48 @@ which is a fold whose initial state is the first element of the series. The key differences between `#inject` and `#scan_left` are: - 1. Incremental results: `#scan_left` returns a series of results + 1. **Incremental results**: `#scan_left` returns a series of results after processing each element of the input series. `#inject` returns a single value, which equals the final result in the series returned by `#scan_left`. - 2. Laziness: `#scan_left` can preserve the laziness of the input + 2. **Laziness**: `#scan_left` can preserve the laziness of the input series. As each incremental result is read from the output series, the actual calculation is lazily performed against the input. `#inject` cannot be a a lazy operation in general, as its single result reflects a calculation across every element of the input series. ## Examples -``` -require 'scan_left' +```ruby +require "scan_left" +# For comparison, results from #inject are shown as well: + ScanLeft.new([]).scan_left(0) { |s, x| s + x } == [0] [].inject(0) { |s, x| s + x } == 0 ScanLeft.new([1]).scan_left(0) { |s, x| s + x } == [0, 1] [1].inject(0) { |s, x| s + x } == 1 ScanLeft.new([1, 2, 3]).scan_left(0) { |s, x| s + x } == [0, 1, 3, 6] [1, 2, 3].inject(0) { |s, x| s + x } == 6 + +# OPTIONAL: To avoid explicitly using the `ScanLeft` class, you may +# choose to use the provided refinement on Enumerable. +# +# This refinement adds a `#scan_left` method directly to Enumerable +# for a more concise syntax. + +using EnumerableWithScanleft + +[].scan_left(0) { |s, x| s + x } => [0] +[1].scan_left(0) { |s, x| s + x } => [0, 1] +[1, 2, 3].scan_left(0) { |s, x| s + x } => [0, 1, 3, 6] ``` ## Further Reading * https://en.wikipedia.org/wiki/Fold_(higher-order_function)