class Erlang::Cons

The basic building block for constructing lists

A Cons, also known as a “cons cell”, has a “head” and a “tail”, where the head is an element in the list, and the tail is a reference to the rest of the list. This way a singly linked list can be constructed, with each `Erlang::Cons` holding a single element and a pointer to the next `Erlang::Cons`.

The last `Erlang::Cons` instance in the chain has the {Erlang::Nil} as its tail.

Licensing

Portions taken and modified from github.com/hamstergem/hamster

Copyright (c) 2009-2014 Simon Harris

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@private

Attributes

head[R]
tail[R]

Public Class Methods

inspect() click to toggle source
# File lib/erlang/cons.rb, line 49
def inspect
  return Erlang::List.inspect
end
new(head, tail = Erlang::Nil) click to toggle source
# File lib/erlang/cons.rb, line 62
def initialize(head, tail = Erlang::Nil)
  @head = Erlang.from(head)
  @tail = Erlang.from(tail)
  @improper = @tail.kind_of?(Erlang::List) ? @tail.improper? : true
end
pretty_inspect() click to toggle source
# File lib/erlang/cons.rb, line 53
def pretty_inspect
  return Erlang::List.pretty_inspect
end
pretty_print(q) click to toggle source
# File lib/erlang/cons.rb, line 57
def pretty_print(q)
  return Erlang::List.pretty_print(q)
end

Public Instance Methods

empty?() click to toggle source
# File lib/erlang/cons.rb, line 68
def empty?
  return false
end
improper?() click to toggle source
# File lib/erlang/cons.rb, line 72
def improper?
  return !!@improper
end
length()
Alias for: size
marshal_dump() click to toggle source

@return [::Array] @private

# File lib/erlang/cons.rb, line 94
def marshal_dump
  if improper?
    return [to_proper_list.to_a, last(true)]
  else
    return [to_a, Erlang::Nil]
  end
end
marshal_load(args) click to toggle source

@private

# File lib/erlang/cons.rb, line 103
def marshal_load(args)
  h, t = args
  if h.size == 0
    return t
  elsif Erlang::Nil.eql?(t)
    head = h[0]
    tail = Erlang::List.from_enum(h[1..-1])
    initialize(head, tail)
    __send__(:immutable!)
    return self
  else
    head = h[0]
    tail = Erlang::List.from_enum(h[1..-1]) + t
    initialize(head, tail)
    __send__(:immutable!)
    return self
  end
end
size() click to toggle source

Return the number of items in this `Erlang::List`. @return [Integer]

# File lib/erlang/cons.rb, line 78
def size
  raise Erlang::ImproperListError if improper?
  return 0 if empty?
  size = 0
  list = self
  until list.empty?
    list = list.tail
    size += 1
  end
  return size
end
Also aliased as: length