Created
April 29, 2018 11:05
-
-
Save gotoken/b009eb8d88dbce8a5f6d544099d30498 to your computer and use it in GitHub Desktop.
pretty looks Numo::NArray#inplace alternative
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
module InplaceAccessor | |
def self.append_features(klass) | |
klass.class_eval do | |
alias _getter [] | |
alias _inplace inplace | |
alias _setter []= | |
def inplace | |
@inplace ||= _inplace | |
end | |
def [](*arg) | |
if arg.empty? | |
return inplace | |
else | |
_getter(*arg) | |
end | |
end | |
def []=(*arg) | |
if arg.size == 1 | |
self | |
else | |
_setter(*arg) | |
end | |
end | |
end | |
end | |
end | |
require "numo/narray" | |
%w(Bit DComplex DFloat Int16 Int32 Int64 Int8 RObject SComplex SFloat UInt16 UInt32 UInt64 UInt8).each do |type| | |
Numo.const_get(type).class_eval do | |
include InplaceAccessor | |
end | |
end | |
a = Numo::DFloat.new(3,5).seq | |
a[] += 100 | |
p a | |
# Numo::DFloat#shape=[3,5] | |
# [[100, 101, 102, 103, 104], | |
# [105, 106, 107, 108, 109], | |
# [110, 111, 112, 113, 114]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment