Skip to content

Instantly share code, notes, and snippets.

@pmoura
Last active June 21, 2018 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmoura/153a4dde11e12b2d820ab35acacd6e9d to your computer and use it in GitHub Desktop.
Save pmoura/153a4dde11e12b2d820ab35acacd6e9d to your computer and use it in GitHub Desktop.
Sane parametric object implementation of points and color points
% Logtalk parametric entity parameters are _logical variables_
% that are implicitly shared by all entity predicates.
%
% Parameters can be accessed using either the parameter/2 built-in
% predicate or by using _parameter variables_. Here we use the
% latter to decouple parameter access from parameter position.
%
% As ancestors should not force a specific parameter order on the
% descendants (which would be a code smell), each prototype provides
% a protected predicate (that can be overriden) for constructing a
% new object identifier term from new values for its characteristic
% parameters.
:- object(point(_X_,_Y_)).
:- public(multiply/2).
multiply(F, New) :-
X is _X_ * F,
Y is _Y_ * F,
::with_position(X, Y, New).
:- public(x/1).
x(_X_).
:- public(with_x/2).
with_x(X, New) :-
::with_position(X, _Y_, New).
:- public(y/1).
y(_Y_).
:- public(with_y/2).
with_y(Y, New) :-
::with_position(_X_, Y, New).
:- protected(with_position/3).
with_position(X, Y, point(X, Y)).
:- end_object.
:- object(colorpoint(_X_,_Y_,_C_),
extends(point(_X_,_Y_))).
:- public(invert/1).
invert(New) :-
C is 255 - _C_,
::with_color(C, New).
:- public(color/1).
color(_C_).
:- protected(with_color/2).
with_color(C, colorpoint(_X_, _Y_, C)).
with_position(X, Y, colorpoint(X, Y, _C_)).
:- end_object.
:- object(namedcolorpoint(_N_,_C_,_X_,_Y_),
extends(colorpoint(_X_,_Y_,_C_))).
:- public(name/1).
name(_N_).
:- protected(with_name/2).
with_name(N, namedcolorpoint(N, _C_, _X_, _Y_)).
with_color(C, namedcolorpoint(_N_, C, _X_, _Y_)).
with_position(X, Y, namedcolorpoint(_N_, _C_, X, Y)).
:- end_object.
@pmoura
Copy link
Author

pmoura commented Jun 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment