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.8.0 lib/abstractivator/cons.rb
abstractivator-0.7.0 lib/abstractivator/cons.rb
abstractivator-0.6.0 lib/abstractivator/cons.rb
abstractivator-0.5.0 lib/abstractivator/cons.rb
abstractivator-0.4.1 lib/abstractivator/cons.rb
abstractivator-0.4.0 lib/abstractivator/cons.rb
abstractivator-0.3.1 lib/abstractivator/cons.rb
abstractivator-0.3.0 lib/abstractivator/cons.rb
abstractivator-0.2.0 lib/abstractivator/cons.rb
abstractivator-0.1.1 lib/abstractivator/cons.rb
abstractivator-0.1.0 lib/abstractivator/cons.rb
abstractivator-0.0.31 lib/abstractivator/cons.rb
abstractivator-0.0.30 lib/abstractivator/cons.rb
abstractivator-0.0.29 lib/abstractivator/cons.rb
abstractivator-0.0.28 lib/abstractivator/cons.rb
abstractivator-0.0.27 lib/abstractivator/cons.rb
abstractivator-0.0.26 lib/abstractivator/cons.rb
abstractivator-0.0.25 lib/abstractivator/cons.rb
abstractivator-0.0.24 lib/abstractivator/cons.rb
abstractivator-0.0.23 lib/abstractivator/cons.rb