Hi,
How would we do a reverse sorted map?
I find in Java:
TreeMap(Comparator<? super K> comparator)
Constructs a new, empty tree map, ordered
according to the given comparator. https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
Or in Dogelog Player:
tree_new(T):
tree_new(T, F):
The predicate succeeds in R with a new red-black tree.
The binary predicate allows specifying a term compare F. https://www.dogelog.ch/typtab/doclet/book/12_lang/05_libraries/03_util/06_tree.html
Here is an example, using the destructive API. But
the same constructor works also for the non-destructive API.
?- tree_new(_T), tree_add(_T, 0rInf, foo),
-a-a tree_add(_T, 0rNaN, bar), tree_pairs(_T, L).
L = [0rNaN-bar, 0rInf-foo].
And now using a comparator modifier, aggregate with a comparator,
as a closure. Some Joy of Higher Order logic programming:
reverse(C, R, X, Y) :- call(C, R, Y, X).
?- tree_new(_T,reverse(compare)), tree_add(_T, 0rInf, foo),
-a-a tree_add(_T, 0rNaN, bar), tree_pairs(_T, L).
L = [0rInf-foo, 0rNaN-bar].
?- tree_new(_T,reverse(reverse(compare))), tree_add(_T, 0rInf, foo),
-a-a tree_add(_T, 0rNaN, bar), tree_pairs(_T, L).
L = [0rNaN-bar, 0rInf-foo].
Just toying around with my new NaNs.
Have Fun!
Bye
Mild Shock schrieb:
Hi,
Functional requirement:
?- Y = g(_,_), X = f(Y,C,D,Y), term_singletons(X, L),
-a-a-a L == [C,D].
?- Y = g(A,X,B), X = f(Y,C,D), term_singletons(X, L),
-a-a-a L == [A,B,C,D].
Non-Functional requirement:
?- member(N,[5,10,15]), time(singletons(N)), fail; true.
% Zeit 1 ms, GC 0 ms, Lips 4046000, Uhr 11.08.2025 01:36
% Zeit 3 ms, GC 0 ms, Lips 1352000, Uhr 11.08.2025 01:36
% Zeit 3 ms, GC 0 ms, Lips 1355333, Uhr 11.08.2025 01:36
true.
Can your Prolog system do that?
P.S.: Benchmark was:
singletons(N) :-
-a-a-a hydra2(N,Y),
-a-a-a between(1,1000,_), term_singletons(Y,_), fail; true.
hydra2(0, _) :- !.
hydra2(N, s(X,X)) :-
-a-a-a M is N-1,
-a-a-a hydra2(M, X).
Bye
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 01:49:32 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
10 files (20,373K bytes) |
| Messages: | 264,321 |