Skip to content

Commit 540ad7e

Browse files
committed
Add initial code, docs, and tests
1 parent dcff53d commit 540ad7e

12 files changed

+730
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## [Unreleased]
22

3-
## [0.1.0] - 2022-03-03
3+
## [0.1.0] - 2022-03-04
44

55
- Initial release

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Max Chernyak
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.

README.md

Lines changed: 140 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# EnumUtils
22

3-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/enum_utils`. To experiment with that code, run `bin/console` for an interactive prompt.
4-
5-
TODO: Delete this and the text above, and describe your gem
3+
Functions for mixing and matching lazy, potentially infinite enumerables.
64

75
## Installation
86

@@ -22,18 +20,152 @@ Or install it yourself as:
2220

2321
## Usage
2422

25-
TODO: Write usage instructions here
23+
### EnumUtils.sorted_intersection
24+
25+
Given N consistently-sorted enums, return unique values that appear in at least
26+
[degree] number of different enums, preserving the global order, until all enums
27+
are depleted. By default a value must appear in all of them.
28+
29+
```ruby
30+
# 3 ascending enums
31+
EnumUtils.sorted_intersection(
32+
[1,2].each,
33+
[2,3].each,
34+
[1,2,5].each
35+
).to_a # => [2]
36+
37+
# 3 descending enums
38+
EnumUtils.sorted_intersection(
39+
[2,1].each,
40+
[3,2].each,
41+
[5,2,1].each,
42+
compare: -> a, b { b <=> a } # reverse order comparison
43+
).to_a # => [2]
44+
45+
# 3 ascending enums with degree 2
46+
EnumUtils.sorted_intersection(
47+
[1,2].each,
48+
[2,3].each,
49+
[1,2,5].each,
50+
degree: 2
51+
).to_a # => [1, 2]
52+
```
53+
54+
### EnumUtils.sorted_union
55+
56+
Given N consistently-sorted enums, return all their unique values while
57+
preserving their global order. If `with_index: true` is given, also return index
58+
of the enum in which the corresponding value was first seen.
59+
60+
```ruby
61+
# 3 ascending enums
62+
EnumUtils.sorted_union(
63+
[1,2].each,
64+
[2,3].each,
65+
[1,2,5].each
66+
).to_a # => [1,2,3,5]
67+
68+
# 3 descending enums
69+
EnumUtils.sorted_union(
70+
[2,1].each,
71+
[3,2].each,
72+
[5,2,1].each,
73+
compare: -> a, b { b <=> a } # reverse order comparison
74+
).to_a # => [5,3,2,1]
75+
76+
# 3 ascending enums with index
77+
EnumUtils.sorted_union(
78+
[1,2].each,
79+
[2,3].each,
80+
[1,2,5].each,
81+
with_index: true
82+
).to_a # => [[1,0], [2,0], [3,1], [5,2]]
83+
```
84+
85+
### EnumUtils.sorted_merge
86+
87+
Given N consistently-sorted enums, return all their values while preserving the
88+
global order. If `with_index: true` is given, also return index in the enum that
89+
originated the value.
90+
91+
```ruby
92+
## 3 ascending enums
93+
EnumUtils.sorted_merge(
94+
[1,2].each,
95+
[2,3].each,
96+
[1,2,5].each
97+
).to_a # => [1,1,2,2,2,3,5]
98+
99+
## 3 descending enums
100+
EnumUtils.sorted_merge(
101+
[2,1].each,
102+
[3,2].each,
103+
[5,2,1].each,
104+
compare: -> a, b { b <=> a } # reverse order comparison
105+
).to_a # => [5,3,2,2,2,1,1]
106+
107+
## 3 ascending enums with index
108+
EnumUtils.sorted_union(
109+
[1,2].each,
110+
[2,3].each,
111+
[1,2,5].each,
112+
with_index: true
113+
).to_a # => [[1,0], [1,2], [2,0], [2,1], [2,2], [3,1], [5,2]]
114+
```
115+
116+
### EnumUtils.round_robin
117+
118+
Given N enums, return all their values by taking one value from each enum in
119+
turn, until all are exhausted. If `with_index: true` is given, also return index
120+
of the enum that originated the value.
121+
122+
```ruby
123+
## 3 enums
124+
EnumUtils.round_robin(
125+
[3,2].each,
126+
[1,3].each,
127+
[5,3,4].each
128+
).to_a # => [3,1,5,2,3,3,4]
129+
130+
## 3 enums with index
131+
EnumUtils.round_robin(
132+
[3,2].each,
133+
[1,3].each,
134+
[5,3,4].each,
135+
with_index: true
136+
).to_a # => [[3,0], [1,1], [5,2], [2,0], [3,1], [3,2], [4,2]]
137+
```
138+
139+
### EnumUtils.concat
140+
141+
Given N enums, return a new enum which lazily exhausts every enum.
26142

27143
## Development
28144

29-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
145+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
146+
`rake test` to run the tests. You can also run `bin/console` for an interactive
147+
prompt that will allow you to experiment.
30148

31-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
149+
To install this gem onto your local machine, run `bundle exec rake install`. To
150+
release a new version, update the version number in `version.rb`, and then run
151+
`bundle exec rake release`, which will create a git tag for the version, push
152+
git commits and the created tag, and push the `.gem` file to
153+
[rubygems.org](https://rubygems.org).
32154

33155
## Contributing
34156

35-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/enum_utils. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/enum_utils/blob/master/CODE_OF_CONDUCT.md).
157+
Bug reports and pull requests are welcome on GitHub at
158+
https://github.com/maxim/enum_utils. This project is intended to be a safe,
159+
welcoming space for collaboration, and contributors are expected to adhere to
160+
the [code of
161+
conduct](https://github.com/maxim/enum_utils/blob/master/CODE_OF_CONDUCT.md).
162+
163+
## License
164+
165+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36166

37167
## Code of Conduct
38168

39-
Everyone interacting in the EnumUtils project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/enum_utils/blob/master/CODE_OF_CONDUCT.md).
169+
Everyone interacting in the EnumUtils project's codebases, issue trackers, chat
170+
rooms and mailing lists is expected to follow the [code of
171+
conduct](https://github.com/maxim/enum_utils/blob/master/CODE_OF_CONDUCT.md).

0 commit comments

Comments
 (0)