Class: J8::Stream
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#all_match?(predicate = nil, &block) ⇒ Boolean
-
#any_match?(predicate = nil, &block) ⇒ Boolean
-
#collect ⇒ Object
-
#count ⇒ Object
-
#each(consumer = nil, &block) ⇒ Object
-
#filter(predicate = nil, &block) ⇒ Object
-
#find_first ⇒ Object
(also: #find_any)
-
#flat_map(function = nil, &block) ⇒ Object
-
#initialize(enumerator) ⇒ Stream
constructor
A new instance of Stream.
-
#limit(size) ⇒ Object
-
#map(function = nil, &block) ⇒ Object
-
#max(comparator = nil, &block) ⇒ Object
-
#min(comparator = nil, &block) ⇒ Object
-
#none_match?(predicate = nil, &block) ⇒ Boolean
-
#peek(consumer = nil, &block) ⇒ Object
-
#reduce(binary_operator = nil, memo: nil, &block) ⇒ Object
-
#skip(amount) ⇒ Object
-
#sort ⇒ Object
-
#sort_by(comparator = nil, &block) ⇒ Object
-
#to_a ⇒ Object
Methods included from Common
callable_from_proc, from_callable, from_callable_class, initialize, lambda?, make_lambda, raise_unless_lambda
Constructor Details
#initialize(enumerator) ⇒ Stream
Returns a new instance of Stream.
9
10
11
|
# File 'lib/j8/stream.rb', line 9
def initialize(enumerator)
@enumerator = enumerator
end
|
Instance Attribute Details
#enumerator ⇒ Object
Returns the value of attribute enumerator.
7
8
9
|
# File 'lib/j8/stream.rb', line 7
def enumerator
@enumerator
end
|
Class Method Details
.concat(stream1, stream2) ⇒ Object
13
14
15
16
17
18
19
20
21
|
# File 'lib/j8/stream.rb', line 13
def self.concat(stream1, stream2)
J8::Stream.new(
Enumerator.new do |enumerator|
[stream1, stream2].each do |stream|
stream.enumerator.each { |o| enumerator << o }
end
end
)
end
|
23
24
25
|
# File 'lib/j8/stream.rb', line 23
def self.empty
[].j8_stream
end
|
.generate(supplier = nil, &block) ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/j8/stream.rb', line 27
def self.generate(supplier = nil, &block)
callable = J8::Common.from_callable_class(supplier, block, J8::Supplier)
J8::Stream.new(
Enumerator.new do |enumerator|
loop { enumerator << callable.get }
end
)
end
|
.iterate(seed, function = nil, &block) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/j8/stream.rb', line 37
def self.iterate(seed, function = nil, &block)
callable = J8::Common.from_callable_class(function, block, J8::Function)
J8::Stream.new(
Enumerator.new do |enumerator|
loop { enumerator << callable.apply(seed) }
end
)
end
|
.of(*values) ⇒ Object
47
48
49
|
# File 'lib/j8/stream.rb', line 47
def self.of(*values)
values.j8_stream
end
|
Instance Method Details
#all_match?(predicate = nil, &block) ⇒ Boolean
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/j8/stream.rb', line 51
def all_match?(predicate = nil, &block)
callable = from_callable_class(predicate, block, J8::Predicate)
ran = false
ret = @enumerator.all? do |o|
ran = true
callable.test(o) == true
end
ran ? ret : false
end
|
#any_match?(predicate = nil, &block) ⇒ Boolean
66
67
68
69
70
|
# File 'lib/j8/stream.rb', line 66
def any_match?(predicate = nil, &block)
callable = from_callable_class(predicate, block, J8::Predicate)
@enumerator.any? { |o| callable.test(o) }
end
|
72
73
74
|
# File 'lib/j8/stream.rb', line 72
def collect
end
|
76
77
78
|
# File 'lib/j8/stream.rb', line 76
def count
@enumerator.count
end
|
#each(consumer = nil, &block) ⇒ Object
97
98
99
100
101
|
# File 'lib/j8/stream.rb', line 97
def each(consumer = nil, &block)
callable = from_callable_class(consumer, block, J8::Consumer)
@enumerator.each { |n| callable.accept(n) }
end
|
#filter(predicate = nil, &block) ⇒ Object
80
81
82
83
84
|
# File 'lib/j8/stream.rb', line 80
def filter(predicate = nil, &block)
callable = from_callable_class(predicate, block, J8::Predicate)
@enumerator.select { |n| callable.test(n) }.j8_stream
end
|
#find_first ⇒ Object
Also known as:
find_any
86
87
88
|
# File 'lib/j8/stream.rb', line 86
def find_first
J8::Optional.new(@enumerator.first)
end
|
#flat_map(function = nil, &block) ⇒ Object
91
92
93
94
95
|
# File 'lib/j8/stream.rb', line 91
def flat_map(function = nil, &block)
callable = from_callable_class(function, block, J8::Function)
@enumerator.map { |n| callable.apply(n) }.flatten.j8_stream
end
|
#limit(size) ⇒ Object
103
104
105
|
# File 'lib/j8/stream.rb', line 103
def limit(size)
@enumerator.first(size).j8_stream
end
|
#map(function = nil, &block) ⇒ Object
107
108
109
110
111
|
# File 'lib/j8/stream.rb', line 107
def map(function = nil, &block)
callable = from_callable_class(function, block, J8::Function)
@enumerator.map { |object| callable.apply(object) }.j8_stream
end
|
#max(comparator = nil, &block) ⇒ Object
113
114
115
116
117
|
# File 'lib/j8/stream.rb', line 113
def max(comparator = nil, &block)
callable = from_callable_class(comparator, block, J8::Comparator)
@enumerator.max { |a, b| callable.compare(a, b) }
end
|
#min(comparator = nil, &block) ⇒ Object
119
120
121
122
123
|
# File 'lib/j8/stream.rb', line 119
def min(comparator = nil, &block)
callable = from_callable_class(comparator, block, J8::Comparator)
@enumerator.min { |a, b| callable.compare(a, b) }
end
|
#none_match?(predicate = nil, &block) ⇒ Boolean
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/j8/stream.rb', line 125
def none_match?(predicate = nil, &block)
callable = from_callable_class(predicate, block, J8::Predicate)
ran = false
ret = @enumerator.all? do |object|
ran = true
callable.test(object) != true
end
ran ? ret : false
end
|
#peek(consumer = nil, &block) ⇒ Object
140
141
142
143
144
145
146
147
|
# File 'lib/j8/stream.rb', line 140
def peek(consumer = nil, &block)
callable = from_callable_class(consumer, block, J8::Consumer)
@enumerator.entries.tap do |entries|
entries.each { |v| callable.accept(v) }
@enumerator.rewind
end.j8_stream
end
|
#reduce(binary_operator = nil, memo: nil, &block) ⇒ Object
149
150
151
152
153
154
155
156
157
|
# File 'lib/j8/stream.rb', line 149
def reduce(binary_operator = nil, memo: nil, &block)
callable = from_callable_class(binary_operator, block, J8::BinaryOperator)
J8::Optional.of(
@enumerator.reduce(memo) do |accumulator, object|
callable.apply(accumulator, object)
end
)
end
|
#skip(amount) ⇒ Object
159
160
161
|
# File 'lib/j8/stream.rb', line 159
def skip(amount)
@enumerator.slice_after(amount).to_a.last.j8_stream
end
|
163
164
165
|
# File 'lib/j8/stream.rb', line 163
def sort
@enumerator.sort.j8_stream
end
|
#sort_by(comparator = nil, &block) ⇒ Object
167
168
169
170
171
|
# File 'lib/j8/stream.rb', line 167
def sort_by(comparator = nil, &block)
callable = from_callable_class(comparator, block, J8::Comparator)
@enumerator.sort { |a, b| callable.compare(a, b) }.j8_stream
end
|
173
174
175
|
# File 'lib/j8/stream.rb', line 173
def to_a
@enumerator.to_a
end
|