How does one get the first key,value pair from F# Map without knowing the key?
<p>How does one get the first <code>key,value pair</code> from F# <a href="https://msdn.microsoft.com/en-us/library/ee353880.aspx" rel="nofollow noreferrer">Map</a> without knowing the key? </p>

<p>I know that the Map type is used to get a corresponding value given a key, e.g. <a href="https://msdn.microsoft.com/en-us/library/ee340502.aspx" rel="nofollow noreferrer">find</a>.</p>

<p>I also know that one can convert the map to a list and use List.Head, e.g. </p>

<pre><code>List.head (Map.toList map)
</code></pre>

<p>I would like to do this<br>
1. without a key<br>
2. without knowing the types of the key and value<br>
3. without using a mutable<br>
4. without iterating through the entire map<br>
5. without doing a conversion that iterates through the entire map behind the seen, e.g. Map.toList, etc.  </p>

<p>I am also aware that if one gets the first <code>key,value pair</code> it might not be of use because the map documentation does not note if using map in two different calls guarantees the same order.</p>

<p>If the code can not be written then an existing reference from a site such as MSDN explaining and showing why not would be accepted.</p>

<p>TLDR;</p>

<p>How I arrived at this problem was converting this function:</p>

<pre><code>let findmin l = 
    List.foldBack
        (fun (_,pr1 as p1) (_,pr2 as p2) -&gt; if pr1 &lt;= pr2 then p1 else p2)
        (List.tail l) (List.head l)
</code></pre>

<p>which is based on list and is used to find the minimum value in the associative list of <code>string * int</code>.</p>

<p>An example list:</p>

<pre><code>["+",10; "-",10; "*",20; "/",20]
</code></pre>

<p>The list is used for parsing binary operator expressions that have precedence where the string is the binary operator and the int is the precedence. Other functions are preformed on the data such that using F# map might be an advantage over list. I have not decided on a final solution but wanted to explore this problem with map while it was still in the forefront.</p>

<p>Currently I am using:</p>

<pre><code>let findmin m =
    if Map.isEmpty m then 
        None
    else
        let result = 
            Map.foldBack 
                (fun key value (k,v) -&gt; 
                    if value &lt;= v then (key,value) 
                    else (k,v))
                m ("",1000)
        Some(result)
</code></pre>

<p>but here I had to hard code in the initial state <code>("",1000)</code> when what would be better is just using the first value in the map as the initial state and then passing the remainder of the map as the starting map as was done with the list:</p>

<pre><code>(List.tail l) (List.head l)
</code></pre>

<p>Yes this is partitioning the map but that did not work e.g.,</p>

<pre><code>let infixes = ["+",10; "-",10; "*",20; "/",20]
let infixMap = infixes |&gt; Map.ofList
let mutable test = true
let fx k v : bool =
    if test then 
        printfn "first"
        test &lt;- false
        true
    else 
        printfn "rest"
        false
let (first,rest) = Map.partition fx infixMap
</code></pre>

<p>which results in</p>

<pre><code>val rest : Map&lt;string,int&gt; = map [("*", 20); ("+", 10); ("-", 10)]  
val first : Map&lt;string,int&gt; = map [("/", 20)]
</code></pre>

<p>which are two maps and not a key,value pair for first</p>

<pre><code>("/",20)
</code></pre>

<h2>Notes about answers</h2>

<p>For practical purposes with regards to the precedence parsing seeing the <code>+</code> operations before <code>-</code> in the final transformation is preferable so returning <code>+</code> before <code>-</code> is desirable. Thus this variation of the answer by <a href="https://stackoverflow.com/a/36719524/1243762">marklam</a> </p>

<pre><code>let findmin (map : Map&lt;_,_&gt;) = map |&gt; Seq.minBy (fun kvp -&gt; kvp.Value)
</code></pre>

<p>achieves this and does this variation by <a href="https://stackoverflow.com/a/36719634/1243762">Tomas</a></p>

<pre><code>let findmin m = 
    Map.foldBack (fun k2 v2 st -&gt;
        match st with
        | Some(k1, v1) when v1 &lt; v2 -&gt; st
        | _ -&gt; Some(k2, v2)) m None
</code></pre>

<p>The use of <code>Seq.head</code> does return the first item in the <code>map</code> but one must be aware that the <code>map</code> is constructed with the keys sorted so while for my practical example I would like to start with the lowest value being <code>10</code> and since the items are sorted by <code>key</code> the first one returned is <code>("*",20)</code> with <code>*</code> being the first key because the keys are strings and sorted by such.</p>

<p>For me to practically use the answer by <a href="https://stackoverflow.com/a/36719524/1243762">marklam</a> I had to check for an empty list before calling and massage the output from a <a href="https://msdn.microsoft.com/en-us/library/5tbh8a42(v=vs.110).aspx" rel="nofollow noreferrer">KeyValuePair</a> into a tuple using <code>let (a,b) = kvp.Key,kvp.Value</code></p>