Module: Oscillo::Enumerable
- Included in:
- Signal
- Defined in:
- lib/oscillo/enumerable.rb
Overview
Adds enumerable-like methods to Signal.
Instance Method Summary collapse
-
#all? {|new_value| ... } ⇒ Signal<Boolean>
Passes each value of the signal to the given block.
-
#any? {|new_value| ... } ⇒ Signal<Boolean>
Passes each value of the signal to the given block.
-
#collect {|*signals| ... } ⇒ Signal
(also: #map)
Returns a signal that is the result of running the values of the original signal through the block.
-
#count(obj = NO_ARG, &block) ⇒ Object
Returns the number values that the signal has had.
-
#each {|value| ... } ⇒ Signal
Equivalent to Signal#on_change.
-
#find_all {|value| ... } ⇒ Signal
(also: #select)
Returns a signal that changes only when the original signal changes and the block is not false.
-
#inject(*args, &block) ⇒ Signal
(also: #reduce)
Combines all values of the signal by applying a binary operation, specified by a block or a symbol that names a method or operator.
-
#reject {|value| ... } ⇒ Signal
Returns a signal that changes only when the original signal changes and the block is false.
-
#zip(*signals) ⇒ Signal
Take the value from this signal and merges it with the values of the given signals to form an array.
Instance Method Details
#all? {|new_value| ... } ⇒ Signal<Boolean>
Only takes in to consideration values from the time the signal was created.
Passes each value of the signal to the given block. The value of the resulting signal is true if the block never returns false or nil. If the block is not given, uses an implicit block of {|value| value} (that is #all? will return true only if none of the signal values were false or nil.)
53 54 55 56 57 58 |
# File 'lib/oscillo/enumerable.rb', line 53 def all?(&block) self.class.new(true, self) do |v, s| v = block.(v) if block_given? s.val & v end end |
#any? {|new_value| ... } ⇒ Signal<Boolean>
Only takes in to consideration values from the time the signal was created.
Passes each value of the signal to the given block. The value of the resulting signal is true if the block ever returns a value other than false or nil. If the block is not given, uses an implicit block of {|value| value} (that is #any? will return true if at least one of the signal values were not false or nil.)
71 72 73 74 75 |
# File 'lib/oscillo/enumerable.rb', line 71 def any?(&block) self.class.new(false, self) do |v, s| s.val | block.(v) end end |
#collect {|*signals| ... } ⇒ Signal Also known as: map
Returns a signal that is the result of running the values of the original signal through the block.
12 13 14 |
# File 'lib/oscillo/enumerable.rb', line 12 def collect(&block) self.class.new(self) { |*v, s| block.(*v) } end |
#count ⇒ Object #count(obj) ⇒ Object #count {|value| ... } ⇒ Object
Only takes in to consideration values from the time the signal was created.
Returns the number values that the signal has had. If an argument is given, counts the number of values in the signal, for which equals to item. If a block is given, counts the number of values yielding a true value.
109 110 111 112 113 114 115 116 |
# File 'lib/oscillo/enumerable.rb', line 109 def count(obj=NO_ARG, &block) return count_with_arg(obj) unless obj.equal?(NO_ARG) return count_with_block(&block) if block_given? self.class.new(0, self) do |v, s| s.val + 1 end end |
#each {|value| ... } ⇒ Signal
Equivalent to Signal#on_change
90 91 92 |
# File 'lib/oscillo/enumerable.rb', line 90 def each(&block) self.on_change(&block) end |
#find_all {|value| ... } ⇒ Signal Also known as: select
Returns a signal that changes only when the original signal changes and the block is not false.
23 24 25 26 27 |
# File 'lib/oscillo/enumerable.rb', line 23 def find_all(&block) self.class.new(self) do |v, s| s.abort unless block.(v); v end end |
#inject {|memo, value| ... } ⇒ Signal #inject(symbol) ⇒ Signal #inject(initial) {|memo, value| ... } ⇒ Signal #inject(initial, symbol) ⇒ Signal Also known as: reduce
Combines all values of the signal by applying a binary operation, specified by a block or a symbol that names a method or operator. If you specify a block, then for each value in this signal the block is passed an accumulator value (memo) the new value. If you specify a symbol instead, then each new value will be passed to the named method of memo. In either case, the result becomes the new value for memo.
138 139 140 141 142 143 144 |
# File 'lib/oscillo/enumerable.rb', line 138 def inject(*args, &block) if block_given? inject_with_block(*args, &block) else inject_with_sym(*args) end end |
#reject {|value| ... } ⇒ Signal
Returns a signal that changes only when the original signal changes and the block is false.
36 37 38 39 40 |
# File 'lib/oscillo/enumerable.rb', line 36 def reject(&block) self.class.new(self) do |v, s| s.abort if block.(v); v end end |
#zip(*signals) ⇒ Signal
Take the value from this signal and merges it with the values of the given signals to form an array.
82 83 84 |
# File 'lib/oscillo/enumerable.rb', line 82 def zip(*signals) self.class.new(self, *signals) {|*v, s| v } end |