memoize 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGES +2 -0
  2. data/MANIFEST +11 -0
  3. data/README +55 -0
  4. data/lib/memoize.rb +12 -0
  5. data/test/tc_memoize.rb +34 -0
  6. metadata +46 -0
data/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ = 1.0.0
2
+ * Initial release
data/MANIFEST ADDED
@@ -0,0 +1,11 @@
1
+ CHANGES
2
+ MANIFEST
3
+ README
4
+ install.rb
5
+ memoize.gemspec
6
+
7
+ examples/fibonacci.rb
8
+
9
+ lib/memoize.rb
10
+
11
+ test/tc_memoize.rb
data/README ADDED
@@ -0,0 +1,55 @@
1
+ = Description
2
+ A method that speeds methods up at the cost of memory.
3
+
4
+ = Prerequisites
5
+ Ruby 1.8.0 or later
6
+
7
+ = Installation
8
+ == Standard Installation
9
+ ruby test/tc_memoize.rb (optional)
10
+ ruby install.rb
11
+
12
+ == Gems Installation
13
+ ruby test/tc_memoize.rb (optional)
14
+ ruby memoize.gemspec
15
+ gem install memoize-xyz.gem # Where 'xyz' is the version
16
+
17
+ = Synopsis
18
+ require "memoize"
19
+ include Memoize
20
+
21
+ # Inefficient fibonacci method
22
+ def fib(n)
23
+ return n if n < 2
24
+ fib(n-1) + fib(n-2)
25
+ end
26
+
27
+ fib(100) # Slow
28
+
29
+ memoize(:fib)
30
+ fib(100) # Fast
31
+
32
+ = Constants
33
+ Memoize::MEMOIZE_VERSION
34
+ Returns the version of this package as a String.
35
+
36
+ = Methods
37
+ Memoize.memoize(method)
38
+ Takes a method (symbol) and caches the results of +method+ in a hash table.
39
+ If you call +method+ again with the same arguments, memoize gives you the
40
+ value from the table instead of letting the method compute the value again.
41
+
42
+ = Acknowledgements
43
+ Code borrowd from Nobu Nakada (ruby-talk:155159).
44
+
45
+ == License
46
+ Ruby's
47
+
48
+ == Copyright
49
+ (C) 2005, Daniel J. Berger
50
+ All Rights Reserved
51
+
52
+ = Author
53
+ Daniel J. Berger
54
+ djberg96@gmail.com
55
+ IRC nick: imperator (freenode)
data/lib/memoize.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Memoize
2
+ MEMOIZE_VERSION = "1.0.0"
3
+ def memoize(name)
4
+ meth = method(name)
5
+ cache = {}
6
+ (class << self; self; end).class_eval do
7
+ define_method(name) do |*args|
8
+ cache[args] ||= meth.call(*args)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ ###############################################
2
+ # tc_memoize.rb
3
+ #
4
+ # Test suite for the memoize package.
5
+ ###############################################
6
+ base = File.basename(Dir.pwd)
7
+
8
+ if base == "test" || base =~ /memoize/
9
+ Dir.chdir("..") if base == "test"
10
+ $LOAD_PATH.unshift(Dir.pwd + "/lib")
11
+ Dir.chdir("test") rescue nil
12
+ end
13
+
14
+ require "test/unit"
15
+ require "memoize"
16
+
17
+ class TC_Memoize < Test::Unit::TestCase
18
+ include Memoize
19
+
20
+ def fib(n)
21
+ return n if n < 2
22
+ fib(n-1) + fib(n-2)
23
+ end
24
+
25
+ def test_version
26
+ assert_equal("1.0.0", Memoize::MEMOIZE_VERSION)
27
+ end
28
+
29
+ def test_memoize
30
+ assert_respond_to(self, :memoize)
31
+ assert_nothing_raised{ memoize(:fib) }
32
+ assert_nothing_raised{ fib(50) }
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: memoize
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2005-09-09 00:00:00 -06:00
8
+ summary: Speeds up methods at the cost of memory
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/shards
13
+ rubyforge_project:
14
+ description: Speeds up methods at the cost of memory
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ signing_key:
28
+ cert_chain:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - lib/memoize.rb
33
+ - CHANGES
34
+ - MANIFEST
35
+ - README
36
+ - test/tc_memoize.rb
37
+ test_files:
38
+ - test/tc_memoize.rb
39
+ rdoc_options: []
40
+ extra_rdoc_files:
41
+ - README
42
+ - CHANGES
43
+ executables: []
44
+ extensions: []
45
+ requirements: []
46
+ dependencies: []