You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+140-8Lines changed: 140 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# EnumUtils
2
2
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.
6
4
7
5
## Installation
8
6
@@ -22,18 +20,152 @@ Or install it yourself as:
22
20
23
21
## Usage
24
22
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
Given N enums, return a new enum which lazily exhausts every enum.
26
142
27
143
## Development
28
144
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.
30
148
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).
32
154
33
155
## Contributing
34
156
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
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
166
37
167
## Code of Conduct
38
168
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
0 commit comments