Skip to content

Instantly share code, notes, and snippets.

@aoitaku
Created February 12, 2015 10:39
Show Gist options
  • Save aoitaku/507d3da3a73254be9d46 to your computer and use it in GitHub Desktop.
Save aoitaku/507d3da3a73254be9d46 to your computer and use it in GitHub Desktop.
class Symbol
def call(*args, &block)
if block
-> recv { to_proc.(recv, *args, &block) }
else
-> recv { to_proc.(recv, *args) }
end
end
end
class Array
def call(*args, &block)
symbol, * = *self
if block
-> recv { symbol.to_proc.(recv, *args, &block) }
else
-> recv { symbol.to_proc.(recv, *args) }
end
end
def to_proc
-> callables {
-> recv {
callables.inject(recv) {|r, c| c.call(r) }
}
}.(self.map {|_| symbol, *args = *_.first ; symbol.(*args) })
end
end
class Hash
def call(*args, &block)
symbol, * = *self.first
if block
-> recv { symbol.to_proc.(recv, *args, &block) }
else
-> recv { symbol.to_proc.(recv, *args) }
end
end
def to_proc
-> callables {
-> recv {
callables.inject(recv) {|r, c| c === r }
}
}.([*self].map {|symbol, *args| symbol.(*args) })
end
end
p [*0..10].map(&:*.(2))
p [*0..10].map(&{:* => 2, :+ => 3})
p [*0..10].map(&[{:* => 3}, {:/ => 2}, {:+ => 1}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment