File: cons.rb

package info (click to toggle)
mikutter 5.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,780 kB
  • sloc: ruby: 22,912; sh: 186; makefile: 21
file content (56 lines) | stat: -rw-r--r-- 915 bytes parent folder | download | duplicates (2)
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
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
require_relative 'list'

module MIKU
  class Cons
    include List
    include Enumerable

    attr_reader(:car, :cdr)

    def self.list(*nodes)
      unless nodes.empty?
        carnode, *cdrnode = *nodes
        Cons.new(carnode, list(*cdrnode))
      end
    end

    def initialize(car, cdr=nil)
      @car = car
      @cdr = cdr
    end

    def setcar(val)
      @car = val
      self
    end

    def setcdr(val)
      @cdr = val
      self
    end

    def each(&proc)
      proc.call @car
      @cdr.each(&proc) if(@cdr.is_a? Enumerable)
    end

    def to_cons
      self
    end

    def empty?
      false
    end

    def inspect
      "(#{car.inspect}"+(if @cdr.is_a?(Cons) then " #{@cdr.inspect[1..-1]}"
                          elsif @cdr === nil then ')'
                          else " . #{@cdr.inspect})" end)
    end

    def deconstruct
      to_a
    end
  end
end