Merge and aggregate datasets: Difference between revisions
Refactor the aggregates
Thundergnat (talk | contribs) 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 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) :-
unsorted_aggregate(Predicate, Aggregator, Initial, Result)).▼
bag_count(Predicate, Count) :-
▲ promise_equivalent_solutions[Count] (
▲ unsorted_aggregate(Predicate,
▲ (pred(_X::in,Y::in,Z::out) is det :- Z is Y+1),
bag_sum(Predicate, Sum) :-
bag_aggr(Predicate, (pred(X::in,Y::in,Z::out) is det :- Z is X+Y), 0.0, Sum).
▲ (pred(X::in,Y::in,Z::out) is det :- Z is X+Y),
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,
(pred(X::in,Y::in,Z::out) is det :-
▲ (if R = (>) then Z = X else Z = Y)),
bag_max_date(Predicate, MaxDate) :-
▲ Initial, Max)).
bag_max(Predicate, date(0,0,0), 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(_,_,_)),
▲ (if MaxDate1 = date(0,0,0) then MaxDate = no else MaxDate = MaxDate1)),
Solutions),
foldl(io.write_line, Solutions, !IO).</syntaxhighlight>
|