lib/funkr/extensions/enumerable.rb in funkr-0.0.11 vs lib/funkr/extensions/enumerable.rb in funkr-0.0.12
- old
+ new
@@ -1,5 +1,6 @@
+# -*- coding: utf-8 -*-
module Enumerable
# enumerable extensions
class NoHead; end
@@ -52,11 +53,13 @@
# trouve l'index d'une séquence
def seq_index(seq)
self.sliding_groups_of(seq.size).index(seq)
end
- def uniq_by(&block)
+ # Prend un prédicat p(x,y), et construit un tableau dans lequel tous
+ # les couples (a,b), tels que 'a' précède 'b', vérifient p(a,b).
+ def make_uniq_by(&block)
result = []
self.each do |e|
unless result.any?{|x| yield(x,e)} then result.push(e) end
end
return result
@@ -64,11 +67,11 @@
# 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)
m, i, a = [], [], []
- u_s = self.uniq_by(&block)
- u_o = other.uniq_by(&block)
+ u_s = self.make_uniq_by(&block)
+ u_o = other.make_uniq_by(&block)
u_s.each do |e|
if u_o.find{|x| yield(e,x)} then i.push(e) # intersection
else m.push(e) end # missing
end
u_o.each { |e| a.push(e) unless u_s.find{|x| yield(e,x)} } # added