File: multidispatch.rb

package info (click to toggle)
ruby-facets 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,824 kB
  • sloc: ruby: 25,483; xml: 90; makefile: 20
file content (236 lines) | stat: -rw-r--r-- 5,661 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require 'facets/core/kernel/instance_exec'

module MultipleDispatch

  class Dispatcher

    def initialize(name, lists)
      @name = name
      @lists = lists
    end

    def run(you, args)
      @lists.each do |list|
        catch(:mismatch) do
          return list.match(you, args)
        end
      end
      begin
        raise NoMethodError.new("undefined method `#{@name}' for #{you.inspect}:#{you.class}")
      rescue NoMethodError => e
        e.set_backtrace(e.backtrace[2..-1])
        raise e
      end
    end

  end

  class ArgumentList

    def initialize(required, optional, defaults, catch_all, catch_all_type, &action)
      @required = required
      @optional = optional
      @defaults = defaults
      @catch_all = catch_all
      @catch_all_type = catch_all_type
      @action = action
      calc_arity
    end

    def calc_arity
      @min_arity = @required.length
      @max_arity = (@min_arity + @optional.length unless @catch_all)
    end

    def match(you, args)
      throw :mismatch if args.length < @min_arity
      throw :mismatch if @max_arity && @max_arity < args.length
      (0...@required.length).each do |i|
        if ArgumentChanger === @required[i]
          args[i] = @required[i] === args[i]
        else
          throw :mismatch unless @required[i] === args[i]
        end
      end
      (@required.length...[args.length, @required.length + @optional.length].min).each do |i|
        if ArgumentChanger === @optional[i - @required.length]
          args[i] = @optional[i - @required.length] === args[i]
        else
          throw :mismatch unless @optional[i - @required.length] === args[i]
        end
      end
      (args.length - @required.length...@optional.length).each do |i|
        args << @defaults[i]
      end
      if @catch_all
        (@required.length + @optional.length...args.length).each do |i|
          if ArgumentChanger === @catch_all_type
            args[i] = @catch_all_type === args[i]
          else
            throw :mismatch unless @catch_all_type === args[i]
          end
        end
        you.instance_exec(*(args[0, @required.length + @optional.length] << args[@required.length + @optional.length..-1]), &@action)
      else
        you.instance_exec(*args, &@action)
      end
    end

  end

  module ArgumentChanger
    
  end

  class PropertyDispatch

    def initialize(&blk)
      @blk = blk
    end

    def ===(arg)
      @blk.call(arg)
    end

  end

  class CastDispatch

    include ArgumentChanger

    def initialize(type)
      @type = :"to_#{type}"
    end

    def ===(arg)
      arg.send(@type) rescue throw :mismatch
    end

    def changes_argument?
      true
    end

  end

  class AndDispatch
    
    def initialize(subterms)
      @subterms = subterms
    end

    def ===(arg)
      @subterms.each do |t|
        return false unless t === arg
      end
      true
    end

  end

  class OrDispatch

    def initialize(subterms)
      @subterms = subterms
    end

    def ===(arg)
      @subterms.each do |t|
        return true if t === arg
      end
      false
    end

  end

  class DontCareDispatch

    def initialize
      
    end

    def ===(arg)
      true
    end

  end

end

if __FILE__ == $0

  include MultipleDispatch

  class A

    def m(*args, &blk)
      # Argument list 1
      arg1 = ArgumentList.new([String, String], [], [], false, nil) do |s1, s2|
        yield [:two_strings, s1, s2]
      end
      # Argument list 2
      arg2 = ArgumentList.new([String, Integer], [], [], false, nil) do |s, i|
        yield [:string_and_integer, s, i]
      end
      # Argument list 3
      arg3 = ArgumentList.new([Integer, Integer, Integer], [], [], false, nil) do |i1, i2, i3|
        yield [:three_integers, i1, i2, i3]
      end
      # Argument list 4
      arg4 = ArgumentList.new([:do1, Integer], [], [], false, nil) do |_, i|
        yield [:do1, i]
      end
      # Argument list 5
      arg5 = ArgumentList.new([:do2, Integer], [], [], false, nil) do |_, i|
        yield [:do2, i]
      end
      # Argument list 6
      arg6 = ArgumentList.new([:do3, Integer], [Integer, String], [11, "extra"], false, nil) do |_, i1, i2, s|
        yield [:do3, i1, i2, s]
      end
      # Argument list 7
      arg7 = ArgumentList.new([:do4], [], [], true, Integer) do |_, args|
        yield [:do4, args]
      end
      # Argument list 8
      arg8 = ArgumentList.new([:do4], [], [], true, String) do |_, args|
        yield [:do4, args]
      end
      # Argument list 9
      arg9 = ArgumentList.new([PropertyDispatch.new { |a| :do5 === a }], [], [], false, nil) do |_|
        yield [:do5]
      end
      # Argument list 10
      arg10 = ArgumentList.new([/run/, DontCareDispatch.new], [], [], false, nil) do |i, dc|
        yield [:run, i, dc]
      end
      # Argument list 11
      arg11 = ArgumentList.new([:do6, CastDispatch.new(:sym)], [], [], false, nil) do |_, s|
        yield [:do6, s]
      end
      
      # Dispatcher
      d = Dispatcher.new(:'test', [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11])

      d.run(self, args)
    end

  end

  A.new.m("hello", "world") { |a| p a }
  A.new.m("world", 1) { |a| p a }
  A.new.m(1, 2, 3) { |a| p a }
  A.new.m(:do1, 1) { |a| p a }
  A.new.m(:do2, 2) { |a| p a }
  A.new.m(:do3, 3, 4, "a1") { |a| p a }
  A.new.m(:do3, 3, 4) { |a| p a }
  A.new.m(:do3, 3) { |a| p a }
  A.new.m(:do4) { |a| p a }
  A.new.m(:do4, 2) { |a| p a }
  A.new.m(:do4, 2, 3) { |a| p a }
  A.new.m(:do4, "2", "3") { |a| p a }
  A.new.m(:do5) { |a| p a }
  A.new.m("grunt", 1) { |a| p a }
  A.new.m("grunt", :a) { |a| p a }
  A.new.m(:do6, "hi") { |a| p a }

end