Class: Code::Object::List
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#<=>, #==, #code_and_operator, #code_different, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_inclusive_range, #code_or_operator, #code_self, #code_to_string, #falsy?, #hash, maybe, #multi_fetch, repeat, #sig, #truthy?, |
Constructor Details
#initialize(raw = []) ⇒ List
Returns a new instance of List.
8
9
10
|
# File 'lib/code/object/list.rb', line 8
def initialize(raw = [])
@raw = raw.to_a
end
|
Instance Attribute Details
Returns the value of attribute raw.
6
7
8
|
# File 'lib/code/object/list.rb', line 6
def raw
@raw
end
|
Class Method Details
12
13
14
|
# File 'lib/code/object/list.rb', line 12
def self.name
"List"
end
|
Instance Method Details
#call(**args) ⇒ Object
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/code/object/list.rb', line 16
def call(**args)
operator = args.fetch(:operator, nil)
arguments = args.fetch(:arguments, [])
globals = multi_fetch(args, *GLOBALS)
value = arguments.first&.value
case operator.to_s
when "sort"
sig(args)
code_sort
when "<<", "append"
sig(args) { Object }
code_append(value)
when "any?"
sig(args) { Function }
code_any?(value, **globals)
when "detect"
sig(args) { Function }
code_detect(value, **globals)
when "each"
sig(args) { Function }
code_each(value, **globals)
when "first"
sig(args)
code_first
when "flatten"
sig(args) { Integer.maybe }
code_flatten
when "include?"
sig(args) { Object }
code_include?(value)
when "last"
sig(args)
code_last
when "map"
sig(args) { Function }
code_map(value, **globals)
when "max"
sig(args)
code_max
when "max_by"
sig(args) { Function }
code_max_by(value, **globals)
when "none?"
sig(args) { Function }
code_none?(value, **globals)
when "reduce"
sig(args) { Function }
code_reduce(value, **globals)
when "reverse"
sig(args)
code_reverse
when "select"
sig(args) { Function }
code_select(value, **globals)
when "size"
sig(args)
code_size
when "uniq"
sig(args)
code_uniq
else
super
end
end
|
#code_any?(argument, **globals) ⇒ Boolean
82
83
84
85
86
87
88
|
# File 'lib/code/object/list.rb', line 82
def code_any?(argument, **globals)
Boolean.new(
raw.any? do |element|
argument.call(arguments: [Argument.new(element)], **globals).truthy?
end
)
end
|
#code_append(other) ⇒ Object
90
91
92
93
|
# File 'lib/code/object/list.rb', line 90
def code_append(other)
raw << other
self
end
|
#code_detect(argument, **globals) ⇒ Object
95
96
97
98
99
|
# File 'lib/code/object/list.rb', line 95
def code_detect(argument, **globals)
raw.detect do |element|
argument.call(arguments: [Argument.new(element)], **globals).truthy?
end || Nothing.new
end
|
#code_each(argument, **globals) ⇒ Object
101
102
103
104
105
106
|
# File 'lib/code/object/list.rb', line 101
def code_each(argument, **globals)
raw.each do |element|
argument.call(arguments: [Argument.new(element)], **globals)
end
self
end
|
#code_first ⇒ Object
108
109
110
|
# File 'lib/code/object/list.rb', line 108
def code_first
raw.first || Nothing.new
end
|
#code_flatten(level = nil) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/code/object/list.rb', line 112
def code_flatten(level = nil)
level ||= Integer.new(-1)
level = level.raw if level.is_a?(Integer)
List.new(
raw.reduce([]) do |acc, element|
element = element.to_list if element.is_a?(Dictionary)
if element.is_a?(List) && level != 0
if level.positive?
acc + element.code_flatten(level - 1).raw
else
acc + element.code_flatten(level).raw
end
else
acc + [element]
end
end
)
end
|
#code_include?(other) ⇒ Boolean
133
134
135
|
# File 'lib/code/object/list.rb', line 133
def code_include?(other)
Boolean.new(raw.include?(other))
end
|
#code_last ⇒ Object
137
138
139
|
# File 'lib/code/object/list.rb', line 137
def code_last
raw.last || Nothing.new
end
|
#code_map(argument, **globals) ⇒ Object
141
142
143
144
145
146
147
|
# File 'lib/code/object/list.rb', line 141
def code_map(argument, **globals)
List.new(
raw.map do |element|
argument.call(arguments: [Argument.new(element)], **globals)
end
)
end
|
149
150
151
|
# File 'lib/code/object/list.rb', line 149
def code_max
raw.max || Nothing.new
end
|
#code_max_by(argument, **globals) ⇒ Object
153
154
155
156
157
|
# File 'lib/code/object/list.rb', line 153
def code_max_by(argument, **globals)
raw.max_by do |element|
argument.call(arguments: [Argument.new(element)], **globals)
end || Nothing.new
end
|
#code_none?(argument, **globals) ⇒ Boolean
159
160
161
162
163
164
165
|
# File 'lib/code/object/list.rb', line 159
def code_none?(argument, **globals)
Boolean.new(
raw.none? do |element|
argument.call(arguments: [Argument.new(element)], **globals).truthy?
end
)
end
|
#code_reduce(argument, **globals) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/code/object/list.rb', line 167
def code_reduce(argument, **globals)
raw.reduce do |acc, element|
argument.call(
arguments: [Argument.new(acc), Argument.new(element)],
**globals
)
end || Nothing.new
end
|
#code_reverse ⇒ Object
176
177
178
|
# File 'lib/code/object/list.rb', line 176
def code_reverse
List.new(raw.reverse)
end
|
#code_select(argument, **globals) ⇒ Object
180
181
182
183
184
185
186
|
# File 'lib/code/object/list.rb', line 180
def code_select(argument, **globals)
List.new(
raw.select do |element|
argument.call(arguments: [Argument.new(element)], **globals).truthy?
end
)
end
|
#code_size ⇒ Object
192
193
194
|
# File 'lib/code/object/list.rb', line 192
def code_size
Integer.new(raw.size)
end
|
#code_sort ⇒ Object
188
189
190
|
# File 'lib/code/object/list.rb', line 188
def code_sort
List.new(raw.sort)
end
|
#code_uniq ⇒ Object
196
197
198
|
# File 'lib/code/object/list.rb', line 196
def code_uniq
List.new(raw.uniq)
end
|
200
201
202
|
# File 'lib/code/object/list.rb', line 200
def inspect
to_s
end
|
204
205
206
|
# File 'lib/code/object/list.rb', line 204
def to_s
"[#{raw.map(&:inspect).join(", ")}]"
end
|