In Ruby, Is There an Array Method That Combines 'Select' and 'Map'

In Ruby, is there an Array method that combines 'select' and 'map'?

I usually use map and compact together along with my selection criteria as a postfix if. compact gets rid of the nils.

jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}    
=> [3, 3, 3, nil, nil, nil]


jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}.compact
=> [3, 3, 3]

Mapping and selecting at the same time

Almost the same to reduce or inject

new_array = some_array.each_with_object([]) do |m,res|
res << modification(x) if some_condition(x)
end

The difference is that you don't need to put result at the end of block.

Iterate through array using Map and Select method Together in Ruby

Why not test in the call to map?

bunch2 = bunch1.map {|y| y !~ /^http/ ? "http://#{y}" : y }

Ruby Array Chainables Combining Map & Inject

If you use this wierd Symbol patch shown here:

https://stackoverflow.com/a/23711606/2981429

class Symbol
def call(*args, &block)
->(caller, *rest) { caller.send(self, *rest, *args, &block) }
end
end

This allows you to pass arguments to the proc shorthand by means of Currying:

[[1,2],[3,4]].map(&:inject.(1, &:*))
# => [2, 12]

I'm sure this has been requested in Ruby core many times, unfortunately I don't have a link to the Ruby forums right now but I promise you it's on there.

Array method that combines 'uniq' and 'compact'

No, but you can append them in any order you like i.e.

array.uniq.compact
array.compact.uniq

As pointed out by phts, you can pass a block to uniq but I don't see that being a very helpful alternative over uniq.compact.

For better speed however, something like the following might help:

[].tap do |new_array|
hash = {}
original_array.each do |element|
next if element.nil? || !hash[element].nil?
new_array << (hash[element] = element)
end
end

Finally, if speed isn't an issue and you'll be frequently using this method, then you could create your own method:

class Array
def compact_uniq
self.compact.uniq
end

def compact_blank # in case you want to remove all 'blanks' as well
self.compact.reject(&:blank?)
end
end

Getting Ruby array of indexes which fulfill a certain condition

If you are using Ruby version >= 2.7 you can use the filter_map method, like so:

a = [3,4,8,9,11,14]

a.each_with_index.filter_map {|n, i| i if n.odd?}

How to combines two nested data structures into one?

Please note that what you call hashes are indeed arrays, and that too they are not consistent. First one is an array containing single hash, and second one is an array containing two hashes.

For the given two arrays, if we call the first v1 and second one v2, then, below code should give the output you desire:

v1[0].values.map.with_index {|v, i| v2[i].merge(v)}

Explanation:

Hash#merge merges two hashes, so it has to be used to achieve the result.

The first hash will be from v1 - we pick the first element of v1 array, which is a hash, and take all its values which are hashes themselves - thus, we have an array of hash in v1[0].values.

The second array of hashes is v2.

Now, we use the Enumerable#map method to iterate over first array of hashes and collecting the result of merging each of its elements with corresponding element from v2.

Does ruby support an enumerable map_cons method or its equivalent?

The documentation of each_cons ends with this innocent phrase: "If no block is given, returns an enumerator." Most methods of Enumerable do this. What can you do with an Enumerator? Nothing truly impressive . But Enumerators include Enumerable, which does provide a large amount of powerful methods, map being one of them. So, as Stefan Pochmann does:

[1, 1, 4, 5, 6, 2, 2].each_cons(2).map { |e| e[0] if e[0] == e[1] }

each_consis called without a block, so it returns an Enumerator. mapis simply one of its methods.

Trying to concat 2 arrays that are output from the map method

  sc = str.split(/\s\s\s/)

deciphered = sc.map do |string|
string.split(' ').map {|key| morsecode[key]; }.join
end

deciphered.join(' ')

Is there a standard method in Ruby to prepend and/or append a string onto each of an array of strings?

listOfnames.map {|name| "Mr. " + name}

If you need to edit the listOfnames variable, use the destructive version of map:

listOfnames.map! {|name| "Mr. " + name}


Related Topics



Leave a reply



Submit