lib/funkr/extensions/enumerable.rb in funkr-0.0.12 vs lib/funkr/extensions/enumerable.rb in funkr-0.0.14

- old
+ new

@@ -1,35 +1,30 @@ # -*- coding: utf-8 -*- module Enumerable # enumerable extensions - class NoHead; end - - def head; empty? ? NoHead : first; end - - def tail; self.drop(1); end - def all_different? - return true if tail.empty? - !tail.include?(head) and tail.all_different? + a = self.to_a + self.each do |e| + return false if a.include?(e) + a.shift + end + return true end # http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/Data-List.html#group def group_seq_by(&block) - if empty? then [] - else - res = [] - rst = self - while rst.any? do - v = yield(rst.head) - a,rst = rst.span{|x| (yield x) == v} - res.push(a) - end - return res + res = [] + rst = self.to_a + while !rst.empty? do + v = yield(rst.first) + a,rst = rst.span{|x| (yield x) == v} + res.push(a) end + return res end - + # http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/GHC-List.html#span def span(&block) inc = [] self.each_with_index do |e,i| if yield(e) then inc.push(e) @@ -38,17 +33,18 @@ return [ inc, [] ] end # Constitue des groupes disjoints de n éléments au plus def groups_of(n) - return [] if self.empty? - [self.take(n)] + self.drop(n).groups_of(n) + g = self.take(n) + return [] if g.empty? + [g] + self.drop(n).groups_of(n) end # constitue des groupes de n éléments exactement, décalés de 1 def sliding_groups_of(n) return [] if self.size < n - [ self.take(n) ] + self.tail.sliding_groups_of(n) + [ self.take(n) ] + self.drop(1).sliding_groups_of(n) end # trouve l'index d'une séquence def seq_index(seq) self.sliding_groups_of(seq.size).index(seq)