RosettaCodeData/Task/Vector/Arturo/vector.arturo

38 lines
696 B
Plaintext

define :vector [
x y
][
print: -> render "(|this\x|, |this\y|)" ; prettyprint function
]
ensureVector: function [block][
ensure -> every? @block => [is? :vector &]
]
vadd: function [a b][
ensureVector [a b]
to :vector @[a\x + b\x, a\y + b\y]
]
vsub: function [a b][
ensureVector [a b]
to :vector @[a\x - b\x, a\y - b\y]
]
vmul: function [a n][
ensureVector [a]
to :vector @[a\x * n, a\y * n]
]
vdiv: function [a n][
ensureVector [a]
to :vector @[a\x // n, a\y // n]
]
; test our vector object
a: to :vector [5 7]
b: to :vector [2 3]
print [a '+ b '= vadd a b]
print [a '- b '= vsub a b]
print [a '* 11 '= vmul a 11]
print [a '/ 11 '= vdiv a 2]