can map/collect return fewer values than are found in the target?

  • Thread starter Kelly Dwight Felkins
  • Start date
K

Kelly Dwight Felkins

------=_Part_9367_20485179.1139447912579
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Is there a way for collect to return fewer items than the enumerable the
collect is running on? It looks to me like
map/collect will return at least one value for each item in the array --
I've seen examples of how to return more items, but
not less.

Is there a way to do this more succinctly:

VOWELS =3D %w{a e i o u}
items =3D %w{ a b c d e f g h i}
new_items =3D []
items.each do |item|
new_items << item unless VOWELS.include(item)
end
new_items

map/collect in ruby seems very similar to map in perl except that in perl
the block can return zero or more elements. Is there an method in ruby that
is similar to perl's map?

Thanks,

-Kelly

------=_Part_9367_20485179.1139447912579--
 
L

Logan Capaldo

Is there a way for collect to return fewer items than the
enumerable the
collect is running on? It looks to me like
map/collect will return at least one value for each item in the
array --
I've seen examples of how to return more items, but
not less.

Is there a way to do this more succinctly:

VOWELS = %w{a e i o u}
items = %w{ a b c d e f g h i}
new_items = []
items.each do |item|
new_items << item unless VOWELS.include(item)
end
new_items

map/collect in ruby seems very similar to map in perl except that
in perl
the block can return zero or more elements. Is there an method in
ruby that
is similar to perl's map?

Thanks,

-Kelly

new_items = items.reject { |item| VOWELS.include?(item) }

or

new_items = items.select { |item| true unless VOWELS.include?(item) }

or

new_items = items.inject([]) { |list, item| list << item unless
VOWELS.include?(item); list }
 
M

Marcel Molina Jr.

Is there a way for collect to return fewer items than the enumerable the
collect is running on? It looks to me like
map/collect will return at least one value for each item in the array --
I've seen examples of how to return more items, but
not less.

Is there a way to do this more succinctly:

VOWELS = %w{a e i o u}
items = %w{ a b c d e f g h i}
new_items = []
items.each do |item|
new_items << item unless VOWELS.include(item)
end
new_items

map/collect in ruby seems very similar to map in perl except that in perl
the block can return zero or more elements. Is there an method in ruby that
is similar to perl's map?

Looks like you want Arry#select. In fact, take a look at the docs ;)

----------------------------------------------------------- Array#select
array.select {|item| block } -> an_array
------------------------------------------------------------------------
Invokes the block passing in successive elements from _array_,
returning an array containing those elements for which the block
returns a true value (equivalent to +Enumerable#select+).

a = %w{ a b c d e f }
a.select {|v| v =~ /[aeiou]/} #=> ["a", "e"]

marcel
 
D

David Vallner

The difference seems to be that Ruby's interpretation of map seems to be=20
stricter than Perl's, e.g. for one item in the original Array, there's=20
exactly one corresponding item in the result.

If you want a method with the behaviour similar to Perl's, you could do:

class Array
def pl_map(&block)
map(&block).compact.flatten
end
end

new_items =3D items.pl_map { | item | item if VOWELS.include(item) }

David Vallner

D=C5=88a =C5=A0tvrtok 09 Febru=C3=A1r 2006 02:18 Kelly Dwight Felkins nap=
=C3=ADsal:
 
R

Robert Klemme

Logan said:
new_items = items.select { |item| true unless VOWELS.include?(item) }

new_items = items.select { |item| not VOWELS.include?(item) }

robert
 
J

Jeremy Henty

Is there a way for collect to return fewer items than the enumerable
the collect is running on?

Strictly speaking, no (AFAIK), but it's easy to fake it. Use
Array#collect with a block that returns nil when you don't want an
element, then Array#compact the result.

Similarly, to get the effect of a block returning multiple values to
Array#collect, just have the block return an Array and then
Array#flatten the result.

Cheers,

Jeremy Henty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top