public
Description: Everyone should experiment!
Homepage:
Clone URL: git://github.com/jonleighton/experiments.git
experiments / functional.rb
100644 110 lines (90 sloc) 2.23 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# An attempt at creating a functional dialect of ruby - doesn't work atm
#
# Example:
# fib(0).is 1
# fib(1).is 1
# fib(gt(1)).is { |n| fib(n-2) + fib(n-1) }
#
# In order to work properly, when fib is called, it would need to
# 1. Search for a definition of fib with the given arguments
# 2. Return the answer if there is a definition
# 3. Otherwise raise an error
#
# This needs an array of instances (rather than the current hash) and for instances
# to respond to something like @instance.has_value_for?(5)
 
require 'delegate'
 
class Function
  attr_reader :dynamic_instance
 
  def initialize(name)
    @name = name
    self.class.class_eval <<-STR
def #{name}(*args, &block)
call(*args, &block)
end
STR
  end
  
  class FunctionInstances
    def initialize
      @instances = {}
    end
    
    def [](key)
      if instance_for?(key)
        @instances[key]
      else
        @instances[key] = FunctionInstance.new
      end
    end
    
    def []=(key, value)
      self[key].is(value)
    end
    
    def instance_for?(key)
      @instances.has_key? key
    end
  end
  
  class FunctionInstance < Delegator
    def initialize
      @value = nil
      super(nil)
    end
    
    def __getobj__
      @value
    end
    
    def __setobj__(obj)
      @value = obj
    end
    
    alias_method :is, :__setobj__
  end
  
  def instances
    @instances ||= FunctionInstances.new
  end
  
  def call(*args, &block)
    if block_given? && !block.nil?
      @dynamic_instance = block
    else
      if @dynamic_instance
        
      else
        
      end
    
      if static_instances.instance_for?(args)
        static_instances[args]
      elsif !@dynamic_instance.nil?
        @dynamic_instance.call(*args)
      else
        raise "The instance #{@name}(#{args.join(", ")}) has not been defined"
      end
    end
  end
end
 
class Object
  def method_missing(name, *args)
    raise NoMethodError unless name.to_s == "fib"
    func = Function.new(name)
    self.class.class_eval do
      define_method(name) { |*args| func.call(*args) }
    end
    func.call(*args)
  end
end
 
fib(0).is 1
fib(1).is 1
fib(gt(1)).is { |n| fib(n-2) + fib(n-1) }
 
#p (0..20).map { |i| fib.call(i) }