Assign to an array and replace emerged nil values - ruby

Greetings!
When assigning a value to an array as in the following, how could I replace the nils by 0?
array = [1,2,3]
array[10] = 2
array # => [1, 2, 3, nil, nil, nil, nil, nil, nil, nil, 2]
If not possible when assigning, how would I do it the best way afterwards? I thought of array.map { |e| e.nil? ? 0 : e }, but well…
Thanks!

To change the array after assignment:
array.map! { |x| x || 0 }
Note that this also converts false to 0.
If you want to use zeros during assignment, it's a little messy:
i = 10
a = [1, 2, 3]
a += ([0] * (i - a.size)) << 2
# => [1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 2]

There is no built-in function to replace nil in an array, so yes, map is the way to go. If a shorter version would make you happier, you could do:
array.map {|e| e ? e : 0}

nil.to_i is 0, if all the numbers are integers then below should work. I think It is also the shortest answer here.
array.map!(&:to_i)

To change the array in place
array.map!{|x|x ?x:0}
If the array can contain false you'll need to use this instead
array.map!{|x|x.nil? ? 0:x}

a.select { |i| i }
This answer is too short so I am adding a few more words.

Another approach would be to define your own function for adding a value to the array.
class Array
def addpad(index,newval)
concat(Array.new(index-size,0)) if index > size
self[index] = newval
end
end
a = [1,2,3]
a.addpad(10,2)
a => [1,2,3,0,0,0,0,0,0,0,2]

Related

sum of corresponding elements of array including nil

How can I sum in Ruby
[1, 2, nil, 4]
with
[nil, 2, nil, 4]
and have
[1, 4, nil, 8]
?
For example as follows:
a = [1, 2, nil, 4]
b = [nil, 2, nil, 4]
a.map.with_index {|v,i| (v || b[i]) && v.to_i + b[i].to_i }
More estetic way is:
a.zip(b).map {|v| v.compact.reduce(:+) }
Explanation: Here #zip just reconstructs the array with other passed value-by-value, so in 1st row will consist of 1st elements of each array, 2nd of 2nds, etc. Then #compact, and #reduce is applied of each of rows 1st, 2nd, etc. #compact just removes nil value, so they do not included in result. #reduce then construct a sum for each row, via :+ operator between sum (by default it is nil), and value, so in output it resulted in the sum of values, of nil in case empty row.
If we'll get the more general approach, let's sum rows of the matrix.
m = [ [ 1, 2, nil, 4],
[nil, 2, nil, 4] ]
m.shift.zip(*m).map {|v| v.compact.reduce(:+) }
a.zip(b).map {|x, y| x.nil? ? (y.nil? ? nil : y) : (y.nil? ? x : x + y)}
# => [1, 4, nil, 8]
Here's a way that makes use of the fact that nil.to_i => O:
a.zip(b).map { |e,f| [e,f]==[nil,nil] ? nil : e.to_i + f.to_i }

Ruby check array, return the indexes, which data is exist

How can I check if there's a data that not nil in an array, and then return the index of that data?
Example:
myary = [nil, nil, 300, nil, nil] # <= index 2 is 300
now is there a method to get the value 2? As we know the index 2 is 300 and not nil.
I need to get the index not the value. And moreover there probably will ot only one element that is not nil, perhaps the array could be like this
myotherary = [nil, nil, 300, 400, nil] # <= index 2,3 = 300,400
now for this I need to get 2 and 3 value, is this posibble?
Okay thank you very much, I appreciate all answer.
P.S : Please no flaming, if you don't want to help then just leave, I have spent some time to solve this matter and not succeed. I'm not going to ask here if I can solve it by myself. I had enough of them who not helping, instead asking "what method have you tried?" or write something else that actually not helping but harrasing.
You can use map.with_index:
myary.map.with_index { |v, i| i if v }.compact
# => [2]
myotherary.map.with_index { |v, i| i if v }.compact
# => [2, 3]
I would be inclined to use Enumerable#select in part because it reads well; the word "select" describes what you want to do.
Code
For just the indices:
def indices_only(arr)
arr.size.times.select { |i| arr[i] }
end
If it would be more useful to return both non-nil values and corresponding indices:
def values_and_indices(arr)
arr.each_with_index.select(&:first)
end
Examples
arr1 = [nil, nil, 300, nil, nil]
arr2 = [nil, nil, 300, 400, nil]
indices_only(arr1) #=> [2]
indices_only(arr2) #=> [2, 3]
values_and_indices(arr1) #=> [[300, 2]]
values_and_indices(arr2) #=> [[300, 2], [400, 3]]

undefined method `>' for nil:NilClass in ruby array

c = [1, 2, 3, 4, 5, 6, 7, nil, nil, nil]
c.map { |i| p i if i > 10 }
NoMethodError: undefined method `>' for nil:NilClass
How to avoid nil values during comparison?
Try as below :
c.each { |i| p i if i.to_i > 10 } # fix is to use `to_i`
Use each method instead of map, when you want to traverse the array, and outputs some elements when there is a match, as you are doing. If you use the method to_i, then whenever there is nil in your source array, then nil.to_i will convert it to 0. And if it found any integers like 1,2 etc, then you will get back the same integers.
Look also NilClass#to_i and Fixnum#to_i methods.
There are a couple ways.
Use Array.compact:
c.compact.each { |i| p i if i > 10 }
The Array#compact method gives you a new array with all nils removed.
Use next:
c.each do |i|
next if i.nil?
p i if i > 10
end
This avoids creating a new array.
Note the use of each instead of map. The latter is used when you are creating a new array with the result of applying the block to each element in the original array, such as c.map { |i| i + 1 }, which is equivalent to c.map &:succ.
If all you need to do is iterate over the array and apply side effects such as printing numbers, each should suffice.
Try this one:
c = [1, 2, 3, 4, 5, 6, 7, nil, nil, nil]
c.compact.each{|i| p i if i > 10}
c.each { |i| p i if !i.nil? && i > 10 }

How to map and remove nil values in Ruby

I have a map which either changes a value or sets it to nil. I then want to remove the nil entries from the list. The list doesn't need to be kept.
This is what I currently have:
# A simple example function, which returns a value or nil
def transform(n)
rand > 0.5 ? n * 10 : nil }
end
items.map! { |x| transform(x) } # [1, 2, 3, 4, 5] => [10, nil, 30, 40, nil]
items.reject! { |x| x.nil? } # [10, nil, 30, 40, nil] => [10, 30, 40]
I'm aware I could just do a loop and conditionally collect in another array like this:
new_items = []
items.each do |x|
x = transform(x)
new_items.append(x) unless x.nil?
end
items = new_items
But it doesn't seem that idiomatic. Is there a nice way to map a function over a list, removing/excluding the nils as you go?
You could use compact:
[1, nil, 3, nil, nil].compact
=> [1, 3]
I'd like to remind people that if you're getting an array containing nils as the output of a map block, and that block tries to conditionally return values, then you've got code smell and need to rethink your logic.
For instance, if you're doing something that does this:
[1,2,3].map{ |i|
if i % 2 == 0
i
end
}
# => [nil, 2, nil]
Then don't. Instead, prior to the map, reject the stuff you don't want or select what you do want:
[1,2,3].select{ |i| i % 2 == 0 }.map{ |i|
i
}
# => [2]
I consider using compact to clean up a mess as a last-ditch effort to get rid of things we didn't handle correctly, usually because we didn't know what was coming at us. We should always know what sort of data is being thrown around in our program; Unexpected/unknown data is bad. Anytime I see nils in an array I'm working on, I dig into why they exist, and see if I can improve the code generating the array, rather than allow Ruby to waste time and memory generating nils then sifting through the array to remove them later.
'Just my $%0.2f.' % [2.to_f/100]
Try using reduce or inject.
[1, 2, 3].reduce([]) { |memo, i|
if i % 2 == 0
memo << i
end
memo
}
I agree with the accepted answer that we shouldn't map and compact, but not for the same reasons.
I feel deep inside that map then compact is equivalent to select then map. Consider: map is a one-to-one function. If you are mapping from some set of values, and you map, then you want one value in the output set for each value in the input set. If you are having to select before-hand, then you probably don't want a map on the set. If you are having to select afterwards (or compact) then you probably don't want a map on the set. In either case you are iterating twice over the entire set, when a reduce only needs to go once.
Also, in English, you are trying to "reduce a set of integers into a set of even integers".
Ruby 2.7+
There is now!
Ruby 2.7 is introducing filter_map for this exact purpose. It's idiomatic and performant, and I'd expect it to become the norm very soon.
For example:
numbers = [1, 2, 5, 8, 10, 13]
enum.filter_map { |i| i * 2 if i.even? }
# => [4, 16, 20]
In your case, as the block evaluates to falsey, simply:
items.filter_map { |x| process_x url }
"Ruby 2.7 adds Enumerable#filter_map" is a good read on the subject, with some performance benchmarks against some of the earlier approaches to this problem:
N = 100_000
enum = 1.upto(1_000)
Benchmark.bmbm do |x|
x.report("select + map") { N.times { enum.select { |i| i.even? }.map{ |i| i + 1 } } }
x.report("map + compact") { N.times { enum.map { |i| i + 1 if i.even? }.compact } }
x.report("filter_map") { N.times { enum.filter_map { |i| i + 1 if i.even? } } }
end
# Rehearsal -------------------------------------------------
# select + map 8.569651 0.051319 8.620970 ( 8.632449)
# map + compact 7.392666 0.133964 7.526630 ( 7.538013)
# filter_map 6.923772 0.022314 6.946086 ( 6.956135)
# --------------------------------------- total: 23.093686sec
#
# user system total real
# select + map 8.550637 0.033190 8.583827 ( 8.597627)
# map + compact 7.263667 0.131180 7.394847 ( 7.405570)
# filter_map 6.761388 0.018223 6.779611 ( 6.790559)
Definitely compact is the best approach for solving this task. However, we can achieve the same result just with a simple subtraction:
[1, nil, 3, nil, nil] - [nil]
=> [1, 3]
In your example:
items.map! { |x| process_x url } # [1, 2, 3, 4, 5] => [1, nil, 3, nil, nil]
it does not look like the values have changed other than being replaced with nil. If that is the case, then:
items.select{|x| process_x url}
will suffice.
If you wanted a looser criterion for rejection, for example, to reject empty strings as well as nil, you could use:
[1, nil, 3, 0, ''].reject(&:blank?)
=> [1, 3, 0]
If you wanted to go further and reject zero values (or apply more complex logic to the process), you could pass a block to reject:
[1, nil, 3, 0, ''].reject do |value| value.blank? || value==0 end
=> [1, 3]
[1, nil, 3, 0, '', 1000].reject do |value| value.blank? || value==0 || value>10 end
=> [1, 3]
You can use #compact method on the resulting array.
[10, nil, 30, 40, nil].compact => [10, 30, 40]
each_with_object is probably the cleanest way to go here:
new_items = items.each_with_object([]) do |x, memo|
ret = process_x(x)
memo << ret unless ret.nil?
end
In my opinion, each_with_object is better than inject/reduce in conditional cases because you don't have to worry about the return value of the block.
One more way to accomplish it will be as shown below. Here, we use Enumerable#each_with_object to collect values, and make use of Object#tap to get rid of temporary variable that is otherwise needed for nil check on result of process_x method.
items.each_with_object([]) {|x, obj| (process x).tap {|r| obj << r unless r.nil?}}
Complete example for illustration:
items = [1,2,3,4,5]
def process x
rand(10) > 5 ? nil : x
end
items.each_with_object([]) {|x, obj| (process x).tap {|r| obj << r unless r.nil?}}
Alternate approach:
By looking at the method you are calling process_x url, it is not clear what is the purpose of input x in that method. If I assume that you are going to process the value of x by passing it some url and determine which of the xs really get processed into valid non-nil results - then, may be Enumerabble.group_by is a better option than Enumerable#map.
h = items.group_by {|x| (process x).nil? ? "Bad" : "Good"}
#=> {"Bad"=>[1, 2], "Good"=>[3, 4, 5]}
h["Good"]
#=> [3,4,5]

How to interpolate an array?

I would like to do something like join with an Array, but instead of getting the result as a String, I would like to get an Array. I will call this interpolate. For example, given:
a = [1, 2, 3, 4, 5]
I expect:
a.interpolate(0) # => [1, 0, 2, 0, 3, 0, 4, 0, 5]
a.interpolate{Array.new} # => [1, [], 2, [], 3, [], 4, [], 5]
What is the best way to get this? The reason I need it to take a block is because when I use it with a block, I want different instances for each interpolator that comes in between.
After getting great answers from many, I came up with some modified ones.
This one is a modification from tokland's answer. I made it accept nil for conj1. And also moved if conj2 condition to outside of the flat_map loop to make it faster.
class Array
def interpolate conj1 = nil, &conj2
return [] if empty?
if conj2 then first(length - 1).flat_map{|e| [e, conj2.call]}
else first(length - 1).flat_map{|e| [e, conj1]}
end << last
end
end
This one is a modification of Victor Moroz's answer. I added the functionality to accept a block.
class Array
def interpolate conj1 = nil, &conj2
return [] if empty?
first, *rest = self
if conj2 then rest.inject([first]) {|a, e| a.push(conj2.call, e)}
else rest.inject([first]) {|a, e| a.push(conj1, e)}
end
end
end
After benchmark test, the second one looks faster. It seems that flat_map, although looking beautiful, is slow.
Use zip:
a.zip(Array.new(a.size) { 0 }).flatten(1)[0...-1]
Another way
class Array
def interpolate(pol=nil)
new_ary = self.inject([]) do |memo, orig_item|
pol = yield if block_given?
memo += [orig_item, pol]
end
new_ary.pop
new_ary
end
end
[1,2,3].interpolate("A")
#=> [1, "A", 2, "A", 3]
[1,2,3].interpolate {Array.new}
#=> [1, [], 2, [], 3]
class Array
def interpolate_with val
res = []
self.each_with_index do |el, idx|
res << val unless idx == 0
res << el
end
res
end
end
Usage:
ruby-1.9.3-p0 :021 > [1,2,3].interpolate_with 0
=> [1, 0, 2, 0, 3]
ruby-1.9.3-p0 :022 > [1,2,3].interpolate_with []
=> [1, [], 2, [], 3]
Not really sure what you want to do with a block, but I would do it this way:
class Array
def interpolate(sep)
h, *t = self
t.empty? ? [h] : t.inject([h]) { |a, e| a.push(sep, e) }
end
end
UPDATE:
Benchmarks (array size = 100):
user system total real
inject 0.730000 0.000000 0.730000 ( 0.767565)
zip 1.030000 0.000000 1.030000 ( 1.034664)
Actually I am a bit surprised, I thought zip would be faster.
UPDATE2:
zip is faster, flatten is not.
Here's a simple version (which can handle multiple values and/or a block) using flat_map and each_cons:
class Array
def interpolate *values
each_cons(2).flat_map do |e, _|
[e, *values, *(block_given? ? yield(e) : [])]
end << last
end
end
[1,2,3].interpolate(0, "") # => [1, 0, "", 2, 0, "", 3]
[1,2,3].interpolate(&:even?) # => [1, false, 2, true, 3]
This does it inplace:
class Array
def interpolate(t = nil)
each_with_index do |e, i|
t = yield if block_given?
insert(i, t) if i % 2 == 1
end
end
end
This works because t is inserted before the element with the current index, which makes the just inserted t the element with the current index, which means that the iteration can continue normally.
So many ways to do this. For example (Ruby 1.9):
class Array
def intersperse(item = nil)
return clone if self.empty?
take(self.length - 1).flat_map do |x|
[x, item || yield]
end + [self.last]
end
end
p [].intersperse(0)
#=> []
p [1, 2, 3, 4, 5].intersperse(0)
#= >[1, 0, 2, 0, 3, 0, 4, 0, 5]
p [1, 2, 3, 4, 5].intersperse { 0 }
#= >[1, 0, 2, 0, 3, 0, 4, 0, 5]
(I use the Haskell function name: intersperse.)
Here is one way:
theArray.map {|element| [element, interpolated_obj]}.flatten

Resources