Module: DaruLite::Vector::Iterable
- Included in:
- DaruLite::Vector
- Defined in:
- lib/daru_lite/vector/iterable.rb
Instance Method Summary collapse
- #apply_method(method, keys: nil, by_position: true) ⇒ Object (also: #apply_method_on_sub_vector)
- #each(&block) ⇒ Object
- #each_index(&block) ⇒ Object
- #each_with_index(&block) ⇒ Object
- #map!(&block) ⇒ Object
-
#recode(dt = nil, &block) ⇒ Object
Like map, but returns a DaruLite::Vector with the returned values.
-
#recode!(dt = nil, &block) ⇒ Object
Destructive version of recode!.
-
#replace_values(old_values, new_value) ⇒ DaruLite::Vector
Replaces specified values with a new value.
-
#verify ⇒ Object
Reports all values that doesn’t comply with a condition.
Instance Method Details
#apply_method(method, keys: nil, by_position: true) ⇒ Object Also known as: apply_method_on_sub_vector
58 59 60 61 62 63 64 65 66 |
# File 'lib/daru_lite/vector/iterable.rb', line 58 def apply_method(method, keys: nil, by_position: true) vect = keys ? get_sub_vector(keys, by_position: by_position) : self case method when Symbol then vect.send(method) when Proc then method.call(vect) else raise end end |
#each(&block) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/daru_lite/vector/iterable.rb', line 4 def each(&block) return to_enum(:each) unless block @data.each(&block) self end |
#each_index(&block) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/daru_lite/vector/iterable.rb', line 11 def each_index(&block) return to_enum(:each_index) unless block @index.each(&block) self end |
#each_with_index(&block) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/daru_lite/vector/iterable.rb', line 18 def each_with_index(&block) return to_enum(:each_with_index) unless block @data.to_a.zip(@index.to_a).each(&block) self end |
#map!(&block) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/daru_lite/vector/iterable.rb', line 26 def map!(&block) return to_enum(:map!) unless block @data.map!(&block) self end |
#recode(dt = nil, &block) ⇒ Object
Like map, but returns a DaruLite::Vector with the returned values.
34 35 36 37 38 |
# File 'lib/daru_lite/vector/iterable.rb', line 34 def recode(dt = nil, &block) return to_enum(:recode, dt) unless block dup.recode! dt, &block end |
#recode!(dt = nil, &block) ⇒ Object
Destructive version of recode!
41 42 43 44 45 46 47 |
# File 'lib/daru_lite/vector/iterable.rb', line 41 def recode!(dt = nil, &block) return to_enum(:recode!, dt) unless block @data.map!(&block).data @data = cast_vector_to(dt || @dtype) self end |
#replace_values(old_values, new_value) ⇒ DaruLite::Vector
Note:
It performs the replace in place.
Replaces specified values with a new value
86 87 88 89 90 91 92 |
# File 'lib/daru_lite/vector/iterable.rb', line 86 def replace_values(old_values, new_value) old_values = [old_values] unless old_values.is_a? Array size.times do |pos| set_at([pos], new_value) if include_with_nan? old_values, at(pos) end self end |
#verify ⇒ Object
Reports all values that doesn’t comply with a condition. Returns a hash with the index of data and the invalid data.
51 52 53 54 55 56 |
# File 'lib/daru_lite/vector/iterable.rb', line 51 def verify (0...size) .map { |i| [i, @data[i]] } .reject { |_i, val| yield(val) } .to_h end |