Class: Code::Object::List

Inherits:
Code::Object show all
Defined in:
lib/code/object/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #falsy?, #hash, #truthy?

Constructor Details

#initialize(raw = []) ⇒ List

Returns a new instance of List.



6
7
8
# File 'lib/code/object/list.rb', line 6

def initialize(raw = [])
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



4
5
6
# File 'lib/code/object/list.rb', line 4

def raw
  @raw
end

Instance Method Details

#<<(other) ⇒ Object



82
83
84
85
# File 'lib/code/object/list.rb', line 82

def <<(other)
  raw << other
  self
end

#call(**args) ⇒ Object



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
57
58
59
60
61
62
63
64
# File 'lib/code/object/list.rb', line 10

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *::Code::GLOBALS)
  value = arguments.first&.value

  if operator == "any?"
    sig(arguments) { ::Code::Object::Function }
    any?(value, **globals)
  elsif operator == "none?"
    sig(arguments) { ::Code::Object::Function }
    none?(value, **globals)
  elsif operator == "detect"
    sig(arguments) { ::Code::Object::Function }
    detect(value, **globals)
  elsif operator == "reduce"
    sig(arguments) { ::Code::Object::Function }
    reduce(value, **globals)
  elsif operator == "each"
    sig(arguments) { ::Code::Object::Function }
    each(value, **globals)
  elsif operator == "select"
    sig(arguments) { ::Code::Object::Function }
    select(value, **globals)
  elsif operator == "map"
    sig(arguments) { ::Code::Object::Function }
    map(value, **globals)
  elsif operator == "max_by"
    sig(arguments) { ::Code::Object::Function }
    max_by(value, **globals)
  elsif operator == "max"
    sig(arguments)
    max
  elsif operator == "flatten"
    sig(arguments)
    flatten
  elsif operator == "reverse"
    sig(arguments)
    reverse
  elsif operator == "first"
    sig(arguments)
    first
  elsif operator == "last"
    sig(arguments)
    last
  elsif operator == "<<"
    sig(arguments) { ::Code::Object }
    append(value)
  elsif operator == "include?"
    sig(arguments) { ::Code::Object }
    include?(value)
  else
    super
  end
end

#deep_dupObject



78
79
80
# File 'lib/code/object/list.rb', line 78

def deep_dup
  ::Code::Object::List.new(raw.deep_dup)
end

#flattenObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/code/object/list.rb', line 66

def flatten
  ::Code::Object::List.new(
    raw.reduce([]) do |acc, element|
      if element.is_a?(::Code::Object::List)
        acc + element.flatten.raw
      else
        acc + [element]
      end
    end
  )
end

#inspectObject



91
92
93
# File 'lib/code/object/list.rb', line 91

def inspect
  to_s
end

#to_sObject



87
88
89
# File 'lib/code/object/list.rb', line 87

def to_s
  "[#{raw.map(&:inspect).join(", ")}]"
end