Logo

Programming-Idioms

History of Idiom 112 > diff from v9 to v10

Edit summary for version 10 by :
[Haskell] original author mixed up forM_ with mapM_...

Version 9

2016-02-17, 04:09:33

Version 10

2016-02-18, 16:58:03

Idiom #112 Iterate over map entries, ordered by keys

Print each key k with its value x from an associative array mymap, in ascending order of k.

Idiom #112 Iterate over map entries, ordered by keys

Print each key k with its value x from an associative array mymap, in ascending order of k.

Imports
import Data.Map as Map
Imports
import Data.Map as Map
Code
mapM_ print (Map.toList mymap)
Code
mapM_ print (Map.toList mymap)
Comments bubble
Data.Map.Map is an ordered map, so Map.toList will give the elements in ascending order by key.
Comments bubble
Data.Map.Map is an ordered map, so Map.toList will give the elements in ascending order by key.
Imports
import std.algorithm;
import std.array;
import std.stdio;
Imports
import std.algorithm;
import std.array;
import std.stdio;
Code
mymap.byKeyValue
     .array
     .sort!((a, b) => a.key < b.key)
     .each!(p => writeln(p.key, " ", p.value));
Code
mymap.byKeyValue
     .array
     .sort!((a, b) => a.key < b.key)
     .each!(p => writeln(p.key, " ", p.value));