Sha256: bb13d125ec573dc5f0352e33d7c549fb005fa92876f6bfac3c322cb6674a33ef

Contents?: true

Size: 852 Bytes

Versions: 11

Compression:

Stored size: 852 Bytes

Contents

module Abstractivator
  module Cons
    Nil = Object.new
    Nil.define_singleton_method(:empty?) { true }

    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

11 entries across 11 versions & 1 rubygems

Version Path
abstractivator-0.20.0 lib/abstractivator/cons.rb
abstractivator-0.19.0 lib/abstractivator/cons.rb
abstractivator-0.18.0 lib/abstractivator/cons.rb
abstractivator-0.17.0 lib/abstractivator/cons.rb
abstractivator-0.16.0 lib/abstractivator/cons.rb
abstractivator-0.15.0 lib/abstractivator/cons.rb
abstractivator-0.14.0 lib/abstractivator/cons.rb
abstractivator-0.13.0 lib/abstractivator/cons.rb
abstractivator-0.12.0 lib/abstractivator/cons.rb
abstractivator-0.11.0 lib/abstractivator/cons.rb
abstractivator-0.10.0 lib/abstractivator/cons.rb