Module: Lazily

Defined in:
lib/lazily/proxy.rb,
lib/lazily/merging.rb,
lib/lazily/version.rb,
lib/lazily/zipping.rb,
lib/lazily/dequeuing.rb,
lib/lazily/filtering.rb,
lib/lazily/threading.rb,
lib/lazily/enumerable.rb,
lib/lazily/associating.rb,
lib/lazily/prefetching.rb,
lib/lazily/concatenating.rb

Defined Under Namespace

Modules: Enumerable Classes: Concatenator, Dequeuer, Filter, Merger, Prefetcher, Proxy, TaggedElement, Zipper

Constant Summary collapse

VERSION =
"0.2.1".freeze

Class Method Summary collapse

Class Method Details

.associate(source_map, &block) ⇒ Enumerable

Associate “like” elements of two or more Enumerables.

Examples:

Lazily.associate(a: [1,2,4], b: [1,3,4,4])
  #=> [
    { a: [1], b: [1] },
    { a: [2] },
    { b: [3] },
    { a: [4], b: [4, 4] }
  ]

Parameters:

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lazily/associating.rb', line 21

def associate(source_map, &block)
  labels = source_map.keys
  tagged_element_sources = source_map.map do |label, enumerable|
    enumerable.lazily.map do |value|
      key = block ? block.call(value) : value
      TaggedElement.new(key, value, label)
    end
  end
  tagged_elements = Lazily.merge(*tagged_element_sources) { |te| te.key }
  tagged_elements.chunk { |te| te.key }.map do |_, tagged_elements|
    association = {}
    labels.each { |label| association[label] = [] }
    tagged_elements.each do |te|
      association[te.label] << te.value
    end
    association
  end
end

.concat(*enumerables) ⇒ Enumerable

Concatenates two or more Enumerables.

Examples:

array1 = [1,4,5]
array2 = [2,3,6]
Lazily.concat(array1, array2)       #=> [1,4,5,2,3,6]

Parameters:

Returns:

  • (Enumerable)

    elements of all the enumerables



17
18
19
# File 'lib/lazily/concatenating.rb', line 17

def concat(*enumerables)
  Concatenator.new(enumerables)
end

.dequeue(queue) ⇒ Object



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

def dequeue(queue)
  Dequeuer.new(queue)
end

.merge(*enumerables, &block) ⇒ Enumerable

Merge a number of sorted Enumerables into a single sorted collection.

Draws elements from enumerables as appropriate, to preserve the sort order. An optional block, if provided, is used for comparison of elements. Otherwise, natural element order is used.

Examples:

array1 = [1,4,5]
array2 = [2,3,6]
Lazily.merge(array1, array2)        #=> [1,2,3,4,5,6]

Parameters:

Returns:



20
21
22
# File 'lib/lazily/merging.rb', line 20

def merge(*enumerables, &block)
  Merger.new(enumerables, &block)
end

.zip(*enumerables) ⇒ Object



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

def zip(*enumerables)
  Zipper.new(enumerables)
end