Created
December 7, 2010 15:28
-
-
Save mark/731905 to your computer and use it in GitHub Desktop.
You don't get anything approaching the nice syntax, but you can implement the X and Z metaoperators in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> :cons.X [1, 2], ['a', 'b'] | |
=> [[1, "a"], [1, "b"], [2, "a"], [2, "b"]] | |
> :+.X [1, 2], [10, 11] | |
=> [11, 12, 12, 13] | |
> :strcat.X [1, 2], [10, 11] | |
=> ["110", "111", "210", "211"] | |
> :==.X [1, 2], [1, 1] | |
=> [true, true, false, false] | |
> :cons.Z [1, 2], [3, 4] | |
=> [[1, 3], [2, 4]] | |
> :+.Z [1, 2], [3, 4] | |
=> [4, 6] | |
> :==.Z [1, 2], [1, 1] | |
=> [true, false] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def cons(other) | |
[ self, other ] | |
end | |
def strcat(other) | |
self.to_s + other.to_s | |
end | |
end | |
class Symbol | |
def X(ary, bry) | |
result = [] | |
ary.each do |a| | |
bry.each do |b| | |
result << a.send(self, b) | |
end | |
end | |
return result | |
end | |
def Z(ary, bry) | |
result = [] | |
ary.length.times do |i| | |
result << ary[i].send(self, bry[i]) | |
end | |
return result | |
end | |
end | |
puts "> :cons.X [1, 2], ['a', 'b']" | |
puts "=> #{ :cons.X([1, 2], ['a', 'b']).inspect }" | |
puts | |
puts "> :+.X [1, 2], [10, 11]" | |
puts "=> #{ :+.X([1, 2], [10, 11]).inspect }" | |
puts | |
puts "> :strcat.X [1, 2], [10, 11]" | |
puts "=> #{ :strcat.X([1, 2], [10, 11]).inspect }" | |
puts | |
puts "> :==.X [1, 2], [1, 1]" | |
puts "=> #{ :==.X([1, 2], [1, 1]).inspect }" | |
puts | |
puts "> :cons.Z [1, 2], [3, 4]" | |
puts "=> #{ :cons.Z([1, 2], [3, 4]).inspect }" | |
puts | |
puts "> :+.Z [1, 2], [3, 4]" | |
puts "=> #{ :+.Z([1, 2], [3, 4]).inspect }" | |
puts | |
puts "> :==.Z [1, 2], [1, 1]" | |
puts "=> #{ :==.Z([1, 2], [1, 1]).inspect }" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment