lib/funkr/extensions/array.rb in funkr-0.0.8 vs lib/funkr/extensions/array.rb in funkr-0.0.9
- old
+ new
@@ -105,12 +105,18 @@
# trouve l'index d'une séquence
def seq_index(seq)
self.sliding_groups_of(seq.size).index(seq)
end
- # difference entre 2 tableaux, retourne le couple [ [missing] , [added] ]
+ # difference entre 2 tableaux, retourne le triplet [ [missing] , [intersection], [added] ]
+ # codé en impératif parce que inject est trop lent :(
def diff_with(other, &block)
- [ self.inject([]){|a,e| other.find{|x| yield(e,x)} ? a : a + [e]},
- other.inject([]){|a,e| self.find{|x| yield(e,x)} ? a : a + [e]} ]
+ m, i, a = [], [], []
+ self.each do |e|
+ if other.find{|x| yield(e,x)} then i.push(e) # intersection
+ else m.push(e) end # missing
+ end
+ other.each { |e| a.push(e) unless self.find{|x| yield(e,x)} } # added
+ [ m, i, a ]
end
end