Set of keys from a map

Convert your map to sequence of tuples (key,value) first and then map it to a sequence of just keys:

map |> Map.toSeq |> Seq.map fst

FSI sample:

>Map.ofList[(1,"a");(2,"b")] |> Map.toSeq |> Seq.map fst;;
val it : seq<int> = seq [1; 2]

Or alternatively, as ordering of keys likely does not matter you may use more eager method returning the list of all keys. It is also not hard to make it into extension method keys of Microsoft.FSharp.Collections.Map module:

module Map =
    let keys (m: Map<'Key, 'T>) =
        Map.fold (fun keys key _ -> key::keys) [] m

In F# 6.0, Map collection has now a Keys property.


OLD ANSWER:

Most readable (and probably most efficient, due to not needing previous conversions to Seq or mapping) answer:

let Keys(map: Map<'K,'V>) =
    seq {
        for KeyValue(key,value) in map do
            yield key
    } |> Set.ofSeq

Tags:

Hashtable

F#