Module: Oscillo::Enumerable

Included in:
Signal
Defined in:
lib/oscillo/enumerable.rb

Overview

Adds enumerable-like methods to Signal.

Instance Method Summary collapse

Instance Method Details

#all? {|new_value| ... } ⇒ Signal<Boolean>

Note:

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.)

Yields:

  • (new_value)

    give the new value of the signal to the block

Returns:

See Also:



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>

Note:

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.)

Yields:

  • (new_value)

    give the new value of the signal to the block

Returns:

See Also:



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.

Yields:

  • (*signals)

    gives the new values of the signals to the block

Returns:



12
13
14
# File 'lib/oscillo/enumerable.rb', line 12

def collect(&block)
  self.class.new(self) { |*v, s| block.(*v) }
end

#countObject #count(obj) ⇒ Object #count {|value| ... } ⇒ Object

Note:

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.

Overloads:

  • #countObject

    Returns number of values that the signal had.

    Returns:

    • number of values that the signal had

  • #count(obj) ⇒ Object

    Returns number of values equal to obj that the signal had.

    Parameters:

    • obj

      values to count

    Returns:

    • number of values equal to obj that the signal had

  • #count {|value| ... } ⇒ Object

    Returns number of values for which the block evaluates to true.

    Yields:

    • (value)

      gives the new value to the block

    Returns:

    • number of values for which the block evaluates to true



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

Yields:

  • (value)

    gives the new value to the block

Returns:



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.

Yields:

  • (value)

    gives the new value of the signal to the block

Returns:

See Also:



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.

Overloads:

  • #inject {|memo, value| ... } ⇒ Signal

    Yields:

    • (memo, value)

      gives the memo and the new value of the signal to the block

  • #inject(symbol) ⇒ Signal

    Parameters:

    • symbol

      that represents method to call on memo

  • #inject(initial) {|memo, value| ... } ⇒ Signal

    Parameters:

    • initial

      the start value of the memo

    Yields:

    • (memo, value)

      gives the memo and the new value of the signal to the block

  • #inject(initial, symbol) ⇒ Signal

    Parameters:

    • initial

      the start value of the memo

    • symbol

      that represents method to call on memo

Returns:



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.

Yields:

  • (value)

    give the new value of the signal to the block

Returns:

See Also:



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.

Parameters:

  • signals ([Signal])

    the other signals to merge in the array

Returns:



82
83
84
# File 'lib/oscillo/enumerable.rb', line 82

def zip(*signals)
  self.class.new(self, *signals) {|*v, s| v }
end