piecewise 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/piecewise.rb +22 -0
- data/piecewise.gemspec +25 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2d56687de0f3c643537d145b8e1c09c0af89da0a0d50089851c037cb91b84a5f
|
4
|
+
data.tar.gz: 771721745e8ad36269e317429131939b600d08c8076455aa81a6f5971bae5a37
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 68d63088a4a3aa87e082cac1e55914c299b85e8418b31892c32501dd00dcba5697760174eee16d66c08f8be84ed4d44167301f8432eba4ea6dcaf60173a3399d
|
7
|
+
data.tar.gz: 93f5511c15b8b7ef3fac4a49cdc717468d629565d3a196b6e54d3d056e2cbc5b540fa4fe33d08afcf4c039f2b57386101d1c73f67b6de7f481b6927789f1911f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Mike Pastore
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Piecewise Rubygem
|
2
|
+
|
3
|
+
Piecewise Enumerables, Enumerators, and Lazy Enumerators for Ruby.
|
4
|
+
|
5
|
+
Do you ever find yourself doing something like:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
even_keys = tuples.map { |k, v| k.upcase if v.even? }.compact
|
9
|
+
```
|
10
|
+
|
11
|
+
Or:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
even_keys = tuples.select { |_, v| v.even? }.keys.map(&:upcase)
|
15
|
+
```
|
16
|
+
|
17
|
+
I've always found this pattern a little bit off-putting, especially when I have
|
18
|
+
a big `do`..`end` block containing conditional logic and then a `.compact`
|
19
|
+
tacked on to it. (Maybe I'm picky.) And sometimes `nil` is a valid value, so
|
20
|
+
you have to introduce a new sentinel value to reject by. You can wrap the logic
|
21
|
+
in an `Enumerator.new { |yielder| .. }` or move the logic to a method that
|
22
|
+
returns an enumerator, but that can be a lot of boilerplate for a simple
|
23
|
+
filter+map operation.
|
24
|
+
|
25
|
+
This simple gem monkeypatches Enumerable, Enumerator, and Enumerator::Lazy to
|
26
|
+
add a `#piecewise` method that lets you do kind of an inline enumerator. The
|
27
|
+
above example can be rewritten like this:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
even_keys = tuples.piecewise { |yielder, (k, v)| yielder << k.upcase if v.even? }
|
31
|
+
```
|
32
|
+
|
33
|
+
I find this easier to read and grok. Perhaps you will too.
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
|
37
|
+
Add this line to your application's Gemfile:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
gem 'piecewise'
|
41
|
+
```
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
```sh
|
46
|
+
$ bundle
|
47
|
+
```
|
48
|
+
|
49
|
+
Or install it yourself as:
|
50
|
+
|
51
|
+
```sh
|
52
|
+
$ gem install piecewise
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
58
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
59
|
+
prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
62
|
+
release a new version, update the version number in `version.rb`, and then run
|
63
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
64
|
+
git commits and tags, and push the `.gem` file to
|
65
|
+
[rubygems.org](https://rubygems.org).
|
66
|
+
|
67
|
+
This gem was briefly named *chainenum* before being renamed to *piecewise*.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at
|
72
|
+
https://github.com/mwpastore/ruby-piecewise.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT
|
77
|
+
License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/piecewise.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
class Enumerator
|
3
|
+
def piecewise(*args)
|
4
|
+
self.class.new(*args) do |yielder|
|
5
|
+
each do |*values|
|
6
|
+
yield yielder, *values
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Lazy
|
12
|
+
def piecewise(*args, &block)
|
13
|
+
self.class.new(self, *args, &block)
|
14
|
+
end
|
15
|
+
end if const_defined?(:Lazy)
|
16
|
+
end
|
17
|
+
|
18
|
+
module Enumerable
|
19
|
+
def piecewise(*args, &block)
|
20
|
+
each(*args).piecewise(&block).to_a
|
21
|
+
end
|
22
|
+
end
|
data/piecewise.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'piecewise'
|
7
|
+
spec.version = '0.3.0'
|
8
|
+
spec.authors = ['Mike Pastore']
|
9
|
+
spec.email = ['mike@oobak.org']
|
10
|
+
|
11
|
+
spec.summary = 'Add #piecewise to enumerables, enumerators, and lazy enumerators'
|
12
|
+
spec.homepage = 'https://github.com/mwpastore/ruby-piecewise'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = %x{git ls-files -z}.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = %w[lib]
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
23
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
24
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: piecewise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Pastore
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- mike@oobak.org
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/piecewise.rb
|
71
|
+
- piecewise.gemspec
|
72
|
+
homepage: https://github.com/mwpastore/ruby-piecewise
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.7.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: 'Add #piecewise to enumerables, enumerators, and lazy enumerators'
|
96
|
+
test_files: []
|