Created
November 13, 2014 02:42
-
-
Save aileron/7ad7dffa54a22072de7e 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 Array | |
def eql_all? &block | |
block = lambda {|a|a} unless block | |
max=self.length | |
i=0 | |
c = block.( self[i] ) | |
n = nil | |
result = while i+1<max | |
n=block.(self[i+1]) | |
break false unless c == n | |
c=n | |
i+=1 | |
end | |
result.nil? | |
end | |
end |
結局、func(a) == func(b) を想定するなら
func(a) == func(b) == func(c) 等も存在するだろうから
問題としては、複数の値に、同一の処理を行って比較する事にあるのかなと思ったので
なら arrayに、値の比較の為のブロックを受け付けるメソッドが存在すれば良いかと
思ったので、とりあえずモンキーパッチで試してみた所。
なるほど、であれば
class Array
def eql_all?(&block)
map(&block).uniq.count == 1
end
end
で良いかもしれませんね、uniqは===
比較してくれる気がするので.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
おおありがとうございます!
eql?
の挙動変えるfeatureなのでどちらにせよArray#eql?
も変えないといけないですね、while
で回すよりC層でrb_exec_recursive_paired
というAPIが提供されているのでこんな感じがいいかなーと思っています