Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/asross/lazier_enumerator
Adds support for compact, flatten, and uniq to lazy enumerators in Ruby
https://github.com/asross/lazier_enumerator
Last synced: 10 days ago
JSON representation
Adds support for compact, flatten, and uniq to lazy enumerators in Ruby
- Host: GitHub
- URL: https://github.com/asross/lazier_enumerator
- Owner: asross
- Created: 2016-04-13T02:40:23.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-04-15T22:32:44.000Z (over 8 years ago)
- Last Synced: 2024-04-25T09:02:26.084Z (6 months ago)
- Language: Ruby
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Lazier Enumerators [![Build Status](https://api.travis-ci.org/asross/lazier_enumerator.svg)](https://travis-ci.org/asross/lazier_enumerator)
This library adds support for `uniq`, `flatten`, and `compact` to Ruby 2.0's
[`Enumerator::Lazy`](http://ruby-doc.org/core-2.0.0/Enumerator/Lazy.html).## Basic Usage
Instead of
```ruby
enumerable.flatten.compact.uniq.map(&:uppercase)
```which would loop through the `enumerable` multiple times, you can do
```ruby
require 'lazier_enumerator'enumerable.lazy.flatten.compact.uniq.map(&:uppercase).force
```after running
```sh
gem install lazier_enumerator
```which will do the same thing, but lazily, only loading one element of
`enumerable` at a time and only looping through it once.In practice, based on benchmarks of this library and `Enumerator::Lazy` itself,
for most enumerables lazy enumeration is actually ~2x slower than normal
enumeration -- so use this library with a grain of salt :)## If you want something even lazier (and slower)
You can require `lazier_enumerator/even_lazier`, which will define additional
methods on `Enumerable` to provide lazy support for a variety of operations --
but in practice they take ~10x longer than their industrious versions.```ruby
require 'lazier_enumerator/even_lazier'# pass procs or symbols, not blocks, to lmap/lselect/lreject
[1,-2,[3,nil,[4]]].lflatten.lcompact.luniq.lmap(:abs).lselect(:even?).each do |i|
puts i
end# => 2 4
```