Add some tuple utilities.
This commit is contained in:
parent
d1fc0c3241
commit
75a318c567
|
@ -19,3 +19,33 @@ class Array(T)
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
struct Tuple(*T)
|
||||
def add(other : Tuple(*T))
|
||||
{% begin %}
|
||||
{ {% for t, i in T %} self[{{i}}] + other[{{i}}], {% end %} }
|
||||
{% end %}
|
||||
end
|
||||
|
||||
def neg
|
||||
{% begin %}
|
||||
{ {% for t, i in T %} -self[{{i}}], {% end %} }
|
||||
{% end %}
|
||||
end
|
||||
|
||||
def sub(other : Tuple(*T))
|
||||
add other.neg
|
||||
end
|
||||
|
||||
def dot(other : Tuple(*T))
|
||||
{% begin %}
|
||||
{% for t, i in T %} self[{{i}}] * other[{{i}}] + {% end %} 0
|
||||
{% end %}
|
||||
end
|
||||
|
||||
def reduce_fraction
|
||||
gcd = self[0].gcd self[1]
|
||||
return self if gcd == 0
|
||||
{ self[0]//gcd, self[1]//gcd}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user