summaryrefslogtreecommitdiff
path: root/lib/rubygems/util/list.rb
blob: 9f540adcc1d47efba4e5f288176815b38eebda6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Gem
  List = Struct.new(:value, :tail)

  class List
    def each
      n = self
      while n
        yield n.value
        n = n.tail
      end
    end

    def to_a
      ary = []
      n = self
      while n
        ary.unshift n.value
        n = n.tail
      end

      ary
    end

    def find
      n = self
      while n
        v = n.value
        return v if yield(v)
        n = n.tail
      end

      nil
    end

    def prepend(value)
      List.new value, self
    end

    def self.prepend(list, value)
      return List.new(value) unless list
      List.new value, list
    end
  end
end