Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Friday, June 5, 2009

The Possibility of Unary Operators

class Symbol
  def -@
    to_s.reverse.to_sym
  end
end

class Fixnum
  def to_proc
    lambda { self }
  end
end

class Regexp
  def -@
    to_s
  end

  def to_proc
    lambda { self }
  end
end

class Array
  def -@
    reverse
  end

  def to_proc
    lambda {|i| self[i] }
  end
end

p -:hello
p [nil].map(&10)
p -/quick/
p [nil].map(&/cool/)
p -[1, 2, 3]
p [0, 1, 1, 2].map(&[:a, :b, :c])

No comments:

Post a Comment

Followers