Class: Literal::Array

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Types
Defined in:
lib/literal/array.rb

Defined Under Namespace

Classes: Generic

Constant Summary

Constants included from Types

Types::CallableType, Types::LambdaType, Types::NilableBooleanType, Types::NilableCallableType, Types::NilableJSONDataType, Types::NilableLambdaType, Types::NilableProcableType, Types::ProcableType

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Types

#_Any, #_Any?, #_Array, #_Array?, #_Boolean, #_Boolean?, #_Callable, #_Callable?, #_Class, #_Class?, #_Constraint, #_Constraint?, #_Date, #_Date?, #_Deferred, #_Deferred?, #_Descendant, #_Descendant?, #_Enumerable, #_Enumerable?, #_Falsy, #_Float, #_Float?, #_Frozen, #_Frozen?, #_Hash, #_Hash?, #_Integer, #_Integer?, #_Interface, #_Interface?, #_Intersection, #_Intersection?, #_JSONData, #_JSONData?, #_Lambda, #_Lambda?, #_Map, #_Map?, #_Never, #_Nilable, #_Not, #_Predicate, #_Procable, #_Procable?, #_Range, #_Range?, #_Set, #_Set?, #_String, #_String?, #_Symbol, #_Symbol?, #_Time, #_Time?, #_Truthy, #_Tuple, #_Tuple?, #_Union, #_Union?, #_Void

Constructor Details

#initialize(value, type:) ⇒ Array

Returns a new instance of Array.



53
54
55
56
57
58
59
60
# File 'lib/literal/array.rb', line 53

def initialize(value, type:)
  Literal.check(value, _Array(type)) do |c|
    c.fill_receiver(receiver: self, method: "#initialize")
  end

  @__type__ = type
  @__value__ = value
end

Instance Attribute Details

#__type__Object (readonly)

Returns the value of attribute __type__.



76
77
78
# File 'lib/literal/array.rb', line 76

def __type__
  @__type__
end

#__value__Object (readonly)

Returns the value of attribute __value__.



76
77
78
# File 'lib/literal/array.rb', line 76

def __value__
  @__value__
end

Instance Method Details

#&(other) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/literal/array.rb', line 78

def &(other)
  case other
  when ::Array
    __with__(@__value__ & other)
  when Literal::Array
    __with__(@__value__ & other.__value__)
  else
    raise ArgumentError.new("Cannot perform bitwise AND with #{other.class.name}.")
  end
end

#*(times) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/literal/array.rb', line 89

def *(times)
  case times
  when String
    @__value__ * times
  else
    __with__(@__value__ * times)
  end
end

#+(other) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/literal/array.rb', line 98

def +(other)
  case other
  when ::Array
    Literal.check(other, _Array(@__type__)) do |c|
      c.fill_receiver(receiver: self, method: "#+")
    end

    __with__(@__value__ + other)
  when Literal::Array(@__type__)
    __with__(@__value__ + other.__value__)
  when Literal::Array
    raise Literal::TypeError.new(
      context: Literal::TypeError::Context.new(
        expected: Literal::Array(@__type__),
        actual: other
      )
    )
  else
    raise ArgumentError.new("Cannot perform `+` with #{other.class.name}.")
  end
end

#-(other) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/literal/array.rb', line 120

def -(other)
  case other
  when ::Array
    __with__(@__value__ - other)
  when Literal::Array
    __with__(@__value__ - other.__value__)
  else
    raise ArgumentError.new("Cannot perform `-` with #{other.class.name}.")
  end
end

#<<(value) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/literal/array.rb', line 131

def <<(value)
  Literal.check(value, @__type__) do |c|
    c.fill_receiver(receiver: self, method: "#<<")
  end

  @__value__ << value
  self
end

#<=>(other) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/literal/array.rb', line 140

def <=>(other)
  case other
  when ::Array
    @__value__ <=> other
  when Literal::Array
    @__value__ <=> other.__value__
  else
    raise ArgumentError.new("Cannot perform `<=>` with #{other.class.name}.")
  end
end

#==(other) ⇒ Object Also known as: eql?



151
152
153
# File 'lib/literal/array.rb', line 151

def ==(other)
  Literal::Array === other && @__value__ == other.__value__
end

#[](index) ⇒ Object



155
156
157
# File 'lib/literal/array.rb', line 155

def [](index)
  @__value__[index]
end

#[]=(index, value) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/literal/array.rb', line 159

def []=(index, value)
  Literal.check(value, @__type__) do |c|
    c.fill_receiver(receiver: self, method: "#[]=")
  end

  @__value__[index] = value
end

#__initialize_without_check__(value, type:) ⇒ Object



62
63
64
65
66
# File 'lib/literal/array.rb', line 62

def __initialize_without_check__(value, type:)
  @__type__ = type
  @__value__ = value
  self
end

#__with__(value) ⇒ Object

Used to create a new Literal::Array with the same type and collection type but a new value. The value is not checked.



69
70
71
72
73
74
# File 'lib/literal/array.rb', line 69

def __with__(value)
  Literal::Array.allocate.__initialize_without_check__(
    value,
    type: @__type__,
  )
end

#all?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/literal/array.rb', line 167

def all?(...)
  @__value__.all?(...)
end

#any?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/literal/array.rb', line 171

def any?(...)
  @__value__.any?(...)
end

#assocObject



175
176
177
# File 'lib/literal/array.rb', line 175

def assoc(...)
  @__value__.assoc(...)
end

#atObject



179
180
181
# File 'lib/literal/array.rb', line 179

def at(...)
  @__value__.at(...)
end

#bsearchObject



183
184
185
# File 'lib/literal/array.rb', line 183

def bsearch(...)
  @__value__.bsearch(...)
end

#clearObject



187
188
189
190
# File 'lib/literal/array.rb', line 187

def clear(...)
  @__value__.clear(...)
  self
end

#combinationObject



192
193
194
195
# File 'lib/literal/array.rb', line 192

def combination(...)
  @__value__.combination(...)
  self
end

#compactObject



197
198
199
200
# File 'lib/literal/array.rb', line 197

def compact
  # @TODO if this is an array of nils, we should return an emtpy array
  __with__(@__value__)
end

#compact!Object



202
203
204
205
# File 'lib/literal/array.rb', line 202

def compact!
  # @TODO if this is an array of nils, we should set @__value__ = [] and return self
  nil
end

#countObject



207
208
209
# File 'lib/literal/array.rb', line 207

def count(...)
  @__value__.count(...)
end

#deleteObject



211
212
213
# File 'lib/literal/array.rb', line 211

def delete(...)
  @__value__.delete(...)
end

#delete_atObject



215
216
217
# File 'lib/literal/array.rb', line 215

def delete_at(...)
  @__value__.delete_at(...)
end

#delete_ifObject



219
220
221
222
# File 'lib/literal/array.rb', line 219

def delete_if(...)
  @__value__.delete_if(...)
  self
end

#digObject



224
225
226
# File 'lib/literal/array.rb', line 224

def dig(...)
  @__value__.dig(...)
end

#dropObject



228
229
230
# File 'lib/literal/array.rb', line 228

def drop(...)
  __with__(@__value__.drop(...))
end

#drop_whileObject



232
233
234
# File 'lib/literal/array.rb', line 232

def drop_while(...)
  __with__(@__value__.drop_while(...))
end

#eachObject



236
237
238
# File 'lib/literal/array.rb', line 236

def each(...)
  @__value__.each(...)
end

#each_indexObject



240
241
242
# File 'lib/literal/array.rb', line 240

def each_index(...)
  @__value__.each_index(...)
end

#empty?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/literal/array.rb', line 244

def empty?
  @__value__.empty?
end

#fetchObject



658
659
660
# File 'lib/literal/array.rb', line 658

def fetch(...)
  @__value__.fetch(...)
end

#filterObject



250
251
252
# File 'lib/literal/array.rb', line 250

def filter(...)
  __with__(@__value__.filter(...))
end

#filter!Object



254
255
256
257
# File 'lib/literal/array.rb', line 254

def filter!(...)
  @__value__.filter!(...)
  self
end

#firstObject



263
264
265
# File 'lib/literal/array.rb', line 263

def first(...)
  @__value__.first(...)
end

#flattenObject



259
260
261
# File 'lib/literal/array.rb', line 259

def flatten(...)
  __with__(@__value__.flatten(...))
end

#flatten!Object



267
268
269
# File 'lib/literal/array.rb', line 267

def flatten!(...)
  @__value__.flatten!(...) ? self : nil
