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?
new_items = items.inject() { |list, item| list << item unless VOWELS.include?(item); list }
···
On Feb 8, 2006, at 8:18 PM, Kelly Dwight Felkins wrote:
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?
On Thu, Feb 09, 2006 at 10:18:46AM +0900, Kelly Dwight Felkins wrote:
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?
------------------------------------------------------------------------
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"]
items.inject(){|a,i| a << i unless VOWELS.include?(i); a}
Kent.
···
On 2/8/06, Kelly Dwight Felkins <railsinator@gmail.com> wrote:
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?
The difference seems to be that Ruby's interpretation of map seems to be
stricter than Perl's, e.g. for one item in the original Array, there's
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
Dňa Štvrtok 09 Február 2006 02:18 Kelly Dwight Felkins napísal:
···
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?
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
···
On 2006-02-09, Kelly Dwight Felkins <railsinator@gmail.com> wrote:
Is there a way for collect to return fewer items than the enumerable
the collect is running on?
Thanks much everyone. I think the combo of map with compact is what I
needed.
-Kelly
···
On 2/8/06, David Vallner <david@vallner.net> wrote:
The difference seems to be that Ruby's interpretation of map seems to be
stricter than Perl's, e.g. for one item in the original Array, there's
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
Dňa Štvrtok 09 Február 2006 02:18 Kelly Dwight Felkins napísal:
> 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