fibonacci_enumerator 0.1.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/.rubocop.yml +5 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +16 -0
- data/lib/fibonacci_enumerator/version.rb +6 -0
- data/lib/fibonacci_enumerator.rb +91 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c742f135126413ac534f27071379c351368ba40b9d7389469403545f22e6dd71
|
4
|
+
data.tar.gz: c651cd4a032831917fdaebf754c8e61dc3c03925f74587a0da4aabe6c0f37619
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad938b777156261eace3f66453a217dc6aa806d1ae6e96d7b275b8b9d49b2be3c0d73d58f77e94383536aa8dfd9a5d62a5e270c050ae5aa03790f9cdb24277b5
|
7
|
+
data.tar.gz: 7e4cd0b642db9890ba62d26f33ad659110708f64cfbcb982f24f817ab51926adb4789e3bdf2dc4062fadd791f796f5879a431883262fa2f0a4ce91fd696af24f
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Mateusz Drewniak
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Mateusz Drewniak
|
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,117 @@
|
|
1
|
+
# FibonacciEnumerator
|
2
|
+
|
3
|
+
This gems adds `FibonacciEnumerator`, an Enumerable class that makes it possible
|
4
|
+
to efficiently calculate elements of the Fibonacci sequence.
|
5
|
+
|
6
|
+
It is an Enumerable object so it can be iterated through using `each`
|
7
|
+
and implements methods like `first`, `select`, `map` etc.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add fibonacci_enumerator
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install fibonacci_enumerator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Elements of the Fibonacci sequence are calculated using the dynamic programming approach.
|
22
|
+
Instances of `FibonacciEnumerator` contain a cache that accumulates already calculated
|
23
|
+
elements of the sequence.
|
24
|
+
|
25
|
+
You can use the `FibonacciEnumerator` class directly as an `Enumerable` object for convenience.
|
26
|
+
|
27
|
+
### at
|
28
|
+
|
29
|
+
The `at` method aliases as `[]` can be used to get a particular element of the sequence.
|
30
|
+
|
31
|
+
```rb
|
32
|
+
FibonacciEnumerator.at(1) #=> 1
|
33
|
+
FibonacciEnumerator.at(2) #=> 1
|
34
|
+
FibonacciEnumerator.at(3) #=> 2
|
35
|
+
FibonacciEnumerator.at(4) #=> 3
|
36
|
+
FibonacciEnumerator.at(5) #=> 5
|
37
|
+
FibonacciEnumerator.at(6) #=> 8
|
38
|
+
|
39
|
+
FibonacciEnumerator[1] #=> 1
|
40
|
+
FibonacciEnumerator[2] #=> 1
|
41
|
+
FibonacciEnumerator[3] #=> 2
|
42
|
+
FibonacciEnumerator[4] #=> 3
|
43
|
+
FibonacciEnumerator[5] #=> 5
|
44
|
+
FibonacciEnumerator[6] #=> 8
|
45
|
+
```
|
46
|
+
|
47
|
+
You can also pass a `Range`.
|
48
|
+
|
49
|
+
```rb
|
50
|
+
FibonacciEnumerator.at(4..7) #=> [3, 5, 8, 13]
|
51
|
+
FibonacciEnumerator[4..7] #=> [3, 5, 8, 13]
|
52
|
+
```
|
53
|
+
|
54
|
+
### each
|
55
|
+
|
56
|
+
You can iterate through the Fibonacci series.
|
57
|
+
Keep in mind that it's infinite, so the loop will
|
58
|
+
go on forever unless manually interrupted
|
59
|
+
|
60
|
+
```rb
|
61
|
+
FibonacciEnumerator.each do |element|
|
62
|
+
puts element
|
63
|
+
end
|
64
|
+
# 1
|
65
|
+
# 1
|
66
|
+
# 2
|
67
|
+
# 3
|
68
|
+
# 8
|
69
|
+
# ...
|
70
|
+
```
|
71
|
+
|
72
|
+
### select
|
73
|
+
|
74
|
+
You can filter elements using `select` or `reject`
|
75
|
+
|
76
|
+
```rb
|
77
|
+
FibonacciEnumerator.first(25).select(&:even?)
|
78
|
+
#=> [0, 2, 8, 34, 144, 610, 2584, 10946, 46368]
|
79
|
+
```
|
80
|
+
|
81
|
+
This class is `Enumerable` so methods like `each`, `select`, `reject`, `take` etc
|
82
|
+
are available as well.
|
83
|
+
|
84
|
+
### Sharing the cache between multiple calculations
|
85
|
+
|
86
|
+
When you want to manually get particular elements of the Fibonacci sequence
|
87
|
+
you can save some time by sharing the cache between calculations.
|
88
|
+
|
89
|
+
You can do that by creating an instance of `FibonacciEnumerator`.
|
90
|
+
|
91
|
+
```rb
|
92
|
+
fib = FibonacciEnumerator.new
|
93
|
+
|
94
|
+
fib[10]
|
95
|
+
#=> 55
|
96
|
+
fib[10] # cache is reused, no calculations were necessary!
|
97
|
+
#=> 55
|
98
|
+
fib[15] # the first 10 elements were already calculated, so its faster
|
99
|
+
#=> 610
|
100
|
+
```
|
101
|
+
|
102
|
+
Instances are also `Enumerable` objects so methods like `each`, `select`, `reject`, `take` etc
|
103
|
+
are available as well.
|
104
|
+
|
105
|
+
## Development
|
106
|
+
|
107
|
+
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.
|
108
|
+
|
109
|
+
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).
|
110
|
+
|
111
|
+
## Contributing
|
112
|
+
|
113
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Verseth/ruby-fibonacci_enumerator.
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rubocop/rake_task'
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'fibonacci_enumerator/version'
|
4
|
+
|
5
|
+
# An Enumerable class that makes it possible
|
6
|
+
# to efficiently calculate elements of the Fibonacci sequence.
|
7
|
+
#
|
8
|
+
# It is an Enumerable object so it can be iterated through using `each`
|
9
|
+
# and implements methods like `first`, `select`, `map` etc.
|
10
|
+
class FibonacciEnumerator
|
11
|
+
include ::Enumerable
|
12
|
+
|
13
|
+
class << self
|
14
|
+
alias class_include? include?
|
15
|
+
|
16
|
+
include ::Enumerable
|
17
|
+
|
18
|
+
# @param index [Integer, Range]
|
19
|
+
def at(index)
|
20
|
+
new.at(index)
|
21
|
+
end
|
22
|
+
alias [] at
|
23
|
+
|
24
|
+
# @return [Enumerator, self]
|
25
|
+
# @yieldparam [Integer]
|
26
|
+
def each(&block)
|
27
|
+
new.each(&block)
|
28
|
+
end
|
29
|
+
|
30
|
+
alias include? class_include?
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_reader :cache
|
34
|
+
|
35
|
+
def initialize
|
36
|
+
reset_cache
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param index [Integer, Range]
|
40
|
+
# @return [Integer]
|
41
|
+
def at(index)
|
42
|
+
return at_int(index) if index.is_a?(::Integer)
|
43
|
+
|
44
|
+
at_range(index)
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [Enumerator, self]
|
48
|
+
# @yieldparam [Integer]
|
49
|
+
def each
|
50
|
+
return enum_for(:each) unless block_given?
|
51
|
+
|
52
|
+
(0..).each do |i|
|
53
|
+
yield at_int(i)
|
54
|
+
end
|
55
|
+
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [void]
|
60
|
+
def reset_cache
|
61
|
+
@cache = [0, 1, 1]
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# @param index [Integer]
|
67
|
+
# @return [Integer]
|
68
|
+
def at_int(index)
|
69
|
+
if (item = @cache[index])
|
70
|
+
return item
|
71
|
+
end
|
72
|
+
|
73
|
+
while index >= @cache.length # rubocop:disable Style/WhileUntilModifier
|
74
|
+
@cache[@cache.length] = @cache[@cache.length - 1] + @cache[@cache.length - 2]
|
75
|
+
end
|
76
|
+
|
77
|
+
@cache[index]
|
78
|
+
end
|
79
|
+
|
80
|
+
# @param range [Range]
|
81
|
+
# @return [Integer]
|
82
|
+
def at_range(range)
|
83
|
+
return @cache[range] if range.end < @cache.length
|
84
|
+
|
85
|
+
while range.end >= @cache.length # rubocop:disable Style/WhileUntilModifier
|
86
|
+
@cache[@cache.length] = @cache[@cache.length - 1] + @cache[@cache.length - 2]
|
87
|
+
end
|
88
|
+
|
89
|
+
@cache[range]
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fibonacci_enumerator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mateusz Drewniak
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-06-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A Ruby gem which adds a module that can efficiently calculate the elements
|
14
|
+
of the fibonacci sequence.
|
15
|
+
email:
|
16
|
+
- matmg24@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- LICENSE
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/fibonacci_enumerator.rb
|
28
|
+
- lib/fibonacci_enumerator/version.rb
|
29
|
+
homepage: https://github.com/Verseth/ruby-fibonacci_enumerator
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata:
|
33
|
+
homepage_uri: https://github.com/Verseth/ruby-fibonacci_enumerator
|
34
|
+
source_code_uri: https://github.com/Verseth/ruby-fibonacci_enumerator
|
35
|
+
rubygems_mfa_required: 'true'
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 2.7.0
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubygems_version: 3.4.14
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: A Ruby gem which adds a module that can efficiently calculate the elements
|
55
|
+
of the fibonacci sequence.
|
56
|
+
test_files: []
|