end

#freezeObject



271
272
273
274
# File 'lib/literal/array.rb', line 271

def freeze
  @__value__.freeze
  super
end

#hashObject



276
277
278
# File 'lib/literal/array.rb', line 276

def hash
  [self.class, @__value__].hash
end

#include?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/literal/array.rb', line 280

def include?(...)
  @__value__.include?(...)
end

#insert(index, *value) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/literal/array.rb', line 286

def insert(index, *value)
  Literal.check(value, _Array(@__type__)) do |c|
    c.fill_receiver(receiver: self, method: "#insert")
  end

  @__value__.insert(index, *value)
  self
end

#inspectObject



295
296
297
# File 'lib/literal/array.rb', line 295

def inspect
  "Literal::Array(#{@__type__.inspect})#{@__value__.inspect}"
end

#intersect?(other) ⇒ Boolean

Returns:

  • (Boolean)


299
300
301
302
303
304
305
306
307
308
# File 'lib/literal/array.rb', line 299

def intersect?(other)
  case other
  when ::Array
    @__value__.intersect?(other)
  when Literal::Array
    @__value__.intersect?(other.__value__)
  else
    raise ArgumentError.new("Cannot perform `intersect?` with #{other.class.name}.")
  end
end

#intersection(*values) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/literal/array.rb', line 310

def intersection(*values)
  values.map! do |value|
    case value
    when Literal::Array
      value.__value__
    else
      value
    end
  end

  __with__(@__value__.intersection(*values))
end

#joinObject



323
324
325
# File 'lib/literal/array.rb', line 323

def join(...)
  @__value__.join(...)
end

#keep_ifObject



327
328
329
330
331
332
333
334
335
# File 'lib/literal/array.rb', line 327

def keep_if(...)
  return_value = @__value__.keep_if(...)
  case return_value
  when Array
    self
  else
    return_value
  end
end

#lastObject



337
338
339
# File 'lib/literal/array.rb', line 337

def last(...)
  @__value__.last(...)
end

#lengthObject



341
342
343
# File 'lib/literal/array.rb', line 341

def length(...)
  @__value__.length(...)
end

#map(type, &block) ⇒ Object Also known as: collect



345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/literal/array.rb', line 345

def map(type, &block)
  my_type = @__type__
  transform_type = Literal::Transforms.dig(my_type, block)

  if transform_type && Literal.subtype?(transform_type, my_type)
    Literal::Array.allocate.__initialize_without_check__(
      @__value__.map(&block),
      type:,
    )
  else
    Literal::Array.new(@__value__.map(&block), type:)
  end
end

#map!Object Also known as: collect!

TODO: we can make this faster



362
363
364
365
366
# File 'lib/literal/array.rb', line 362

def map!(&)
  new_array = map(@__type__, &)
  @__value__ = new_array.__value__
  self
end

#max(n = nil) ⇒ Object



374
375
376
377
378
379
380
# File 'lib/literal/array.rb', line 374

def max(n = nil, &)
  if n
    __with__(@__value__.max(n, &))
  else
    @__value__.max(&)
  end
end

#min(n = nil) ⇒ Object



382
383
384
385
386
387
388
# File 'lib/literal/array.rb', line 382

def min(n = nil, &)
  if n
    __with__(@__value__.min(n, &))
  else
    @__value__.min(&)
  end
end

#minmaxObject



390
391
392
# File 'lib/literal/array.rb', line 390

def minmax(...)
  __with__(@__value__.minmax(...))
end

#narrow(type) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/literal/array.rb', line 394

def narrow(type)
  unless Literal.subtype?(type, @__type__)
    raise ArgumentError.new("Cannot narrow #{@__type__} to #{type}")
  end

  if __type__ != type
    @__value__.each do |item|
      Literal.check(item, type) do |c|
        c.fill_receiver(receiver: self, method: "#narrow")
      end
    end
  end

  Literal::Array.allocate.__initialize_without_check__(
    @__value__,
    type:,
  )
end

#one?Boolean

Returns:

  • (Boolean)


413
414
415
# File 'lib/literal/array.rb', line 413

def one?(...)
  @__value__.one?(...)
end

#packObject



417
418
419
# File 'lib/literal/array.rb', line 417

def pack(...)
  @__value__.pack(...)
end

#popObject



