Logo

Programming-Idioms

History of Idiom 113 > diff from v31 to v32

Edit summary for version 32 by programming-idioms.org:
[Prolog] Remove doc (not the expected doc)

Version 31

2020-05-03, 19:54:50

Version 32

2020-05-21, 20:48:23

Idiom #113 Iterate over map entries, ordered by values

Print each key k with its value x from an associative array mymap, in ascending order of x.
Note that multiple entries may exist for the same value x.

Idiom #113 Iterate over map entries, ordered by values

Print each key k with its value x from an associative array mymap, in ascending order of x.
Note that multiple entries may exist for the same value x.

Variables
k,x,mymap
Variables
k,x,mymap
Extra Keywords
traverse traversal
Extra Keywords
traverse traversal
Code
dict_pairs(D, _, Ps),
transpose_pairs(Ps, TPs),
keysort(TPs, Val_sorted),
forall(member(V-K, Val_sorted),
       format("~w:~w~n", [V, K]))
Code
dict_pairs(D, _, Ps),
transpose_pairs(Ps, TPs),
keysort(TPs, Val_sorted),
forall(member(V-K, Val_sorted),
       format("~w:~w~n", [V, K]))
Comments bubble
Transpose the key-value pair list, sort by key (a stable sort), then print
Comments bubble
Transpose the key-value pair list, sort by key (a stable sort), then print
Doc URL
swi-prolog.org
Demo URL
http://swish.swi-prolog.org/p/WKeHXAkd.swinb
Demo URL
http://swish.swi-prolog.org/p/WKeHXAkd.swinb