Sha256: 48517550bd4aca74063aaceb6ebe840a635a362db54e24078696eebfb23b8684

Contents?: true

Size: 944 Bytes

Versions: 4

Compression:

Stored size: 944 Bytes

Contents

# frozen_string_literal: true

require_relative 'cons_cell'

module MiniKraken
  module Composite
    # Module that implements convenience methods for manipulating
    # proper lists represented with ConsCell objects.
    module List
      # Factory method for constructing a ConsCell pair.
      # @param obj1 [Term]
      # @param obj2 [Term]
      # @return [Composite::ConsCell]
      def self.cons(obj1, obj2 = nil)
        ConsCell.new(obj1, obj2)
      end

      # Factory method. Build a proper list with elements of given array.
      # @param arr [Array] Array of elements to put in a new list
      # @return [Composite::ConsCell] Head nnode (cell) of created list.
      def self.make_list(arr)
        return cons(nil, nil) if arr.empty?

        reversed = arr.reverse

        reversed.reduce(nil) do |sub_result, elem|
          ConsCell.new(elem, sub_result)
        end
      end
    end # module
  end # module
end # module

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mini_kraken-0.3.03 lib/mini_kraken/composite/list.rb
mini_kraken-0.3.02 lib/mini_kraken/composite/list.rb
mini_kraken-0.3.01 lib/mini_kraken/composite/list.rb
mini_kraken-0.3.00 lib/mini_kraken/composite/list.rb