421
422
423
# File 'lib/literal/array.rb', line 421

def pop(...)
  @__value__.pop(...)
end

#product(*others) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/literal/array.rb', line 425

def product(*others, &)
  if block_given?
    @__value__.product(
      *others.map do |other|
        case other
        when Array
          other
        when Literal::Array
          other.__value__
        end
      end, &
    )

    self
  elsif others.all?(Literal::Array)
    tuple_type = Literal::Tuple(
      @__type__,
      *others.map(&:__type__)
    )

    values = @__value__.product(*others.map(&:__value__)).map do |tuple_values|
      tuple_type.new(*tuple_values)
    end

    Literal::Array(tuple_type).new(*values)
  else
    @__value__.product(*others)
  end
end

#push(*value) ⇒ Object Also known as: append



455
456
457
458
459
460
461
462
# File 'lib/literal/array.rb', line 455

def push(*value)
  Literal.check(value, _Array(@__type__)) do |c|
    c.fill_receiver(receiver: self, method: "#push")
  end

  @__value__.push(*value)
  self
end

#rejectObject



466
467
468
# File 'lib/literal/array.rb', line 466

def reject(...)
  __with__(@__value__.reject(...))
end

#reject!Object



470
471
472
473
# File 'lib/literal/array.rb', line 470

def reject!(...)
  @__value__.reject!(...)
  self
end

#replace(value) ⇒ Object Also known as: initialize_copy



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/literal/array.rb', line 475

def replace(value)
  case value
  when Array
    Literal.check(value, _Array(@__type__)) do |c|
      c.fill_receiver(receiver: self, method: "#replace")
    end

    @__value__.replace(value)
  when Literal::Array(@__type__)
    @__value__.replace(value.__value__)
  when Literal::Array
    raise Literal::TypeError.new(
      context: Literal::TypeError::Context.new(expected: @__type__, actual: value.__type__)
    )
  else
    raise ArgumentError.new("#replace expects Array argument")
  end

  self
end

#rotateObject



498
499
500
# File 'lib/literal/array.rb', line 498

def rotate(...)
  __with__(@__value__.rotate(...))
end

#rotate!Object



502
503
504
505
# File 'lib/literal/array.rb', line 502

def rotate!(...)
  @__value__.rotate!(...)
  self
end

#sampleObject



507
508
509
# File 'lib/literal/array.rb', line 507

def sample(...)
  @__value__.sample(...)
end

#selectObject



511
512
513
# File 'lib/literal/array.rb', line 511

def select(...)
  __with__(@__value__.select(...))
end

#select!Object



515
516
517
518
# File 'lib/literal/array.rb', line 515

def select!(...)
  @__value__.select!(...)
  self
end

#shiftObject



520
521
522
# File 'lib/literal/array.rb', line 520

def shift(...)
  @__value__.shift(...)
end

#shuffleObject



524
525
526
# File 'lib/literal/array.rb', line 524

def shuffle(...)
  __with__(@__value__.shuffle(...))
end

#shuffle!Object



528
529
530
531
# File 'lib/literal/array.rb', line 528

def shuffle!(...)
  @__value__.shuffle!(...)
  self
end

#sizeObject



533
534
535
# File 'lib/literal/array.rb', line 533

def size(...)
  @__value__.size(...)
end

#sortObject



537
538
539
# File 'lib/literal/array.rb', line 537

def sort(...)
  __with__(@__value__.sort(...))
end

#sort!Object



541
542
543
544
# File 'lib/literal/array.rb', line 541

def sort!(...)
  @__value__.sort!(...)
  self
end

#sort_by!Object



546
547
548
549
# File 'lib/literal/array.rb', line 546

def sort_by!(...)
  @__value__.sort_by!(...)
  self
end

#sumObject



370
371
372
# File 'lib/literal/array.rb', line 370

def sum(...)
  @__value__.sum(...)
end

#takeObject



551
552
553
# File 'lib/literal/array.rb', line 551

def take(...)
  __with__(@__value__.take(...))
end

#take_whileObject



555
556
557
# File 'lib/literal/array.rb', line 555

def take_while(...)
  __with__(@__value__.take_while(...))
end

#to_aObject Also known as: to_ary



581
582
583
# File 'lib/literal/array.rb', line 581

def to_a
  @__value__.dup
end

#to_sObject



587
588
589
# File 'lib/literal/array.rb', line 587

def to_s
  @__value__.to_s
end

#transposeObject



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/literal/array.rb', line 559

def transpose
  case @__type__
  when Literal::Tuple::Generic
    tuple_types = @__type__.types
    new_array_types = tuple_types.map { |t| Literal::Array(t) }

    Literal::Tuple(*new_array_types).new(
      *new_array_types.each_with_index.map do |t, i|
        t.new(
          *@__value__.map { |it| it[i] }
        )
      end
    )
  when Literal::Array::Generic
    __with__(
      @__value__.map(&:to_a).transpose.map! { |it| @__type__.new(*it) }
    )
  else
    @__value__.transpose
  end
end

#uniqObject



591
592
593
# File 'lib/literal/array.rb', line 591

def uniq
  __with__(@__value__.uniq)
end

#uniq!Object



595
596
597
# File 'lib/literal/array.rb', line 595

def uniq!(...)
  @__value__.uniq!(...) ? self : nil
end

#unshift(value) ⇒ Object Also known as: prepend



599
600
601
602
603
604
605
606
# File 'lib/literal/array.rb', line 599

def unshift(value)
  Literal.check(value, @__type__) do |c|
    c.fill_receiver(receiver: self, method: "#unshift")
  end

  @__value__.unshift(value)
  self
end

#values_at(*indexes) ⇒ Object



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/literal/array.rb', line 610

def values_at(*indexes)
  unless @__type__ === nil
    max_value = length - 1
    min_value = -length

    indexes.each do |index|
      case index
      when Integer
        if index < min_value || index > max_value
          raise IndexError.new("index #{index} out of range")
        end
      when Range
        if index.begin < min_value || index.end > max_value
          raise IndexError.new("index #{index} out of range")
        end
      else
        raise ArgumentError.new("Invalid index: #{index.inspect}")
      end
    end
  end

  __with__(
    @__value__.values_at(*indexes)
  )
end

#zip(*others) ⇒ Object



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# File 'lib/literal/array.rb', line 662

def zip(*others)
  other_types = others.map do |other|
    case other
    when Literal::Array
      other.__type__
    when Array
      _Any?
    else
      raise ArgumentError
    end
  end

  tuple = Literal::Tuple(
    @__type__,
    *other_types
  )

  my_length = length
  max_length = [my_length, *others.map(&:length)].max

  # Check we match the max length or our type is nilable
  unless my_length == max_length || @__type__ === nil
    raise ArgumentError.new("      The literal array could not be zipped becuase its type is not nilable and it has fewer items than the maximum number of items in the other arrays.\n\n      You can either make the type of this array nilable, or add more items so its length matches the others.\n\n      \#{inspect}\n    MESSAGE\n  end\n\n  # Check others match the max length or their types is nilable\n  others.each_with_index do |other, index|\n    unless other.length == max_length || other_types[index] === nil\n      raise ArgumentError.new(<<~MESSAGE)\n        The literal array could not be zipped becuase its type is not nilable and it has fewer items than the maximum number of items in the other arrays.\n\n        You can either make the type of this array nilable, or add more items so its length matches the others.\n\n        \#{inspect}\n      MESSAGE\n    end\n  end\n\n  i = 0\n\n  if block_given?\n    while i < max_length\n      yield tuple.new(\n        @__value__[i],\n        *others.map { |it| it[i] }\n      )\n      i += 1\n    end\n\n    nil\n  else\n    result_value = []\n\n    while i < max_length\n      result_value << tuple.new(\n        @__value__[i],\n        *others.map { |it| it[i] }\n      )\n      i += 1\n    end\n\n    __with__(result_value)\n  end\nend\n")

#|(other) ⇒ Object



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/literal/array.rb', line 636

def |(other)
  case other
  when ::Array
    Literal.check(other, _Array(@__type__)) do |c|
      c.fill_receiver(receiver: self, method: "#|")
    end

    __with__(@__value__ | other)
  when Literal::Array(@__type__)
    __with__(@__value__ | other.__value__)
  when Literal::Array
    raise Literal::TypeError.new(
      context: Literal::TypeError::Context.new(
        expected: Literal::Array(@__type__),
        actual: other
      )
    )
  else
    raise ArgumentError.new("Cannot perform `|` with #{other.class.name}.")
  end
end