-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHANGED: Rewrite of the counter module API.
Changes in module `counter`: - Renamed `create_counter/1` to `counter_create/1`. - Renamed `increment_counter/1` to `counter_increment/1`. - Added `counter_increment/2`. - Reimplemented by using module `nb_ext`. Additions to module `nb_ext`: - Added `nb_increment/3`. - Added `nb_plus/4`.
- Loading branch information
1 parent
30d393c
commit 87dd2df
Showing
2 changed files
with
40 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,40 @@ | ||
:- module( | ||
counter, | ||
[ | ||
counter_value/2, % +Counter, ?N | ||
create_counter/1, % -Counter | ||
increment_counter/1 % +Counter | ||
counter_create/1, % -Counter | ||
counter_increment/1, % +Counter | ||
counter_increment/2, % +Counter, -Value | ||
counter_value/2 % +Counter, ?Value | ||
] | ||
). | ||
|
||
/** <module> A simple counter | ||
*/ | ||
|
||
:- use_module(library(nb_ext)). | ||
|
||
|
||
|
||
%! counter_create(-Counter:compound) is det. | ||
|
||
%! counter_value(+Counter:compound, +N:nonneg) is semidet. | ||
%! counter_value(+Counter:compound, -N:nonneg) is det. | ||
counter_create(counter(0)). | ||
|
||
counter_value(counter(N), N). | ||
|
||
|
||
%! counter_increment(+Counter:compound) is det. | ||
%! counter_increment(+Counter:compound, -Value:nonneg) is det. | ||
|
||
%! create_counter(-Counter:compound) is det. | ||
counter_increment(Counter) :- | ||
counter_increment(Counter, _). | ||
|
||
create_counter(counter(0)). | ||
|
||
counter_increment(Counter, Value) :- | ||
nb_increment(Counter, 1, Value). | ||
|
||
|
||
%! increment_counter(+Counter:compound) is det. | ||
|
||
increment_counter(Counter) :- | ||
arg(1, Counter, N1), | ||
N2 is N1 + 1, | ||
nb_setarg(1, Counter, N2). | ||
%! counter_value(+Counter:compound, +N:nonneg) is semidet. | ||
%! counter_value(+Counter:compound, -N:nonneg) is det. | ||
|
||
counter_value(counter(N), N). |
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