-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathtuple.cr
774 lines (727 loc) · 19.1 KB
/
tuple.cr
1
2
3
4
5
6
7
8
9
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# A tuple is a fixed-size, immutable, stack-allocated sequence of values
# of possibly different types.
#
# You can think of a `Tuple` as an immutable `Array` whose types for each position
# are known at compile time.
#
# A tuple can be created with the usual `new` method or with a tuple literal:
#
# ```
# tuple = {1, "hello", 'x'} # Tuple(Int32, String, Char)
# tuple[0] # => 1
# tuple[1] # => "hello"
# tuple[2] # => 'x'
# ```
#
# See [`Tuple` literals](https://crystal-lang.org/reference/syntax_and_semantics/literals/tuple.html) in the language reference.
#
# The compiler knows what types are in each position, so when indexing
# a tuple with an integer literal the compiler will return
# the value in that index and with the expected type, like in the above
# snippet. Indexing with an integer literal outside the bounds of the tuple
# will give a compile-time error.
#
# Indexing with an integer value that is only known at runtime will return
# a value whose type is the union of all the types in the tuple, and might raise
# `IndexError`.
#
# Indexing with `#[]?` does not make the return value nilable if the index is
# known to be within bounds:
#
# ```
# tuple = {1, "hello", 'x'}
# tuple[0]? # => 1
# typeof(tuple[0]?) # => Int32
# ```
#
# Indexing with a range literal known at compile-time is also allowed, and the
# returned value will have the correct sub-tuple type:
#
# ```
# tuple = {1, "hello", 'x'} # Tuple(Int32, String, Char)
# sub = tuple[0..1] # => {1, "hello"}
# typeof(sub) # => Tuple(Int32, String)
# ```
#
# `Tuple`'s own instance classes may also be indexed in a similar manner,
# returning their element types instead:
#
# ```
# tuple = Tuple(Int32, String, Char)
# tuple[0] # => Int32
# tuple[3]? # => nil
# tuple[1..] # => Tuple(String, Char)
# ```
#
# Tuples are the preferred way to return fixed-size multiple return
# values because no memory is needed to be allocated for them:
#
# ```
# def one_and_hello
# {1, "hello"}
# end
#
# one, hello = one_and_hello
# one # => 1
# hello # => "hello"
# ```
#
# Good examples of the above are `Number#divmod` and `Enumerable#minmax`.
#
# Tuples can be splat with the `*` operator and passed to methods:
#
# ```
# def multiply(string, value)
# string * value
# end
#
# tuple = {"hey", 2}
# value = multiply(*tuple) # same as multiply tuple[0], tuple[1]
# value # => "heyhey"
# ```
#
# Finally, when using a splat argument in a method definition its type
# will be a tuple of the call arguments:
#
# ```
# def splat_test(*args)
# args
# end
#
# tuple = splat_test 1, "hello", 'x'
# tuple.class # => Tuple(Int32, String, Char)
# tuple # => {1, "hello", 'x'}
# ```
struct Tuple
include Indexable(Union(*T))
include Comparable(Tuple)
# Creates a tuple that will contain the given values.
#
# This method is useful in macros and generic code because with it you can
# create empty tuples, something that you can't do with a tuple literal.
#
# ```
# Tuple.new(1, "hello", 'x') #=> {1, "hello", 'x'}
# Tuple.new #=> {}
#
# {} # syntax error
# ```
def self.new(*args : *T)
{% if @type.name(generic_args: false) == "Tuple" %}
# deduced type vars
args
{% elsif @type.name(generic_args: false) == "Tuple()" %}
# special case: empty tuple
# TODO: check against `Tuple()` directly after 1.4.0
args
{% else %}
# explicitly provided type vars
# following `typeof` is needed to access private types
{% begin %}
{
{% for i in 0...@type.size %}
args[{{ i }}].as(typeof(element_type({{ i }}))),
{% end %}
}
{% end %}
{% end %}
end
# Creates a tuple from the given array, with elements casted to the given types.
#
# ```
# Tuple(String, Int64).from(["world", 2_i64]) # => {"world", 2_i64}
# Tuple(String, Int64).from(["world", 2_i64]).class # => Tuple(String, Int64)
# ```
#
# See also: `#from`.
def self.from(array : Array) : self
{% begin %}
Tuple.new(*{{T}}).from(array)
{% end %}
end
# Expects to be called on a tuple of types, creates a tuple from the given array,
# with types casted appropriately.
#
# This allows you to easily pass an array as individual arguments to a method.
#
# ```
# require "json"
#
# def speak_about(thing : String, n : Int64)
# "I see #{n} #{thing}s"
# end
#
# data = JSON.parse(%(["world", 2])).as_a.map(&.raw)
# speak_about(*{String, Int64}.from(data)) # => "I see 2 worlds"
# ```
def from(array : Array)
if size != array.size
raise ArgumentError.new "Expected array of size #{size} but one of size #{array.size} was given."
end
{% begin %}
Tuple.new(
{% for i in 0...@type.size %}
self[{{i}}].cast(array[{{i}}]),
{% end %}
)
{% end %}
end
def unsafe_fetch(index : Int)
self[index]
end
# Returns the element at the given *index*. Read the type docs to understand
# the difference between indexing with a number literal or a variable.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple[0] # => 1
# typeof(tuple[0]) # => Int32
# tuple[3] # Error: index out of bounds for Tuple(Int32, String, Char) (3 not in -3..2)
#
# i = 0
# tuple[i] # => 1
# typeof(tuple[i]) # => (Char | Int32 | String)
#
# i = 3
# tuple[i] # raises IndexError
# ```
def [](index : Int)
at(index)
end
# Returns the element type at the given *index*. Read the type docs to
# understand the difference between indexing with a number literal or a
# variable.
#
# ```
# alias Foo = Tuple(Int32, String)
# Foo[0] # => Int32
# Foo[0].zero # => 0
# Foo[2] # Error: index out of bounds for Tuple(Int32, String).class (2 not in -2..1)
#
# i = 0
# Foo[i] # => Int32
# Foo[i].zero # Error: undefined method 'zero' for String.class (compile-time type is (Int32.class | String.class))
#
# i = 2
# Foo[i] # raises IndexError
# ```
def self.[](index : Int)
self[index]? || raise IndexError.new
end
# Returns the element at the given *index* or `nil` if out of bounds. Read the
# type docs to understand the difference between indexing with a number
# literal or a variable.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple[0]? # => 1
# typeof(tuple[0]?) # => Int32
# tuple[3]? # => nil
# typeof(tuple[3]?) # => Nil
#
# i = 0
# tuple[i]? # => 1
# typeof(tuple[i]?) # => (Char | Int32 | String | Nil)
#
# i = 3
# tuple[i]? # => nil
# ```
def []?(index : Int)
at(index) { nil }
end
# Returns the element type at the given *index* or `nil` if out of bounds.
# Read the type docs to understand the difference between indexing with a
# number literal or a variable.
#
# ```
# alias Foo = Tuple(Int32, String)
# Foo[0]? # => Int32
# Foo[0]?.zero # => 0
# Foo[2]? # => nil
# typeof(Foo[2]?) # => Nil
#
# i = 0
# Foo[i]? # => Int32
# Foo[i]?.zero # Error: undefined method 'zero' for String.class (compile-time type is (Int32.class | String.class | Nil))
#
# i = 2
# Foo[i]? # => nil
# ```
def self.[]?(index : Int)
# following `typeof` is needed to access private types
{% begin %}
case index
{% for i in 0...T.size %}
when {{ i }}, {{ i - T.size }}
typeof(element_type({{ i }}))
{% end %}
end
{% end %}
end
# Returns all elements that are within the given *range*. *range* must be a
# range literal whose value is known at compile-time.
#
# Negative indices count backward from the end of the tuple (-1 is the last
# element). Additionally, an empty tuple is returned when the starting index
# for an element range is at the end of the tuple.
#
# Raises a compile-time error if `range.begin` is out of range.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple[0..1] # => {1, "hello"}
# tuple[-2..] # => {"hello", 'x'}
# tuple[...1] # => {1}
# tuple[4..] # Error: begin index out of bounds for Tuple(Int32, String, Char) (4 not in -3..3)
#
# i = 0
# tuple[i..2] # Error: Tuple#[](Range) can only be called with range literals known at compile-time
#
# i = 0..2
# tuple[i] # Error: Tuple#[](Range) can only be called with range literals known at compile-time
# ```
def [](range : Range)
{% raise "Tuple#[](Range) can only be called with range literals known at compile-time" %}
end
# Returns all element types that are within the given *range*. *range* must be
# a range literal whose value is known at compile-time.
#
# Negative indices count backward from the end of the tuple (-1 is the last
# element). Additionally, an empty tuple is returned when the starting index
# for an element range is at the end of the tuple.
#
# Raises a compile-time error if `range.begin` is out of range.
#
# ```
# alias Foo = Tuple(Int32, String, Char)
# Foo[0..1] # => Tuple(Int32, String)
# Foo[-2..] # => Tuple(String, Char)
# Foo[...1] # => Tuple(Int32)
# Foo[4..] # Error: begin index out of bounds for Tuple(Int32, String, Char).class (4 not in -3..3)
#
# i = 0
# Foo[i..2] # Error: Tuple.[](Range) can only be called with range literals known at compile-time
#
# i = 0..2
# Foo[i] # Error: Tuple.[](Range) can only be called with range literals known at compile-time
# ```
def self.[](range : Range)
{% raise "Tuple.[](Range) can only be called with range literals known at compile-time" %}
end
# Returns the element at the given *index* or raises IndexError if out of bounds.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple.at(0) # => 1
# tuple.at(3) # raises IndexError
# ```
def at(index : Int)
at(index) { raise IndexError.new }
end
# Returns the element at the given *index* or the value returned by the block if
# out of bounds.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple.at(0) { 10 } # => 1
# tuple.at(3) { 10 } # => 10
# ```
def at(index : Int, &)
index += size if index < 0
{% for i in 0...T.size %}
return self[{{i}}] if {{i}} == index
{% end %}
yield
end
# Yields each of the elements in this tuple.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple.each do |value|
# puts value
# end
# ```
#
# Output:
#
# ```text
# 1
# "hello"
# 'x'
# ```
def each(& : Union(*T) ->) : Nil
{% for i in 0...T.size %}
yield self[{{i}}]
{% end %}
end
# Returns `true` if this tuple has the same size as the other tuple
# and their elements are equal to each other when compared with `==`.
#
# ```
# t1 = {1, "hello"}
# t2 = {1.0, "hello"}
# t3 = {2, "hello"}
#
# t1 == t2 # => true
# t1 == t3 # => false
# ```
def ==(other : self)
{% for i in 0...T.size %}
return false unless self[{{i}}] == other[{{i}}]
{% end %}
true
end
# :ditto:
def ==(other : Tuple)
return false unless size == other.size
size.times do |i|
return false unless self[i] == other[i]
end
true
end
def ==(other)
false
end
# Returns `true` if case equality holds for the elements in `self` and *other*.
#
# ```
# {1, 2} === {1, 2} # => true
# {1, 2} === {1, 3} # => false
# ```
#
# See also: `Object#===`.
def ===(other : self)
{% for i in 0...T.size %}
return false unless self[{{i}}] === other[{{i}}]
{% end %}
true
end
# Returns `true` if `self` and *other* have the same size and case equality holds
# for the elements in `self` and *other*.
#
# ```
# {1, 2} === {1, 2, 3} # => false
# {/o+/, "bar"} === {"foo", "bar"} # => true
# ```
#
# See also: `Object#===`.
def ===(other : Tuple)
return false unless size == other.size
size.times do |i|
return false unless self[i] === other[i]
end
true
end
# The comparison operator.
#
# Each object in each tuple is compared using the `<=>` operator.
#
# Tuples are compared in an "element-wise" manner; the first element of this tuple is
# compared with the first one of *other* using the `<=>` operator, then each of the second elements,
# etc. As soon as the result of any such comparison is non-zero
# (i.e. the two corresponding elements are not equal), that result is returned for the whole tuple comparison.
#
# If all the elements are equal, then the result is based on a comparison of the tuple sizes.
# Thus, two tuples are "equal" according to `<=>` if, and only if, they have the same size
# and the value of each element is equal to the value of the corresponding element in the other tuple.
#
# ```
# {"a", "a", "c"} <=> {"a", "b", "c"} # => -1
# {1, 2, 3, 4, 5, 6} <=> {1, 2} # => 1
# {1, 2} <=> {1, 2.0} # => 0
# ```
def <=>(other : self)
{% for i in 0...T.size %}
cmp = self[{{i}}] <=> other[{{i}}]
return cmp unless cmp == 0
{% end %}
0
end
# :ditto:
def <=>(other : Tuple)
min_size = Math.min(size, other.size)
min_size.times do |i|
cmp = self[i] <=> other[i]
return cmp unless cmp == 0
end
size <=> other.size
end
# See `Object#hash(hasher)`
def hash(hasher)
{% for i in 0...T.size %}
hasher = self[{{i}}].hash(hasher)
{% end %}
hasher
end
# Returns a tuple containing cloned elements of this tuple using the `clone` method.
def clone
{% begin %}
Tuple.new(
{% for i in 0...T.size %}
self[{{i}}].clone,
{% end %}
)
{% end %}
end
# Returns a tuple that contains `self`'s elements followed by *other*'s elements.
#
# ```
# t1 = {1, 2}
# t2 = {"foo", "bar"}
# t3 = t1 + t2
# t3 # => {1, 2, "foo", "bar"}
# typeof(t3) # => Tuple(Int32, Int32, String, String)
# ```
def +(other : Tuple)
plus_implementation(other)
end
private def plus_implementation(other : U) forall U
{% begin %}
Tuple.new(
{% for i in 0...@type.size %}
self[{{i}}],
{% end %}
{% for i in 0...U.size %}
other[{{i}}],
{% end %}
)
{% end %}
end
# Returns the number of elements in this tuple.
#
# ```
# {'a', 'b'}.size # => 2
# ```
def size
{{T.size}}
end
# Returns the types of this tuple type.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple.class.types # => {Int32, String, Char}
# ```
def self.types
Tuple.new(*{{T}})
end
# Same as `to_s`.
def inspect : String
to_s
end
# Returns an `Array` with all the elements in the tuple.
#
# ```
# {1, 2, 3, 4, 5}.to_a # => [1, 2, 3, 4, 5]
# ```
def to_a : Array(Union(*T))
super
end
# Returns an `Array` with the results of running *block* against each element of the tuple.
#
# ```
# {1, 2, 3, 4, 5}).to_a { |i| i * 2 } # => [2, 4, 6, 8, 10]
# ```
def to_a(& : Union(*T) -> U) forall U
Array(U).build(size) do |buffer|
{% for i in 0...T.size %}
buffer[{{i}}] = yield self[{{i}}]
{% end %}
size
end
end
# Returns a `StaticArray` with the same elements.
#
# The element type is `Union(*T)`.
#
# ```
# {1, 'a', true}.to_static_array # => StaticArray[1, 'a', true]
# ```
@[AlwaysInline]
def to_static_array : StaticArray
{% begin %}
ary = uninitialized StaticArray(Union(*T), {{ T.size }})
each_with_index do |value, i|
ary.to_unsafe[i] = value
end
ary
{% end %}
end
# Appends a string representation of this tuple to the given `IO`.
#
# ```
# tuple = {1, "hello"}
# tuple.to_s # => "{1, \"hello\"}"
# ```
def to_s(io : IO) : Nil
io << '{'
join io, ", ", &.inspect(io)
io << '}'
end
def pretty_print(pp) : Nil
pp.list("{", self, "}")
end
# Returns a new tuple where elements are mapped by the given block.
#
# ```
# tuple = {1, 2.5, "a"}
# tuple.map &.to_s # => {"1", "2.5", "a"}
# ```
def map(& : Union(*T) ->)
{% begin %}
Tuple.new(
{% for i in 0...T.size %}
(yield self[{{i}}]),
{% end %}
)
{% end %}
end
# Like `map`, but the block gets passed both the element and its index.
#
# ```
# tuple = {1, 2.5, "a"}
# tuple.map_with_index { |e, i| "tuple[#{i}]: #{e}" } # => {"tuple[0]: 1", "tuple[1]: 2.5", "tuple[2]: a"}
# ```
#
# Accepts an optional *offset* parameter, which tells it to start counting
# from there.
def map_with_index(offset = 0, &)
{% begin %}
Tuple.new(
{% for i in 0...T.size %}
(yield self[{{i}}], offset + {{i}}),
{% end %}
)
{% end %}
end
# Returns a new tuple where the elements are in reverse order.
#
# ```
# tuple = {1, 2.5, "a"}
# tuple.reverse # => {"a", 2.5, 1}
# ```
def reverse
{% begin %}
Tuple.new(
{% for i in 1..T.size %}
self[{{T.size - i}}],
{% end %}
)
{% end %}
end
# Yields each of the elements in this tuple in reverse order.
#
# ```
# tuple = {1, "hello", 'x'}
# tuple.reverse_each do |value|
# puts value
# end
# ```
#
# Output:
#
# ```text
# 'x'
# "hello"
# 1
# ```
def reverse_each(& : Union(*T) ->)
{% for i in 1..T.size %}
yield self[{{T.size - i}}]
{% end %}
nil
end
# :inherit:
def reduce(&)
{% if T.empty? %}
raise Enumerable::EmptyError.new
{% else %}
memo = self[0]
{% for i in 1...T.size %}
memo = yield memo, self[{{ i }}]
{% end %}
memo
{% end %}
end
# :inherit:
def reduce(memo, &)
{% for i in 0...T.size %}
memo = yield memo, self[{{ i }}]
{% end %}
memo
end
# :inherit:
def reduce?(&)
{% unless T.empty? %}
reduce { |memo, elem| yield memo, elem }
{% end %}
end
# Returns the first element of this tuple. Doesn't compile
# if the tuple is empty.
#
# ```
# tuple = {1, 2.5}
# tuple.first # => 1
# ```
def first
self[0]
end
# Returns the first element of this tuple, or `nil` if this
# is the empty tuple.
#
# ```
# tuple = {1, 2.5}
# tuple.first? # => 1
#
# empty = Tuple.new
# empty.first? # => nil
# ```
def first?
{% if T.size == 0 %}
nil
{% else %}
self[0]
{% end %}
end
# Returns the last element of this tuple. Doesn't compile
# if the tuple is empty.
#
# ```
# tuple = {1, 2.5}
# tuple.last # => 2.5
# ```
def last
{% begin %}
self[{{T.size - 1}}]
{% end %}
end
# Returns the last element of this tuple, or `nil` if this
# is the empty tuple.
#
# ```
# tuple = {1, 2.5}
# tuple.last? # => 2.5
#
# empty = Tuple.new
# empty.last? # => nil
# ```
def last?
{% if T.size == 0 %}
nil
{% else %}
self[{{T.size - 1}}]
{% end %}
end
# Returns a value with the same type as the element at the given *index* of
# an instance of `self`. *index* must be an integer or range literal known at
# compile-time.
#
# The most common usage of this macro is to extract the appropriate element
# type in `Tuple`'s class methods. This macro works even if the corresponding
# element type is private.
#
# NOTE: there should never be a need to call this method outside the standard library.
private macro element_type(index)
x = uninitialized self
x[{{ index }}]
end
end