Method: Immutable::List#select

Defined in:
lib/immutable/list.rb

#select {|item| ... } ⇒ List Also known as: find_all, keep_if

Return a List which contains all the items for which the given block returns true.

Examples:

Immutable::List["Bird", "Cow", "Elephant"].select { |e| e.size >= 4 }
# => Immutable::List["Bird", "Elephant"]

Yields:

  • (item)

    Once for each item.

Returns:



267
268
269
270
271
272
273
274
275
276
277
# File 'lib/immutable/list.rb', line 267

def select(&block)
  return enum_for(:select) unless block_given?
  LazyList.new do
    list = self
    loop do
      break list if list.empty?
      break Cons.new(list.head, list.tail.select(&block)) if yield(list.head)
      list = list.tail
    end
  end
end