Class: Code::Object::List

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

Direct Known Subclasses

IdentifierList

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, code_different, #code_different, #code_equal_equal, code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, to_json, #to_json, to_s, truthy?, #truthy?, |

Constructor Details

#initialize(*args, **_kargs) ⇒ List

Returns a new instance of List.



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

def initialize(*args, **_kargs, &)
  raw = args.first
  raw = raw.raw if raw.is_an?(Object)
  @raw = raw.to_a
end

Instance Method Details

#call(**args) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/code/object/list.rb', line 12

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, List.new)
  globals = multi_fetch(args, *GLOBALS)
  value = arguments.code_first

  case operator.to_s
  when "join"
    sig(args) { String.maybe }
    code_join(value)
  when "sort"
    sig(args)
    code_sort
  when "<<", "append"
    sig(args) { Object }
    code_append(value)
  when "any?"
    sig(args) { Function.maybe }
    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 "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.maybe }
    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 "select!"
    sig(args) { Function }
    code_select!(value, **globals)
  when "reject"
    sig(args) { Function }
    code_reject(value, **globals)
  when "reject!"
    sig(args) { Function }
    code_reject!(value, **globals)
  when "size"
    sig(args)
    code_size
  when "sum"
    sig(args)
    code_sum
  when "uniq"
    sig(args)
    code_uniq
  else
    super
  end
end

#code_any?(argument = nil, **globals) ⇒ Boolean

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/code/object/list.rb', line 96

def code_any?(argument = nil, **globals)
  argument ||= Nothing.new
  index = 0

  Boolean.new(
    raw.any? do |element|
      if argument.nothing?
        element.truthy?
      else
        argument.call(
          arguments: List.new([element, Integer.new(index), self]),
          **globals
        ).truthy?
      end
    rescue Error::Next => e
      e.value || Nothing.new
    ensure
      index += 1
    end
  )
end

#code_append(other) ⇒ Object



118
119
120
121
# File 'lib/code/object/list.rb', line 118

def code_append(other)
  raw << other
  self
end

#code_detect(argument, **globals) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/code/object/list.rb', line 123

def code_detect(argument, **globals)
  raw.detect.with_index do |element, index|
    argument.call(
      arguments: List.new([element, Integer.new(index), self]),
      **globals
    ).truthy?
  rescue Error::Next => e
    e.value || Nothing.new
  end || Nothing.new
end

#code_each(argument, **globals) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/code/object/list.rb', line 134

def code_each(argument, **globals)
  raw.each.with_index do |element, index|
    argument.call(
      arguments: List.new([element, Integer.new(index), self]),
      **globals
    )
  rescue Error::Next => e
    e.value || Nothing.new
  end
  self
end

#code_firstObject



146
147
148
# File 'lib/code/object/list.rb', line 146

def code_first
  raw.first || Nothing.new
end

#code_flatten(level = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/code/object/list.rb', line 150

def code_flatten(level = nil)
  level = Integer.new(-1) if level.nil? || level.is_a?(Nothing)
  level = level.raw if level.is_a?(Integer)

  List.new(
    raw.reduce([]) do |acc, element|
      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

Returns:



169
170
171
# File 'lib/code/object/list.rb', line 169

def code_include?(other)
  Boolean.new(raw.include?(other))
end

#code_join(separator = nil) ⇒ Object



307
308
309
310
311
# File 'lib/code/object/list.rb', line 307

def code_join(separator = nil)
  separator ||= +""

  String.new(raw.join(separator.raw))
end

#code_lastObject



173
174
175
# File 'lib/code/object/list.rb', line 173

def code_last
  raw.last || Nothing.new
end

#code_map(argument, **globals) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/code/object/list.rb', line 177

def code_map(argument, **globals)
  List.new(
    raw.map.with_index do |element, index|
      argument.call(
        arguments: List.new([element, Integer.new(index), self]),
        **globals
      )
    rescue Error::Next => e
      e.value || Nothing.new
    end
  )
end

#code_map!(argument, **globals) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/code/object/list.rb', line 190

def code_map!(argument, **globals)
  raw.map!.with_index do |element, index|
    argument.call(
      arguments: List.new([element, Integer.new(index), self]),
      **globals
    )
  rescue Error::Next => e
    e.value || Nothing.new
  end

  self
end

#code_maxObject



203
204
205
# File 'lib/code/object/list.rb', line 203

def code_max
  raw.max || Nothing.new
end

#code_max_by(argument, **globals) ⇒ Object



207
208
209
210
211
212
213
214
215
216
# File 'lib/code/object/list.rb', line 207

def code_max_by(argument, **globals)
  raw.max_by.with_index do |element, index|
    argument.call(
      arguments: List.new([element, Integer.new(index), self]),
      **globals
    )
  rescue Error::Next => e
    e.value || Nothing.new
  end || Nothing.new
end

#code_none?(argument = nil, **globals) ⇒ Boolean

Returns:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/code/object/list.rb', line 218

def code_none?(argument = nil, **globals)
  argument ||= Nothing.new
  index = 0

  Boolean.new(
    raw.none? do |element|
      if argument.nothing?
        element.truthy?
      else
        argument.call(
          arguments: List.new([element, Integer.new(index), self]),
          **globals
        ).truthy?
      end
    rescue Error::Next => e
      (e.value || Nothing.new).truthy?
    ensure
      index += 1
    end
  )
end

#code_reduce(argument, **globals) ⇒ Object



240
241
242
243
244
245
246
247
248
249
# File 'lib/code/object/list.rb', line 240

def code_reduce(argument, **globals)
  raw.reduce.with_index do |acc, element, index|
    argument.call(
      arguments: List.new([acc, element, Integer.new(index), self]),
      **globals
    )
  rescue Error::Next => e
    e.value || Nothing.new
  end || Nothing.new
end

#code_reject(argument, **globals) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/code/object/list.rb', line 281

def code_reject(argument, **globals)
  List.new(
    raw.reject.with_index do |element, index|
      argument.call(
        arguments: List.new([element, Integer.new(index), self]),
        **globals
      ).truthy?
    rescue Error::Next => e
      (e.value || Nothing.new).truthy?
    end
  )
end

#code_reject!(argument, **globals) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/code/object/list.rb', line 294

def code_reject!(argument, **globals)
  raw.reject!.with_index do |element, index|
    argument.call(
      arguments: List.new([element, Integer.new(index), self]),
      **globals
    ).truthy?
  rescue Error::Next => e
    (e.value || Nothing.new).truthy?
  end

  self
end

#code_reverseObject



251
252
253
# File 'lib/code/object/list.rb', line 251

def code_reverse
  List.new(raw.reverse)
end

#code_select(argument, **globals) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/code/object/list.rb', line 255

def code_select(argument, **globals)
  List.new(
    raw.select.with_index do |element, index|
      argument.call(
        arguments: List.new([element, Integer.new(index), self]),
        **globals
      ).truthy?
    rescue Error::Next => e
      (e.value || Nothing.new).truthy?
    end
  )
end

#code_select!(argument, **globals) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/code/object/list.rb', line 268

def code_select!(argument, **globals)
  raw.select!.with_index do |element, index|
    argument.call(
      arguments: List.new([element, Integer.new(index), self]),
      **globals
    ).truthy?
  rescue Error::Next => e
    (e.value || Nothing.new).truthy?
  end

  self
end

#code_sizeObject



317
318
319
# File 'lib/code/object/list.rb', line 317

def code_size
  Integer.new(raw.size)
end

#code_sortObject



313
314
315
# File 'lib/code/object/list.rb', line 313

def code_sort
  List.new(raw.sort)
end

#code_sumObject



325
326
327
# File 'lib/code/object/list.rb', line 325

def code_sum
  raw.inject(&:code_plus) || Nothing.new
end

#code_uniqObject



321
322
323
# File 'lib/code/object/list.rb', line 321

def code_uniq
  List.new(raw.uniq)
end