public
Description: a Scheme written in Ruby, but implemented on the bus!
Homepage: http://bus-scheme.rubyforge.org
Clone URL: git://github.com/technomancy/bus-scheme.git
added caddadar type methods to cons
technomancy (author)
Sat May 31 17:37:21 -0700 2008
commit  ee954a3f167510190e9c67c7ce4a21beb80a5203
tree    32bdcd22ce195c71eef15f22b2f82fd76463013b
parent  db9043eadfcf42ccae7234c84156a08fe4ec79f4
...
93
94
95
96
 
 
97
98
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
101
102
103
104
105
106
107
108
...
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
0
@@ -93,16 +93,31 @@ module BusScheme
0
 
0
     # allows for (mylist 4) => (nth mylist 4)
0
     def call(nth)
0
- nth = nth.car if nth.is_a? Cons # TODO: remove?
0
+ # Allow lists to be called with an argument list or a fixnum arg
0
+ nth = nth.car if nth.is_a? Cons
0
       nth == 0 ? @car : @cdr.call(nth - 1)
0
     end
0
     include Callable
0
+
0
+ # support for methods like caddar
0
+ def method_missing(sym)
0
+ sym = sym.to_s
0
+ raise NoMethodError unless sym =~ /^c([ad]+)r/
0
+ case sym
0
+ when 'car'
0
+ car
0
+ when 'cdr'
0
+ cdr
0
+ when /^ca/
0
+ send(sym.sub('a', '')).car
0
+ when /^cd/
0
+ send(sym.sub('d', '')).cdr
0
+ end
0
+ end
0
   end
0
 
0
   def cons(car = nil, cdr = nil)
0
     Cons.new(car, cdr)
0
   end
0
   module_function :cons
0
-
0
- # TODO: use method_missing to handle stuff like caadadadar
0
 end
...
44
45
46
 
 
 
 
 
 
 
47
...
44
45
46
47
48
49
50
51
52
53
54
0
@@ -44,4 +44,11 @@ class TestCons < Test::Unit::TestCase
0
     # assert_evals_to cons(true, cons(false)), "'(#t #f)"
0
     # assert_evals_to cons(true, false), "'(#t . #f)"
0
   end
0
+
0
+ def test_caadar_stuff
0
+ list = (0 .. 9).to_a
0
+ assert_equal 2, list.to_list.caddr
0
+ list[3] = ['a', 'b', 'c']
0
+ assert_equal 'b', list.to_list.cadadddr
0
+ end
0
 end

Comments

    No one has commented yet.