Class: Stream::FromArray
- Inherits:
-
Object
- Object
- Stream::FromArray
- Defined in:
- lib/fromarray.rb
Overview
A stream implemented based on an Array. This class is immutable and thread-safe.
- Author
-
Mihai Andronache ([email protected])
- Since
-
0.0.1
Instance Method Summary collapse
-
#all_match(&test) ⇒ Object
Returns true if all the elements of the Stream are matching the given predicate (a function which performs a test on the value and should return a boolean).
-
#any_match(&test) ⇒ Object
Returns true if any of the elements of the Stream are matching the given predicate (a function which performs a test on the value and should return a boolean).
-
#collect ⇒ Object
Collect the stream’s data into an array and return it.
-
#count ⇒ Object
Return the number of elements in this stream.
-
#distinct ⇒ Object
Remove all the duplicates from the stream.
-
#filter(&condition) ⇒ Object
Filter out the elements which are not satisfying the given condition.
-
#initialize(array) ⇒ FromArray
constructor
A new instance of FromArray.
-
#map(&function) ⇒ Object
Map the stream’s elements to a given value using a function.
-
#skip(count) ⇒ Object
Skip the first n elements of the stream.
Constructor Details
#initialize(array) ⇒ FromArray
Returns a new instance of FromArray.
31 32 33 |
# File 'lib/fromarray.rb', line 31 def initialize(array) @array = array end |
Instance Method Details
#all_match(&test) ⇒ Object
Returns true if all the elements of the Stream are matching the given predicate (a function which performs a test on the value and should return a boolean).
If the stream is empty, the returned value is true and the predicate is not called at all.
This is a terminal operation.
test
-
A function which should perform some boolean test on the
given value.
110 111 112 113 114 115 |
# File 'lib/fromarray.rb', line 110 def all_match(&test) @array.each do |val| return false unless test.call(val) end true end |
#any_match(&test) ⇒ Object
Returns true if any of the elements of the Stream are matching the given predicate (a function which performs a test on the value and should return a boolean). Iteration will stop at the first match.
If the stream is empty, the returned value is false and the predicate is not called at all.
This is a terminal operation.
test
-
A function which should perform some boolean test on the
given value.
128 129 130 131 132 133 |
# File 'lib/fromarray.rb', line 128 def any_match(&test) @array.each do |val| return true if test.call(val) end false end |
#collect ⇒ Object
Collect the stream’s data into an array and return it. This is a terminal operation.
137 138 139 |
# File 'lib/fromarray.rb', line 137 def collect @array.dup end |
#count ⇒ Object
Return the number of elements in this stream. This is a terminal operation.
37 38 39 |
# File 'lib/fromarray.rb', line 37 def count @array.length end |
#distinct ⇒ Object
Remove all the duplicates from the stream. This is an intermediary operation.
79 80 81 82 83 84 85 |
# File 'lib/fromarray.rb', line 79 def distinct unique = [] @array.each do |val| unique.push(val) unless unique.include? val end FromArray.new(unique) end |
#filter(&condition) ⇒ Object
Filter out the elements which are not satisfying the given condition. Example:
stream = Stream::FromArray.new([1, 2, 3, 4, 5]) collected = stream.filter {|num| num % 2 == 0}.collect puts collected # [2, 4]
This is an intermediary operation.
condition
-
Ruby Block taking one parameter (the stream element) and returning a boolean check on it.
51 52 53 54 55 56 57 |
# File 'lib/fromarray.rb', line 51 def filter(&condition) filtered = [] @array.each do |val| filtered.push(val) unless condition.call(val) == false end FromArray.new(filtered) end |
#map(&function) ⇒ Object
Map the stream’s elements to a given value using a function. Example (map int to string):
stream = Stream::FromArray.new([1, 2, 3]) collected = stream.map {|num| num.to_s}.collect puts collected # [‘1’, ‘2’, ‘3’]
This is an intermediary operation.
function
-
Ruby Block function taking one parameter (the element in the stream).
69 70 71 72 73 74 75 |
# File 'lib/fromarray.rb', line 69 def map(&function) mapped = [] @array.each do |val| mapped.push(function.call(val)) end FromArray.new(mapped) end |
#skip(count) ⇒ Object
Skip the first n elements of the stream. This is an intermediary operation.
count
-
Number of elements to skip from the beginning of the stream.
90 91 92 93 94 95 96 97 98 |
# File 'lib/fromarray.rb', line 90 def skip(count) raise ArgumentError, 'count has to be positive integer' unless count.positive? and count.is_a? Integer skipped = [] @array.each_with_index do |val, index| skipped.push(val) unless index + 1 <= count end FromArray.new(skipped) end |