Merge and aggregate datasets: Difference between revisions

Refactor the aggregates
m (syntax highlighting fixup automation)
(Refactor the aggregates)
Line 1,137:
 
%% Utilities
:- pred bag_aggr(pred(T)::(pred(out) is nondet), pred(T,U,U)::pred(in,in,out) is det,
(pred(XU::in,Y::in,Z U::out) is det :- Z is X+Y),.
:- pred bag_count(pred(T)::(pred(out) is nondet), int::out) is det.
:- pred bag_sum(pred(float)::(pred(out) is nondet), float::out) is det.
:- pred bag_avg(pred(float)::(pred(out) is nondet), float::out) is det.
:- pred bag_max(pred(T)::(pred(out) is nondet), T::in, T::out) is det.
:- pred bag_max_date(pred(maybe_date)::(pred(out) is nondet), maybe_date::out) is det.
bag_aggr(Predicate, Aggregator, Initial, Result) :-
promise_equivalent_solutions[CountResult] (
unsorted_aggregate(Predicate, Aggregator, Initial, Result)).
bag_count(Predicate, Count) :-
bag_aggr(Predicate, (pred(_X::in,Y::in,Z::out) is det :- Z is Y+1), 0, Count).
promise_equivalent_solutions[Count] (
unsorted_aggregate(Predicate,
(pred(_X::in,Y::in,Z::out) is det :- Z is Y+1),
0, Count)).
bag_sum(Predicate, Sum) :-
bag_aggr(Predicate, (pred(X::in,Y::in,Z::out) is det :- Z is X+Y), 0.0, Sum).
promise_equivalent_solutions[Sum] (
unsorted_aggregate(Predicate,
(pred(X::in,Y::in,Z::out) is det :- Z is X+Y),
0.0, Sum)).
bag_avg(Predicate, Avg) :-
bag_count(Predicate, N),
Line 1,156:
(if N = 0 then Avg = nan else Avg is Sum/float(N)).
bag_max(Predicate, Initial, Max) :-
bag_aggr(Predicate,
promise_equivalent_solutions [Max] (
(pred(X::in,Y::in,Z::out) is det :-
unsorted_aggregate(Predicate,
(predcompare(R,X::in,Y::in),Z::out) is det :-
(if R = (>) then Z = X else Z = Y)),
compare(R,X,Y),
Initial, Max)).
(if R = (>) then Z = X else Z = Y)),
bag_max_date(Predicate, MaxDate) :-
Initial, Max)).
bag_max(Predicate, date(0,0,0), MaxDate1),
(if MaxDate1 = date(0,0,0) then MaxDate = no else MaxDate = MaxDate1)),.
 
main(!IO) :-
Line 1,169 ⟶ 1,171:
Scores = (pred(Score::out) is nondet :- visit(Id,_,Score), \+is_nan(Score)),
bag_avg(Scores, Avg),
bag_sum(Scores, Sum),
Dates = (pred(Date::out) is nondet :- visit(Id,Date,_), Date=date(_,_,_)),
bag_maxbag_max_date(Dates, date(0,0,0MaxDate), MaxDate1),
(if MaxDate1 = date(0,0,0) then MaxDate = no else MaxDate = MaxDate1)),
Solutions),
foldl(io.write_line, Solutions, !IO).</syntaxhighlight>
136

edits