Skip to content

Instantly share code, notes, and snippets.

@frogstarr78
Created May 6, 2010 05:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frogstarr78/391800 to your computer and use it in GitHub Desktop.
Save frogstarr78/391800 to your computer and use it in GitHub Desktop.
Attempt to use ruby to generate some functional constructs.

Functional

  • Functional

DESCRIPTION:

Using Ruby to generate some functional constructs

FEATURES/PROBLEMS:

Uses dynamic memoization for performance

PROBLEMS:

None currently know.

EXAMPLES:

See Example.rb

REQUIREMENTS:

  • None

INSTALL:

  • gem

LICENSE:

(The MIT License)

Copyright © 2009 Frogstarr78 Software

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'benchmark'
require 'Functional'
Benchmark.bmbm 30 do |bm|
f = Functional.new
bm.report( "define fib 1" ) { f.define( :fib, 1 ) { 1 } }
bm.report( "define fib 2" ) { f.define( :fib, 2 ) { 1 } }
bm.report( "define fib Fixnum" ) { f.define( :fib, Fixnum ) {|n| fib(n-1) + fib(n-2) } }
bm.report("1 call f.fib 1") { puts f.fib 1 }
bm.report("1 call f.fib 2") { puts f.fib 2 }
bm.report("1 call f.fib 4") { puts f.fib 4 }
bm.report("1 call f.fib 14") { puts f.fib 14 }
bm.report("2 call f.fib 1") { puts f.fib 1 }
bm.report("2 call f.fib 2") { puts f.fib 2 }
bm.report("2 call f.fib 4") { puts f.fib 4 }
bm.report("2 call f.fib 14") { puts f.fib 14 }
# bm.report("call f.fib 1014") { puts f.fib 1014 }
end
puts '','='*100,''
class Fib
def self.run n
case n
when 1, 2
1
else
run(n-1) + run(n-2)
end
end
end
Benchmark.bmbm 30 do |bm|
# bm.report( "define class Fib w/ case statement" ) {
# }
bm.report("1 call f.fib 1 w/ case") { puts Fib.run 1 }
bm.report("1 call f.fib 2 w/ case") { puts Fib.run 2 }
bm.report("1 call f.fib 4 w/ case") { puts Fib.run 4 }
bm.report("1 call f.fib 14 w/ case") { puts Fib.run 14 }
bm.report("2 call f.fib 1 w/ case") { puts Fib.run 1 }
bm.report("2 call f.fib 2 w/ case") { puts Fib.run 2 }
bm.report("2 call f.fib 4 w/ case") { puts Fib.run 4 }
bm.report("2 call f.fib 14 w/ case") { puts Fib.run 14 }
# bm.report("call f.fib 1014") { puts Fib.run 1014 }
end
$LOAD_PATH.unshift '.' if RUBY_VERSION > '1.9.1'
require 'Functional'
def hr size
puts '-' * size
end
hr 10
f = Functional.new
f.define( :fac, 0 ) { 1 }
f.define( :fac, Fixnum ) {|n| n * fac(n-1) }
puts f.fac 0
puts f.fac 4
hr 10
f.define( :fib, 1 ) { 1 }
f.define( :fib, 2 ) { 1 }
f.define( :fib, Fixnum ) {|n| fib(n-1) + fib(n-2) }
puts f.fib 1
puts f.fib 2
puts f.fib 12
class Functional
def define name, argument, &block
if not self.respond_to? name
self.class.send :define_method, name do |arg|
if self.respond_to? "#{name}_#{arg}"
method_name = "#{name}_#{arg}"
method(method_name).call arg
else
method_name = "#{name}_#{arg.class}"
value = method(method_name).call arg
define name, arg, &lambda { value }
value
end
end
end
self.class.send :define_method, "#{name}_#{argument}", block
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment