-
-
Save apeiros/6428ca06a6c2a8dd60e25c501c07a8de to your computer and use it in GitHub Desktop.
This file contains 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 SliceApplicator | |
def initialize(target, *slice) | |
@target = target | |
@slice = slice | |
end | |
def respond_to_missing?(name) | |
@target.respond_to?(name) | |
end | |
def method_missing(name, *args, &block) | |
slice = @target[*@slice] | |
result = slice.public_send(name, *args, &block) | |
@target[*@slice] = slice | |
result.equal?(slice) ? @target : result | |
end | |
end | |
class Array | |
def partially_apply(*slice) | |
SliceApplicator.new(self, *slice) | |
end | |
end | |
ary = [1,2,3,4,5,6,7,8] | |
ary.partially_apply(0, 4).reject! do |x| x.odd? end | |
ary # => [2, 4, 5, 6, 7, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment