Here is some sample code in which the methods / functions
rotr,
rotl,
noop all have the same semantics. Which approach is preferable and why?
# Exploration of data methods and functions.
# data type defining a triple of values and its methods
data Triple:
| no-triple with:
method rotr(self): no-triple end,
method rotl(self): no-triple end,
method noop(self): no-triple end,
| triple(a, b, c) with:
method rotr(self): triple(self.c, self.a, self.b) end,
method rotl(self): triple(self.b, self.c, self.a) end,
method noop(self): triple(self.a, self.b, self.c) end,
where:
no-triple satisfies is-Triple
no-triple satisfies is-no-triple
triple('1', '2', '3') satisfies is-Triple
triple('1', '2', '3') satisfies is-triple
no-triple.rotr() is no-triple
no-triple.rotl() is no-triple
no-triple.noop() is no-triple
triple('1', '2', '3').rotr() is triple('3', '1', '2')
triple('1', '2', '3').rotl() is triple('2', '3', '1')
triple('1', '2', '3').noop() is triple('1', '2', '3')
end
fun rotr(trip):
doc: 'return trip rotated right'
cases (Triple) trip:
| no-triple => no-triple
| triple(x, y, z) => triple(z, x, y)
end
where:
rotr(no-triple) is no-triple
rotr(triple(1, 2, 3)) is triple(3, 1, 2)
end
fun rotl(trip):
doc: 'return trip rotated left'
cases (Triple) trip:
| no-triple => no-triple
| triple(x, y, z) => triple(y, z, x)
end
where:
rotl(no-triple) is no-triple
rotl(triple(1, 2, 3)) is triple(2, 3, 1)
end
fun noop(trip):
doc: 'return trip unchanged'
cases (Triple) trip:
| no-triple => no-triple
| triple(x, y, z) => triple(x, y, z)
end
where:
noop(no-triple) is no-triple
noop(triple(1, 2, 3)) is triple(1, 2, 3)
end
Thanks.
- dcp