Skip to content

Instantly share code, notes, and snippets.

@mark
Created December 7, 2010 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark/731905 to your computer and use it in GitHub Desktop.
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
> :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]
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