Module: LazyList::Enumerable

Includes:
Enumerable
Included in:
LazyList
Defined in:
lib/lazylist/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#combine(other, &operator) ⇒ Object

obsoleted by #zip



57
58
59
60
# File 'lib/lazylist/enumerable.rb', line 57

def combine(other, &operator)
  warn "method 'combine' is obsolete - use 'zip'"
  zip(other, &operator)
end

#each_with_index(&block) ⇒ Object

Calls block with two arguments, the element and its index, for each element of this lazy list. If block isn’t given, the method returns a lazy list that consists of [ element, index ] pairs instead.



29
30
31
32
33
34
35
36
# File 'lib/lazylist/enumerable.rb', line 29

def each_with_index(&block)
  if block
    each_with_index.each { |x| block[x] }
  else
    i = -1
    map { |x| [ x, i += 1 ] }
  end
end

#filter(&p) ⇒ Object

obsoleted by #select



92
93
94
95
# File 'lib/lazylist/enumerable.rb', line 92

def filter(&p)
  warn "method 'filter' is obsolete - use 'select'"
  select(&p)
end

#grep(pattern, &block) ⇒ Object

Returns a lazy list every element of this lazy list for which pattern === element is true. If the optional block is supplied, each matching element is passed to it, and the block’s result becomes an element of the returned lazy list.



66
67
68
69
70
# File 'lib/lazylist/enumerable.rb', line 66

def grep(pattern, &block)
  result = select { |x| pattern === x }
  block and result = result.map(&block)
  result
end

#map(&f) ⇒ Object Also known as: collect

Creates a new Lazylist that maps the block or Proc object f to every element in the old list.



99
100
101
102
103
# File 'lib/lazylist/enumerable.rb', line 99

def map(&f)
  return empty if empty?
  f = Identity unless f
  self.class.new(delay { f[head] }) { tail.map(&f) }
end

#mapper(&f) ⇒ Object

obsoleted by #map



107
108
109
110
# File 'lib/lazylist/enumerable.rb', line 107

def mapper(&f)
  warn "method 'mapper' is obsolete - use 'map'"
  map(&f)
end

#partition(&block) ⇒ Object

Returns two lazy lists, the first containing the elements of this lazy list for which the block evaluates to true, the second containing the rest.



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

def partition(&block)
  return select(&block), reject(&block)
end

#rejectObject

Returns a lazy list of all elements of this lazy list for which the block is false (see also Lazylist#select).



74
75
76
# File 'lib/lazylist/enumerable.rb', line 74

def reject
  select { |obj| !yield(obj) }
end

#select(&block) ⇒ Object Also known as: find_all

Returns a lazy list of all elements of this lazy list for which block is true.



80
81
82
83
84
85
86
87
88
# File 'lib/lazylist/enumerable.rb', line 80

def select(&block)
  block = All unless block
  s = self
  until s.empty? or block[s.head]
    s = s.tail
  end
  return empty if s.empty?
  self.class.new(delay { s.head }) { s.tail.select(&block) }
end

#sortObject

Returns a sorted version of this lazy list. This method should only be called on finite lazy lists or it will never return. Also see Enumerable#sort.



15
16
17
# File 'lib/lazylist/enumerable.rb', line 15

def sort # :yields: a, b
  LazyList.from_enum(super)
end

#sort_byObject

Returns a sorted version of this lazy list. This method should only be called on finite lazy lists or it will never return. Also see Enumerable#sort_by.



22
23
24
# File 'lib/lazylist/enumerable.rb', line 22

def sort_by # :yields: obj
  LazyList.from_enum(super)
end

#zip(*others, &block) ⇒ Object

Returns the lazy list, that contains all the given block’s return values, if it was called on every

self[i], others[0][i], others[1][i],... others[others.size - 1][i]

for i in 0..Infinity. If block wasn’t given this result will be the array

[self[i], others[0][i], others[1][i],... ]

and a lazy list of those arrays is returned.



45
46
47
48
49
50
51
52
53
54
# File 'lib/lazylist/enumerable.rb', line 45

def zip(*others, &block)
  if empty? or others.any? { |o| o.empty? }
    empty
  else
    block ||= Tuple
    self.class.new(delay { block[head, *others.map { |o| o.head }] }) do
      tail.zip(*others.map { |o| o.tail }, &block)
    end
  end
end