Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary functions (from our other libraries) #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/FSharpAux/Dictionary.fs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,52 @@ module Dictionary =
| true -> Some table.[key]
| false -> None

/// <summary>
/// Returns the value of the given key if present in the given Dictionary. Else returns None.
/// </summary>
let tryGetValue k (dict : Dictionary<'K,'V>) =
let b,v = dict.TryGetValue(k)
// Only get value if
if b then Some v
else
None

/// <summary>
/// Returns the value to the key k if it is bound, else fails.
/// </summary>
let getValue k (dict : Dictionary<'K,'V>) =
try
dict.Item k
with
| _ -> failwithf "Key %O is not present in Dictionary %O." k dict

/// <summary>
/// Returns the trimmed string of the given key if present in the given Dictionary. Else returns None.
/// </summary>
let tryGetString k (dict : Dictionary<'K,string>) =
let b,v = dict.TryGetValue(k)
// Only get value if
if b && v.Trim() <> ""
then
v.Trim() |> Some
else
None

/// <summary>
/// Returns the number of key/value pairs contained in the given Dictionary.
/// </summary>
let length (dict : Dictionary<'K,'V>) =
dict.Count

/// <summary>
/// Applies an inner copy function to all values in a given Dictionary and returns a copy of the Dictionary.
/// </summary>
let copyRecursive (innerCopyF : 'V -> 'V) (dict : Dictionary<'K,'V>) =
let newDict = Dictionary<'K,'V>()
for kv in dict do
newDict.Add(kv.Key,innerCopyF kv.Value)
newDict


// /// <summary>Folds over the bindings in the dictionary </summary>
// /// <param name="folder">The function to update the state given the input key/value pairs.</param>
Expand Down
63 changes: 63 additions & 0 deletions tests/FSharpAux.Tests/DictionaryTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module DictionaryTests

open Expecto
open FSharpAux
open System.Collections.Generic


let testDic1 = new Dictionary<int,string>()
testDic1.Add(1, "Hello")
testDic1.Add(2, "World")
testDic1.Add(3, " World ")
let testDicRec = new Dictionary<int,Dictionary<int,string>>()
let testDicRecInner = new Dictionary<int,string>()
testDicRecInner.Add(1, "inner string")
testDicRec.Add(1, testDicRecInner)


[<Tests>]
let dictTests =
testList "DictionaryTests" [
testList "Dictionary.tryGetValue" [
testCase "Is Some" <| fun _ ->
Expect.isSome (Dictionary.tryGetValue 2 testDic1) "Is None"
testCase "Is None when expected" <| fun _ ->
Expect.isNone (Dictionary.tryGetValue 4 testDic1) "Is Some"
testCase "Returns correct value" <| fun _ ->
let res = Dictionary.tryGetValue 2 testDic1
Expect.equal res.Value "World" "Did not return correct value"
]
testList "Dictionary.tryGetString" [
testCase "Is Some" <| fun _ ->
Expect.isSome (Dictionary.tryGetString 2 testDic1) "Is None"
testCase "Is None when expected" <| fun _ ->
Expect.isNone (Dictionary.tryGetString 4 testDic1) "Is Some"
testCase "Returns correct string" <| fun _ ->
let res = Dictionary.tryGetString 2 testDic1
Expect.equal res.Value "World" "Did not return correct string"
testCase "Returns correct string, trimmed" <| fun _ ->
let res = Dictionary.tryGetString 3 testDic1
Expect.equal res.Value "World" "Did not return correctly trimmed string"
]
testList "Dictionary.getValue" [
testCase "Throws when expected" <| fun _ ->
Expect.throws (fun _ -> Dictionary.getValue 4 testDic1 |> ignore) "Did not throw though item does not exist"
testCase "Returns correct value" <| fun _ ->
let res = Dictionary.getValue 2 testDic1
Expect.equal res "World" "Did not return correct value"
]
testList "Dictionary.length" [
testCase "Returns correct count" <| fun _ ->
let res = Dictionary.length testDic1
Expect.equal res 3 "Returns incorrect count"
]
testList "Dictionary.copyRecursive" [
let testDicRecCopy = Dictionary.copyRecursive (Dictionary.copyRecursive id) testDicRec
testCase "Returns deep copy, check outer count" <| fun _ ->
Expect.equal testDicRecCopy.Count 1 "Copied Dictionary does not have correct outer count"
testCase "Returns deep copy, check inner count" <| fun _ ->
Expect.equal (testDicRecCopy.Item 1).Count 1 "Copied Dictionary does not have correct inner count"
testCase "Returns deep copy, check inner value" <| fun _ ->
Expect.equal ((testDicRecCopy.Item 1).Item 1) "inner string" "Copied Dictionary does not have correct inner count"
]
]
1 change: 1 addition & 0 deletions tests/FSharpAux.Tests/FSharpAux.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="DictionaryTests.fs" />
<Compile Include="SeqTests.fs" />
<Compile Include="ArrayTests.fs" />
<Compile Include="Array2DTests.fs" />
Expand Down