Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created August 15, 2012 21:34
Show Gist options
  • Save theburningmonk/3363893 to your computer and use it in GitHub Desktop.
Save theburningmonk/3363893 to your computer and use it in GitHub Desktop.
F# - converting a C# dictionary to a Map
let toMap dictionary =
(dictionary :> seq<_>)
|> Seq.map (|KeyValue|)
|> Map.ofSeq
@ImaginaryDevelopment
Copy link

you don't need the () around the top part.

@rexcfnghk
Copy link

Two suggestions:

  1. Mark the function as inline so that callers of toMap will not affect the F# type inference process and fix the generic arguments
  2. The upcast is not necessary as IDictionary<K, V> already implements IEnumerable<KeyValuePair<K, V>>
let inline toMap kvps =
    kvps
    |> Seq.map (|KeyValue|)
    |> Map.ofSeq

@petrkoutnycz
Copy link

I'm fairly new to F# and wondering - how to get to know with built-in active patterns like this?

@leandromoh
Copy link

I'm fairly new to F# and wondering - how to get to know with built-in active patterns like this?

Same

@gwaxG
Copy link

gwaxG commented Feb 27, 2023

I'm fairly new to F# and wondering - how to get to know with built-in active patterns like this?

This site can be helpful.
https://fsharp.github.io/fsharp-core-docs/

For example, here you can check all Map operations.
https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-mapmodule.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment