Skip to content

Commit eef4e98

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

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-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

+17
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,23 @@ 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 di = new Dictionary<int, int>()
493+
di.Add (1, 2)
494+
di.Add (3, 4)
495+
let id = dict [1, 2; 3, 4]
496+
let ir = readOnlyDict [1, 2; 3, 4]
497+
let ma = Map.ofList [1, 2; 3, 4]
498+
499+
let r = ResizeArray<string> []
500+
501+
iter (fun x -> r.Add (string x)) di
502+
iter (fun x -> r.Add (string x)) id
503+
iter (fun x -> r.Add (string x)) ir
504+
iter (fun x -> r.Add (string x)) map
505+
CollectionAssert.AreEqual (ResizeArray ["2"; "4"; "2"; "4"; "2"; "4"; "2"; "4"], r)
506+
490507

491508

492509
module Foldable =

0 commit comments

Comments
 (0)