numeric_memoist 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd7d14ad0102f36ee2d5cbdb5a267a9ceadf87f4
4
+ data.tar.gz: a912f90e0d6a0ccecab4939a7127f3e578d81ecb
5
+ SHA512:
6
+ metadata.gz: ed5e62405ae8f44f076efa4b2f17f473215bbaf2dba8027146d9af1961c2981672dd9ee75c92e61bfa184ba13fef8552bef10da74c82eb76cc89274f89044d11
7
+ data.tar.gz: cbdbacc3d056193f64bddb10f3b43c793241530ca12254a2d74cdbc56ae8e022eb35e8420614d74bd86282baac24517babdf9db476ef7fa773cbea098d6b212c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in numeric_memoist.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 GOTOH Shunsuke (@antimon2)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # NumericMemoist
2
+
3
+ The memoize library allows you to cache methods for Numerics (Integer/Float) in Ruby >= 2.0.0.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'numeric_memoist'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install numeric_memoist
18
+
19
+ ## Usage
20
+
21
+ Open class `Integer`/`Float`, extend `NumericMemoist` like the following:
22
+
23
+ require 'numeric_memoist'
24
+
25
+ class Integer
26
+ extend NumericMemoist
27
+
28
+ def sq
29
+ self ** 2
30
+ end
31
+ memoize :sq
32
+ end
33
+
34
+ Then Integer#sq will only be calcurated once.
35
+ (These work on Ruby >= 2.0.0, and also <= 1.9.x.)
36
+
37
+ ### Comparison to others
38
+
39
+ On Ruby <= 1.9.x, you may code as follows:
40
+
41
+ class Integer
42
+ def sq
43
+ @sq ||= self ** 2
44
+ # This raises RuntimeError on Ruby >= 2.0.x
45
+ end
46
+ end
47
+
48
+ This doesn't work on Ruby >= 2.0.0, since Integer is frozen.
49
+
50
+ You may use another similar gem like [Memoist](https://github.com/matthewrudy/memoist):
51
+
52
+ require 'memoist'
53
+
54
+ class Integer
55
+ extend Memoist
56
+
57
+ def sq
58
+ self ** 2
59
+ end
60
+ memoize :sq
61
+ # No Error raised, but momeize doesn't work on Ruby >= 2.0.x
62
+ end
63
+
64
+ This doesn't work on Ruby >= 2.0.0 for the same reason.
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
73
+
74
+ ## Acknowledgements
75
+
76
+ Inspired by [Memoist](https://github.com/matthewrudy/memoist).
77
+
78
+ ## License
79
+
80
+ Under the [MIT License](http://www.opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => [:test]
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ t.verbose = true
10
+ end
@@ -0,0 +1,52 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
2
+ require 'numeric_memoist'
3
+
4
+ class Integer
5
+ extend NumericMemoist
6
+
7
+ def isqrt
8
+ $stderr.puts "calculate sqrt(#{self}).to_i"
9
+ # === Newton-Raphson method ===
10
+ n = self
11
+ n = (n * n + self).div(2 * n) while n * n > self
12
+ n
13
+ end
14
+
15
+ def sq?
16
+ $stderr.puts "determining if #{self} is square number"
17
+ self.isqrt ** 2 == self
18
+ end
19
+ memoize :isqrt, :sq?
20
+ end
21
+
22
+ class Fixnum
23
+ # Need not to extend NumericMemoize (Integer has already extended NumericMemoize)
24
+ def isqrt
25
+ return super if self > 0x1fffffffffffff
26
+ $stderr.puts "calculate sqrt(#{self}).to_i (for Fixnum)"
27
+ return 0.0/0 if self < 0 # => NaN
28
+ return self if self < 2
29
+ Math.sqrt(self).to_i
30
+ end
31
+ memoize :isqrt
32
+ end
33
+
34
+ if $0 == __FILE__
35
+ p 123456789.isqrt
36
+ # STDERR> calculate sqrt(123456789).to_i (for Fixnum)
37
+ # STDOUT> 11111
38
+ p 123456789.sq?
39
+ # STDERR> determining if 123456789 is square number
40
+ # STDOUT> false
41
+ p 123456789.sq?
42
+ # (output nothig to STDERR)
43
+ # STDOUT> false
44
+ pi2 = 986960440108935861883449099987613301336842162813430234017512025
45
+ p pi2.sq?
46
+ # STDERR> determining if 986960440108935861883449099987613301336842162813430234017512025 is square number
47
+ # STDERR> calculate sqrt(986960440108935861883449099987613301336842162813430234017512025).to_i
48
+ # STDOUT> true
49
+ p pi2.isqrt
50
+ # (output nothig to STDERR)
51
+ # STDOUT> 31415926535897932384626433832795
52
+ end
@@ -0,0 +1,35 @@
1
+ require "numeric_memoist/version"
2
+
3
+ module NumericMemoist
4
+ def memoize *method_names
5
+ method_names.each do |method_name|
6
+ _memoize method_name
7
+ end
8
+ end
9
+
10
+ private
11
+ def _memoize method_name
12
+ klass = self.respond_to?(:class_eval) ? self : (class<<self;self;end)
13
+ klass.class_eval do
14
+ unmemoized_method = instance_method method_name
15
+ is_private = private_method_defined? method_name
16
+ is_protected = protected_method_defined? method_name
17
+ if unmemoized_method.arity.zero?
18
+ cache = Hash.new do |h, num|
19
+ h[num] = unmemoized_method.bind(num).call
20
+ end
21
+ define_method(method_name){cache[self]}
22
+ else
23
+ cache = Hash.new do |h, num|
24
+ method = unmemoized_method.bind num
25
+ h[num] = Hash.new do |h2, args|
26
+ h2[args] = method[*args]
27
+ end
28
+ end
29
+ define_method(method_name){|*args| cache[self][args]}
30
+ end
31
+ private method_name if is_private
32
+ protected method_name if is_protected
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module NumericMemoist
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'numeric_memoist/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "numeric_memoist"
8
+ spec.version = NumericMemoist::VERSION
9
+ spec.authors = ["antimon2"]
10
+ spec.email = ["antimon2.me@gmail.com"]
11
+ spec.description = %q{The memoize library allows you to cache methods for Numerics (Integer/Float) in Ruby >= 2.0.0.}
12
+ spec.summary = %q{Memoize methods in Integer/Float}
13
+ spec.homepage = %q{https://github.com/antimon2/numeric_memoist}
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ # spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.required_ruby_version = Gem::Requirement.new(">= 1.9.0")
25
+ end
@@ -0,0 +1,51 @@
1
+ require "test_helper"
2
+ require "numeric_memoist"
3
+ require "stringio"
4
+
5
+ class FloatMemoizeTest < Test::Unit::TestCase
6
+ class ::Float
7
+ extend NumericMemoist
8
+ end
9
+
10
+ def setup
11
+ @stdout_org = $stdout.dup
12
+ @stdout_buffer = ""
13
+ $stdout = StringIO.new @stdout_buffer
14
+ end
15
+
16
+ def test_no_args_method
17
+ ::Float.class_eval do
18
+ def no_args_method
19
+ puts "call #{self}.no_args_method"
20
+ self
21
+ end
22
+ memoize :no_args_method
23
+ end
24
+ assert_equal 1.0, 1.0.no_args_method
25
+ assert_equal ["call 1.0.no_args_method"], @stdout_buffer.chomp.split(/\r?\n/)
26
+ assert_equal 1.0, 1.0.no_args_method
27
+ assert_equal ["call 1.0.no_args_method"], @stdout_buffer.chomp.split(/\r?\n/)
28
+ end
29
+
30
+ def test_some_args_method
31
+ ::Float.class_eval do
32
+ def some_args_method a, b
33
+ puts "call #{self}.some_args_method(#{a}, #{b})"
34
+ self * a * b
35
+ end
36
+ memoize :some_args_method
37
+ end
38
+ assert_equal 6.0, 1.0.some_args_method(2.0, 3.0)
39
+ assert_equal ["call 1.0.some_args_method(2.0, 3.0)"], @stdout_buffer.chomp.split(/\r?\n/)
40
+ assert_equal 6.0, 1.0.some_args_method(3.0, 2.0)
41
+ assert_equal ["call 1.0.some_args_method(2.0, 3.0)", "call 1.0.some_args_method(3.0, 2.0)"], @stdout_buffer.chomp.split(/\r?\n/)
42
+ assert_equal 6.0, 1.0.some_args_method(2.0, 3.0)
43
+ assert_equal ["call 1.0.some_args_method(2.0, 3.0)", "call 1.0.some_args_method(3.0, 2.0)"], @stdout_buffer.chomp.split(/\r?\n/)
44
+ # assert_equal 6.0, 1.0.some_args_method(2, 3)
45
+ # assert_equal ["call 1.0.some_args_method(2.0, 3.0)", "call 1.some_args_method(3.0, 2.0)", "call 1.0.some_args_method(2, 3)"], @stdout_buffer.chomp.split(/\r?\n/)
46
+ end
47
+
48
+ def teardown
49
+ $stdout = @stdout_org
50
+ end
51
+ end
@@ -0,0 +1,63 @@
1
+ require "test_helper"
2
+ require "numeric_memoist"
3
+ require "stringio"
4
+
5
+ class IntegerMemoizeTest < Test::Unit::TestCase
6
+ class ::Integer
7
+ extend NumericMemoist
8
+ end
9
+
10
+ def setup
11
+ @stdout_org = $stdout.dup
12
+ @stdout_buffer = ""
13
+ $stdout = StringIO.new @stdout_buffer
14
+ end
15
+
16
+ def test_no_args_method
17
+ ::Integer.class_eval do
18
+ def no_args_method
19
+ puts "call #{self}.no_args_method"
20
+ self
21
+ end
22
+ memoize :no_args_method
23
+ end
24
+ assert_equal 1, 1.no_args_method
25
+ assert_equal ["call 1.no_args_method"], @stdout_buffer.chomp.split(/\r?\n/)
26
+ assert_equal 1, 1.no_args_method
27
+ assert_equal ["call 1.no_args_method"], @stdout_buffer.chomp.split(/\r?\n/)
28
+ end
29
+
30
+ def test_some_args_method
31
+ ::Integer.class_eval do
32
+ def some_args_method a, b
33
+ puts "call #{self}.some_args_method(#{a}, #{b})"
34
+ self * a * b
35
+ end
36
+ memoize :some_args_method
37
+ end
38
+ assert_equal 6, 1.some_args_method(2, 3)
39
+ assert_equal ["call 1.some_args_method(2, 3)"], @stdout_buffer.chomp.split(/\r?\n/)
40
+ assert_equal 6, 1.some_args_method(3, 2)
41
+ assert_equal ["call 1.some_args_method(2, 3)", "call 1.some_args_method(3, 2)"], @stdout_buffer.chomp.split(/\r?\n/)
42
+ assert_equal 6, 1.some_args_method(2, 3)
43
+ assert_equal ["call 1.some_args_method(2, 3)", "call 1.some_args_method(3, 2)"], @stdout_buffer.chomp.split(/\r?\n/)
44
+ end
45
+
46
+ def test_recursive_method
47
+ ::Integer.class_eval do
48
+ def nth_fib
49
+ puts "call fib(#{self})"
50
+ return (even? ? -(-self).nth_fib : (-self).nth_fib) if self < 0
51
+ return self if self < 2
52
+ (self - 1).nth_fib + (self - 2).nth_fib
53
+ end
54
+ memoize :nth_fib
55
+ end
56
+ assert_equal 55, 10.nth_fib
57
+ assert_equal ["call fib(10)", "call fib(9)", "call fib(8)", "call fib(7)", "call fib(6)", "call fib(5)", "call fib(4)", "call fib(3)", "call fib(2)", "call fib(1)", "call fib(0)"], @stdout_buffer.chomp.split(/\r?\n/)
58
+ end
59
+
60
+ def teardown
61
+ $stdout = @stdout_org
62
+ end
63
+ end
@@ -0,0 +1,38 @@
1
+ require "test_helper"
2
+ require "numeric_memoist"
3
+
4
+ class NumericMemoistTest < Test::Unit::TestCase
5
+ extend NumericMemoist
6
+
7
+ def target_methods
8
+ [:gcd, :lcm, :gcdlcm]
9
+ end
10
+ memoize :target_methods
11
+
12
+ # def setup
13
+ # end
14
+
15
+ def test_memoize_exist_methods
16
+ methods = target_methods
17
+ assert_nothing_raised do
18
+ ::Integer.class_eval do
19
+ extend NumericMemoist
20
+
21
+ memoize *methods
22
+ end
23
+ end
24
+ end
25
+
26
+ def test_memoize_non_exist_methods
27
+ assert_raise NameError do
28
+ ::Integer.class_eval do
29
+ extend NumericMemoist
30
+
31
+ memoize :not_exist_method
32
+ end
33
+ end
34
+ end
35
+
36
+ # def teardown
37
+ # end
38
+ end
@@ -0,0 +1,3 @@
1
+ require "test/unit"
2
+
3
+ $:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numeric_memoist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - antimon2
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-16 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: The memoize library allows you to cache methods for Numerics (Integer/Float)
42
+ in Ruby >= 2.0.0.
43
+ email:
44
+ - antimon2.me@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - examples/isqrt_example.rb
55
+ - lib/numeric_memoist.rb
56
+ - lib/numeric_memoist/version.rb
57
+ - numeric_memoist.gemspec
58
+ - test/float_memoize_test.rb
59
+ - test/integer_memoize_test.rb
60
+ - test/numeric_memoist_test.rb
61
+ - test/test_helper.rb
62
+ homepage: https://github.com/antimon2/numeric_memoist
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.9.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.0
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Memoize methods in Integer/Float
86
+ test_files:
87
+ - test/float_memoize_test.rb
88
+ - test/integer_memoize_test.rb
89
+ - test/numeric_memoist_test.rb
90
+ - test/test_helper.rb