This F# library provides:
-
A dedicated
Dict<'T>
type. It is a thin wrapper aroundDictionary<'T>
with more functionality and nicer Error messages. -
A
DefaultDict<'T>
type. It works like Python's' defaultdict.
By providing a default function in the constructor it will always return a value for any key. -
Extension methods for working with the
IDictionary<'T>
interface.
It also works in JS and TS with Fable.
This library was designed for use with F# scripting.
Functions and methods never return null.
Only functions starting with try...
will return an F# Option.
Otherwise when a function fails on invalid input it will throw a descriptive exception.
I was always annoyed that a KeyNotFoundExceptions does not include the actual bad key nor a pretty printed dictionary.
This library fixes that in iDictionary.Get
, iDictionary.Set
and other item access functions.
#r "nuget: Dicts"
open Dicts
let dd = DefaultDict<string,int>(fun _ -> ref 99)
incr dd.["A"] // since dd.["A"] does not exist it will be created with the default value 99, and then incremented to 100
incr dd.["A"] // now it exists and will be incremented to 101
dd.["A"].Value = 101 // true
All Tests run in both javascript and dotnet. Successful Fable compilation to typescript is verified too. Go to the tests folder:
cd Tests
For testing with .NET using Expecto:
dotnet run
for JS testing with Fable.Mocha and TS verification:
npm test
see CHANGELOG.md