-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checker: fix assign check, when rechecking for another concrete type (v…
- Loading branch information
Showing
5 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module main | ||
|
||
pub struct Vector[T] { | ||
n int | ||
values []T | ||
} | ||
|
||
pub fn Vector.new[T](v []T) Vector[T] { | ||
return Vector[T]{ | ||
n: v.len | ||
values: v | ||
} | ||
} | ||
|
||
pub fn (v Vector[T]) str() string { | ||
return v.values.str() | ||
} | ||
|
||
pub fn (v Vector[T]) + (u Vector[T]) Vector[T] { | ||
assert v.n == u.n | ||
return Vector[T]{ | ||
n: v.n | ||
values: []T{len: v.n, init: T(v.values[index] + u.values[index])} | ||
} | ||
} | ||
|
||
pub fn (v Vector[T]) dot(u Vector[T]) T { | ||
assert v.n == u.n | ||
mut sum := T(0) | ||
for i in 0 .. v.n { | ||
sum += v.values[i] * u.values[i] | ||
} | ||
return sum | ||
} | ||
|
||
fn test_main() { | ||
v := Vector.new[f64]([1.0, 2, 3]) | ||
vdotv := v.dot(v) | ||
assert vdotv == 14.0 | ||
|
||
u := Vector.new[int]([1, 2, 3]) | ||
udotu := u.dot(u) | ||
assert udotu == 14 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters