Module: Lazily::Enumerable

Includes:
Enumerable
Included in:
Concatenator, Dequeuer, Filter, Merger, Prefetcher, Proxy, Zipper
Defined in:
lib/lazily/zipping.rb,
lib/lazily/filtering.rb,
lib/lazily/threading.rb,
lib/lazily/enumerable.rb,
lib/lazily/prefetching.rb,
lib/lazily/concatenating.rb

Instance Method Summary collapse

Instance Method Details

#[](n) ⇒ Object

Returns the nth element.

Returns:

  • the nth element



219
220
221
# File 'lib/lazily/filtering.rb', line 219

def [](n)
  drop(n).first
end

#chunk(*args, &block) ⇒ Object



211
212
213
# File 'lib/lazily/filtering.rb', line 211

def chunk(*args, &block)
  super.lazily
end

#collect(&transformation) ⇒ Enumerable Also known as: map

Transform elements using the block provided.

Returns:

See Also:

  • Enumerable#collect


12
13
14
15
16
17
18
# File 'lib/lazily/filtering.rb', line 12

def collect(&transformation)
  filter("collect") do |yielder|
    each do |element|
      yielder.call yield(element)
    end
  end
end

#compactEnumerable

Ignore nil values.

Returns:

See Also:

  • Array#compact


193
194
195
196
197
198
199
# File 'lib/lazily/filtering.rb', line 193

def compact
  filter("compact") do |yielder|
    each do |element|
      yielder.call(element) unless element.nil?
    end
  end
end

#concat(*others) ⇒ Enumerable

Concatenates this and other Enumerables.

Parameters:

Returns:

  • (Enumerable)

    elements of this and other Enumerables

See Also:

  • Array#concat


32
33
34
# File 'lib/lazily/concatenating.rb', line 32

def concat(*others)
  Lazily.concat(self, *others)
end

#drop(n) ⇒ Enumerable

Ignore the first n elements.

Parameters:

  • n (Integer)

    the number of elements to drop

Returns:

See Also:

  • Enumerable#drop


117
118
119
120
121
122
123
124
# File 'lib/lazily/filtering.rb', line 117

def drop(n)
  filter("drop") do |yielder|
    each_with_index do |element, index|
      next if index < n
      yielder.call(element)
    end
  end
end

#drop_while(&predicate) ⇒ Enumerable

Reject elements while a predicate returns true.

Returns:

  • (Enumerable)

    all elements including and after the first that fails the predicate

See Also:

  • Enumerable#drop_while


132
133
134
135
136
137
138
139
140
# File 'lib/lazily/filtering.rb', line 132

def drop_while(&predicate)
  filter("drop_while") do |yielder|
    take = false
    each do |element|
      take ||= !yield(element)
      yielder.call(element) if take
    end
  end
end

#flat_map(&block) ⇒ Object Also known as: collect_concat



181
182
183
# File 'lib/lazily/filtering.rb', line 181

def flat_map(&block)
  map(&block).flatten
end

#flatten(level = 1) ⇒ Enumerable

Flatten the collection, such that Enumerable elements are included inline.

Returns:

  • (Enumerable)

    elements of elements of the collection

See Also:

  • Array#flatten


169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lazily/filtering.rb', line 169

def flatten(level = 1)
  filter("flatten") do |yielder|
    each do |element|
      if level > 0 && element.respond_to?(:each)
        element.flatten(level - 1).each(&yielder)
      else
        yielder.call(element)
      end
    end
  end
end

#grep(pattern) ⇒ Enumerable

Select elements matching a pattern.

Returns:

  • (Enumerable)

    elements for which the pattern matches

See Also:

  • Enumerable#grep


148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lazily/filtering.rb', line 148

def grep(pattern)
  filter("grep") do |yielder|
    each do |element|
      if pattern === element
        result = if block_given?
          yield element
        else
          element
        end
        yielder.call(result)
      end
    end
  end
end

#in_threads(max_threads, &block) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/lazily/threading.rb', line 7

def in_threads(max_threads, &block)
  collect do |item|
    Thread.new { block.call(item) }
  end.prefetch(max_threads - 1).collect do |thread|
    thread.join.value
  end
end

#lazilyObject



12
13
14
# File 'lib/lazily/enumerable.rb', line 12

def lazily
  self
end

#lazy?Boolean

Returns true.

Returns:

  • (Boolean)

    true



8
9
10
# File 'lib/lazily/enumerable.rb', line 8

def lazy?
  true
end

#prefetch(size) ⇒ Object



7
8
9
# File 'lib/lazily/prefetching.rb', line 7

def prefetch(size)
  Prefetcher.new(self, size)
end

#reject {|element| ... } ⇒ Enumerable

Select elements that fail a test.

Yields:

  • (element)

    each element

Yield Returns:

  • (Boolean)

    truthy if the element passed the test

Returns:

  • (Enumerable)

    the elements which failed the test

See Also:

  • Enumerable#reject


46
47
48
49
50
51
52
# File 'lib/lazily/filtering.rb', line 46

def reject
  filter("reject") do |yielder|
    each do |element|
      yielder.call(element) unless yield(element)
    end
  end
end

#select(&predicate) ⇒ Enumerable Also known as: find_all

Select elements using a predicate block.

Returns:

  • (Enumerable)

    the elements that pass the predicate

See Also:

  • Enumerable#select


28
29
30
31
32
33
34
# File 'lib/lazily/filtering.rb', line 28

def select(&predicate)
  filter("select") do |yielder|
    each do |element|
      yielder.call(element) if yield(element)
    end
  end
end

#slice_before(*args, &block) ⇒ Object



203
204
205
# File 'lib/lazily/filtering.rb', line 203

def slice_before(*args, &block)
  super.lazily
end

#take(n) ⇒ Enumerable

Select the first n elements.

Parameters:

  • n (Integer)

    the number of elements to take

Returns:

See Also:

  • Enumerable#take


84
85
86
87
88
89
90
91
92
93
# File 'lib/lazily/filtering.rb', line 84

def take(n)
  filter("take") do |yielder, all_done|
    if n > 0
      each_with_index do |element, index|
        yielder.call(element)
        throw all_done if index + 1 == n
      end
    end
  end
end

#take_while(&predicate) ⇒ Enumerable

Select elements while a predicate returns true.

Returns:

  • (Enumerable)

    all elements before the first that fails the predicate

See Also:

  • Enumerable#take_while


101
102
103
104
105
106
107
108
# File 'lib/lazily/filtering.rb', line 101

def take_while(&predicate)
  filter("take_while") do |yielder, all_done|
    each do |element|
      throw all_done unless yield(element)
      yielder.call(element)
    end
  end
end

#uniqEnumerable #uniq(&block) ⇒ Enumerable

Remove duplicate values.

Returns:

  • (Enumerable)

    elements which have not been previously encountered

See Also:

  • Enumerable#uniq


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lazily/filtering.rb', line 63

def uniq
  filter("uniq") do |yielder|
    seen = Set.new
    each do |element|
      key = if block_given?
        yield element
      else
        element
      end
      yielder.call(element) if seen.add?(key)
    end
  end
end

#zip(*others) ⇒ Object



15
16
17
# File 'lib/lazily/zipping.rb', line 15

def zip(*others)
  Lazily.zip(self, *others)
end