Module: IdempotentEnumerable

Includes:
Enumerable
Defined in:
lib/idempotent_enumerable.rb,
lib/idempotent_enumerable/configurator.rb

Defined Under Namespace

Classes: Configurator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
# File 'lib/idempotent_enumerable.rb', line 4

def self.included(klass)
  def klass.idempotent_enumerable
    @idempotent_enumerable ||= Configurator.new(self)
  end
end

Instance Method Details

#chunk(*arg, &block) ⇒ Object



19
20
21
22
23
# File 'lib/idempotent_enumerable.rb', line 19

def chunk(*arg, &block)
  # FIXME: should return enumerator
  return to_enum(:chunk, *arg) unless block
  each(*arg).chunk(&block).map { |key, group| [key, idempotently_construct(group)] }
end

#chunk_while(*arg, &block) ⇒ Object



26
27
28
# File 'lib/idempotent_enumerable.rb', line 26

def chunk_while(*arg, &block)
  idempotent_enumerator(each(*arg).chunk_while(&block))
end

#drop(num, *arg) ⇒ Object



31
32
33
# File 'lib/idempotent_enumerable.rb', line 31

def drop(num, *arg)
  idempotently_construct(each(*arg).drop(num))
end

#each_cons(num, *arg) ⇒ Object



35
36
37
38
# File 'lib/idempotent_enumerable.rb', line 35

def each_cons(num, *arg)
  return to_enum(:each_cons, num, *arg) unless block_given?
  each(*arg).each_cons(num) { |slice| yield(idempotently_construct(slice)) }
end

#each_slice(num, *arg) ⇒ Object



40
41
42
43
# File 'lib/idempotent_enumerable.rb', line 40

def each_slice(num, *arg)
  return to_enum(:each_slice, num, *arg) unless block_given?
  each(*arg).each_slice(num) { |slice| yield(idempotently_construct(slice)) }
end

#first(*arg) ⇒ Object



45
46
47
48
# File 'lib/idempotent_enumerable.rb', line 45

def first(*arg)
  arg, num = idempotent_split_arg(arg)
  num ? idempotently_construct(each(*arg).first(*num)) : each(*arg).first
end

#grep(*arg, pattern, &block) ⇒ Object



50
51
52
# File 'lib/idempotent_enumerable.rb', line 50

def grep(*arg, pattern, &block)
  idempotently_construct(each(*arg).grep(pattern, &block))
end

#grep_v(pattern, *arg, &block) ⇒ Object



55
56
57
# File 'lib/idempotent_enumerable.rb', line 55

def grep_v(pattern, *arg, &block)
  idempotently_construct(each(*arg).grep_v(pattern, &block))
end

#group_by(*arg, &block) ⇒ Object



60
61
62
# File 'lib/idempotent_enumerable.rb', line 60

def group_by(*arg, &block)
  each(*arg).group_by(&block).map { |key, val| [key, idempotently_construct(val)] }.to_h
end

#max(*arg) ⇒ Object



75
76
77
78
# File 'lib/idempotent_enumerable.rb', line 75

def max(*arg)
  arg, num = idempotent_split_arg(arg)
  num ? idempotently_construct(each(*arg).max(*num)) : each(*arg).max
end

#max_by(*arg, &block) ⇒ Object



80
81
82
83
84
# File 'lib/idempotent_enumerable.rb', line 80

def max_by(*arg, &block)
  return to_enum(:max_by, *arg) unless block
  arg, num = idempotent_split_arg(arg)
  num ? idempotently_construct(each(*arg).max_by(*num, &block)) : each(*arg).max_by(&block)
end

#min(*arg) ⇒ Object



64
65
66
67
# File 'lib/idempotent_enumerable.rb', line 64

def min(*arg)
  arg, num = idempotent_split_arg(arg)
  num ? idempotently_construct(each(*arg).min(*num)) : each(*arg).min
end

#min_by(*arg, &block) ⇒ Object



69
70
71
72
73
# File 'lib/idempotent_enumerable.rb', line 69

def min_by(*arg, &block)
  return to_enum(:min_by, *arg) unless block
  arg, num = idempotent_split_arg(arg)
  num ? idempotently_construct(each(*arg).min_by(*num, &block)) : each(*arg).min_by(&block)
end

#partition(*arg, &block) ⇒ Object



86
87
88
89
# File 'lib/idempotent_enumerable.rb', line 86

def partition(*arg, &block)
  return to_enum(:partition, *arg) unless block
  each(*arg).partition(&block).map(&method(:idempotently_construct))
end

#slice_after(pattern = nil, &block) ⇒ Object



96
97
98
# File 'lib/idempotent_enumerable.rb', line 96

def slice_after(pattern = nil, &block)
  idempotent_enumerator(pattern ? each.slice_after(pattern) : each.slice_after(&block))
end

#slice_before(pattern = nil, &block) ⇒ Object



91
92
93
# File 'lib/idempotent_enumerable.rb', line 91

def slice_before(pattern = nil, &block)
  idempotent_enumerator(pattern ? each.slice_before(pattern) : each.slice_before(&block))
end

#slice_when(*arg, &block) ⇒ Object



100
101
102
# File 'lib/idempotent_enumerable.rb', line 100

def slice_when(*arg, &block)
  idempotent_enumerator(each(*arg).slice_when(&block))
end

#sort(*arg, &block) ⇒ Object



105
106
107
# File 'lib/idempotent_enumerable.rb', line 105

def sort(*arg, &block)
  idempotently_construct(each(*arg).sort(&block))
end

#take(num, *arg) ⇒ Object



109
110
111
# File 'lib/idempotent_enumerable.rb', line 109

def take(num, *arg)
  idempotently_construct(each(*arg).take(num))
end

#uniq(*arg, &block) ⇒ Object



114
115
116
# File 'lib/idempotent_enumerable.rb', line 114

def uniq(*arg, &block)
  idempotently_construct(each(*arg).uniq(&block))
end