B
bparanj
What is the difference between this:
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def make_filter(predicate, list)
result = []
list.each do |element|
result << element if predicate.call(element)
end
result
end
require 'rubygems'
require 'facets/integer/ordinal'
filter_ths = make_filter( lambda { |x| x.ordinal =~ /th$/ ? true :
false }, list)
p filter_ths
and this:
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def make_filter(predicate)
lambda do |list|
result = []
list.each do |element|
result << element if predicate.call(element)
end
result
end
end
require 'rubygems'
require 'facets/integer/ordinal'
filter_ths = make_filter( lambda { |x| x.ordinal =~ /th$/ ? true :
false })
p filter_ths.call(list)
I am not clear on the advantage of returning proc object vs just the
result as an array. TIA.
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def make_filter(predicate, list)
result = []
list.each do |element|
result << element if predicate.call(element)
end
result
end
require 'rubygems'
require 'facets/integer/ordinal'
filter_ths = make_filter( lambda { |x| x.ordinal =~ /th$/ ? true :
false }, list)
p filter_ths
and this:
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def make_filter(predicate)
lambda do |list|
result = []
list.each do |element|
result << element if predicate.call(element)
end
result
end
end
require 'rubygems'
require 'facets/integer/ordinal'
filter_ths = make_filter( lambda { |x| x.ordinal =~ /th$/ ? true :
false })
p filter_ths.call(list)
I am not clear on the advantage of returning proc object vs just the
result as an array. TIA.