Sha256: fa1929eb07e5c9cff89c8e0a4cd90e5b521b66f3aa3e9bc840069374c43d4245

Contents?: true

Size: 801 Bytes

Versions: 27

Compression:

Stored size: 801 Bytes

Contents

module Abstractivator
  module Cons
    Nil = Object.new

    def empty_list
      Nil
    end

    def cons(h, t)
      Cell.new(h, t)
    end

    def head(p)
      p.first
    end

    def tail(p)
      p.last
    end

    def list_to_enum(list)
      Enumerator.new do |y|
        while list != Nil
          y << head(list)
          list = tail(list)
        end
      end
    end

    def enum_to_list(enum)
      e = enum.reverse.each
      result = Nil
      begin
        while true
          result = cons(e.next, result)
        end
      rescue StopIteration
        result
      end
    end

    class Cell < Array
      def initialize(h, t)
        super([h, t])
      end

      def head
        first
      end

      def tail
        last
      end
    end

    extend self
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
abstractivator-0.0.22 lib/abstractivator/cons.rb
abstractivator-0.0.21 lib/abstractivator/cons.rb
abstractivator-0.0.20 lib/abstractivator/cons.rb
abstractivator-0.0.19 lib/abstractivator/cons.rb
abstractivator-0.0.18 lib/abstractivator/cons.rb
abstractivator-0.0.16 lib/abstractivator/cons.rb
abstractivator-0.0.15 lib/abstractivator/cons.rb