Skip to content

Commit f2e49ba

Browse files
committed
Fix issue with iteration of dictionaries
1 parent 2aeab30 commit f2e49ba

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/FSharpPlus/Control/Functor.fs

+2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ type Iterate =
4545
static member Iterate (x: Choice<'T, 'E> , action) = match x with Choice1Of2 x -> action x | _ -> ()
4646
static member Iterate (KeyValue(_: 'Key, x: 'T), action) = action x : unit
4747
static member Iterate (x: Map<'Key,'T> , action) = Map.iter (const' action) x
48+
static member Iterate (x: IDictionary<'Key, 'T>, action) = Dict.iterValues action x
4849
static member Iterate (x: Dictionary<'Key, 'T> , action) = Dictionary.iterValues action x
50+
static member Iterate (x: IReadOnlyDictionary<'Key, 'T>, action) = IReadOnlyDictionary.iterValues action x
4951
static member Iterate (x: _ ResizeArray , action) = ResizeArray.iter action x
5052

5153
// Restricted

tests/FSharpPlus.Tests/General.fs

+16
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,22 @@ module Functor =
487487
let nel = zip (NonEmptyList.ofList [1; 2]) (NonEmptyList.ofList ["a"; "b"; "c"])
488488
CollectionAssert.AreEqual (NonEmptyList.ofList [1,"a"; 2,"b"], nel)
489489

490+
[<Test>]
491+
let iterTests () =
492+
let li = [1, 2; 3, 4]
493+
let di: Dictionary<int, int> = ofList li
494+
let id = dict li
495+
let ir = readOnlyDict li
496+
let ma = Map.ofList li
497+
498+
let r = ResizeArray<string> []
499+
500+
iter (r.Add << string) di
501+
iter (r.Add << string) id
502+
iter (r.Add << string) ir
503+
iter (r.Add << string) ma
504+
CollectionAssert.AreEqual (ResizeArray ["2"; "4"; "2"; "4"; "2"; "4"; "2"; "4"], r)
505+
490506

491507

492508
module Foldable =

0 commit comments

Comments
 (0)