Skip to content

Commit 340b7ca

Browse files
gustywallymathieu
authored andcommitted
+ Missing dictionary utility functions (#639)
1 parent 85ad318 commit 340b7ca

9 files changed

Lines changed: 488 additions & 121 deletions

File tree

src/FSharpPlus/Control/Applicative.fs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -91,45 +91,20 @@ type Apply =
9191
let f, x = f.Value, x.Value
9292
KeyValuePair2 (Plus.Invoke a b, f x)
9393

94-
static member ``<*>`` (struct (f: Map<'Key,_> , x: Map<'Key,'T> ) , _output: Map<'Key,'U> , [<Optional>]_mthd: Apply) : Map<'Key,'U> = Map (seq {
95-
for KeyValue(k, vf) in f do
96-
match Map.tryFind k x with
97-
| Some vx -> yield k, vf vx
98-
| _ -> () })
99-
100-
static member ``<*>`` (struct (f: Dictionary<'Key,_>, x: Dictionary<'Key,'T>) , _output: Dictionary<'Key,'U> , [<Optional>]_mthd: Apply) : Dictionary<'Key,'U> =
101-
let dct = Dictionary ()
102-
for KeyValue(k, vf) in f do
103-
match x.TryGetValue k with
104-
| true, vx -> dct.Add (k, vf vx)
105-
| _ -> ()
106-
dct
107-
108-
static member ``<*>`` (struct (f: IDictionary<'Key,_>, x: IDictionary<'Key,'T>) , _output: IDictionary<'Key,'U> , [<Optional>]_mthd: Apply) : IDictionary<'Key,'U> =
109-
let dct = Dictionary ()
110-
for KeyValue(k, vf) in f do
111-
match x.TryGetValue k with
112-
| true, vx -> dct.Add (k, vf vx)
113-
| _ -> ()
114-
dct :> IDictionary<'Key,'U>
115-
116-
static member ``<*>`` (struct (f: IReadOnlyDictionary<'Key,_>, x: IReadOnlyDictionary<'Key,'T>) , _output: IReadOnlyDictionary<'Key,'U> , [<Optional>]_mthd: Apply) : IReadOnlyDictionary<'Key,'U> =
117-
let dct = Dictionary ()
118-
for KeyValue(k, vf) in f do
119-
match x.TryGetValue k with
120-
| true, vx -> dct.Add (k, vf vx)
121-
| _ -> ()
122-
dct :> IReadOnlyDictionary<'Key,'U>
94+
static member ``<*>`` (struct (f: Map<'Key,_> , x: Map<'Key,'T> ) , _output: Map<'Key,'U> , [<Optional>]_mthd: Apply) : Map<'Key,'U> = Map.apply f x
95+
static member ``<*>`` (struct (f: Dictionary<'Key,_> , x: Dictionary<'Key,'T> ) , _output: Dictionary<'Key,'U> , [<Optional>]_mthd: Apply) : Dictionary<'Key,'U> = Dictionary.apply f x
96+
static member ``<*>`` (struct (f: IDictionary<'Key,_> , x: IDictionary<'Key,'T> ) , _output: IDictionary<'Key,'U> , [<Optional>]_mthd: Apply) : IDictionary<'Key,'U> = Dict.apply f x
97+
static member ``<*>`` (struct (f: IReadOnlyDictionary<'Key,_>, x: IReadOnlyDictionary<'Key,'T>) , _output: IReadOnlyDictionary<'Key,'U>, [<Optional>]_mthd: Apply) : IReadOnlyDictionary<'Key,'U> = IReadOnlyDictionary.apply f x
12398

12499
#if !FABLE_COMPILER
125100
// Compatibility member:
126101
static member ``<*>`` (f: Expr<'T->'U>, x: Expr<'T>, [<Optional>]_output: Expr<'U>, [<Optional>]_mthd: Apply) = Expr.Cast<'U> (Expr.Application (f, x))
127-
static member ``<*>`` (struct (f: Expr<'T->'U>, x: Expr<'T>), _output: Expr<'U>, [<Optional>]_mthd: Apply) = Expr.Cast<'U> (Expr.Application (f, x))
102+
static member ``<*>`` (struct (f: Expr<'T->'U>, x: Expr<'T>), _output: Expr<'U>, [<Optional>]_mthd: Apply) = Expr.Cast<'U> (Expr.Application (f, x))
128103
#endif
129104
// Compatibility member:
130105
static member ``<*>`` (f: ('T->'U) ResizeArray, x: 'T ResizeArray, [<Optional>]_output: 'U ResizeArray, [<Optional>]_mthd: Apply) = ResizeArray.apply f x : 'U ResizeArray
131106

132-
static member ``<*>`` (struct (f: ('T->'U) ResizeArray, x: 'T ResizeArray), _output: 'U ResizeArray, [<Optional>]_mthd: Apply) = ResizeArray.apply f x : 'U ResizeArray
107+
static member ``<*>`` (struct (f: ('T->'U) ResizeArray, x: 'T ResizeArray), _output: 'U ResizeArray, [<Optional>]_mthd: Apply) = ResizeArray.apply f x : 'U ResizeArray
133108

134109
static member inline Invoke (f: '``Applicative<'T -> 'U>``) (x: '``Applicative<'T>``) : '``Applicative<'U>`` =
135110
let inline call (mthd : ^M, input1: ^I1, input2: ^I2, output: ^R) =

src/FSharpPlus/Control/Foldable.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ type FoldMap =
148148
static member inline FoldMap (x: Set<_> , f, [<Optional>]_impl: FoldMap) = Seq.fold (fun x y -> Plus.Invoke x (f y)) (Zero.Invoke ()) x
149149
static member inline FoldMap (x: _ [] , f, [<Optional>]_impl: FoldMap) = Array.fold (fun x y -> Plus.Invoke x (f y)) (Zero.Invoke ()) x
150150

151+
static member inline FoldMap (x: Map<_, _> , f, [<Optional>]_impl: FoldMap) = Map.fold (fun x _ y -> Plus.Invoke x (f y)) (Zero.Invoke ()) x
152+
static member inline FoldMap (x: IDictionary<_, _> , f, [<Optional>]_impl: FoldMap) = Dict.fold (fun x _ y -> Plus.Invoke x (f y)) (Zero.Invoke ()) x
153+
static member inline FoldMap (x: IReadOnlyDictionary<_, _>, f, [<Optional>]_impl: FoldMap) = IReadOnlyDictionary.fold (fun x _ y -> Plus.Invoke x (f y)) (Zero.Invoke ()) x
154+
151155
static member inline Invoke (f: 'T->'Monoid) (x: '``Foldable'<T>``) : 'Monoid =
152156
let inline call_2 (a: ^a, b: ^b, f) = ((^a or ^b) : (static member FoldMap : _*_*_ -> _) b, f, a)
153157
let inline call (a: 'a, b: 'b, f) = call_2 (a, b, f)
@@ -186,6 +190,9 @@ type Fold =
186190
static member Fold (x: list<_> , f, z , [<Optional>]_impl: Fold ) = List.fold f z x
187191
static member Fold (x: Set<_> , f, z , [<Optional>]_impl: Fold ) = Set.fold f z x
188192
static member Fold (x: _ [] , f, z , [<Optional>]_impl: Fold ) = Array.fold f z x
193+
static member Fold (x: Map<_,_> , f, z , [<Optional>]_impl: Fold ) = Map.fold (fun s _ -> f s) z x
194+
static member Fold (x: IDictionary<_,_> , f, z , [<Optional>]_impl: Fold ) = Dict.fold (fun s _ -> f s) z x
195+
static member Fold (x: IReadOnlyDictionary<_,_>, f, z , [<Optional>]_impl: Fold ) = IReadOnlyDictionary.fold (fun s _ -> f s) z x
189196

190197
static member inline Invoke (folder: 'State->'T->'State) (state: 'State) (foldable: '``Foldable'<T>``) : 'State =
191198
let inline call_2 (a: ^a, b: ^b, f, z) = ((^a or ^b) : (static member Fold : _*_*_*_ -> _) b, f, z, a)

src/FSharpPlus/Control/Functor.fs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ type Unzip =
161161
static member inline Unzip ((source: '``Functor<'T * 'U>`` , _output: '``Functor<'T>`` * '``Functor<'U>`` ) , _mthd: Default1) = (^``Functor<'T * 'U>``: (static member Unzip : _->_) source) : '``Functor<'T>`` * '``Functor<'U>``
162162
static member inline Unzip (( _ : ^t when ^t: null and ^t: struct , _ ) , _ ) = ()
163163

164-
static member Unzip ((source: Lazy<'T * 'U> , _output: Lazy<'T> * Lazy<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
164+
static member Unzip ((source: Lazy<'T * 'U> , _output: Lazy<'T> * Lazy<'U> ) , _mthd: Unzip ) = Lazy.map fst source, Lazy.map snd source
165165

166166
#if !FABLE_COMPILER
167-
static member Unzip ((source: Task<'T * 'U> , _output: Task<'T> * Task<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
167+
static member Unzip ((source: Task<'T * 'U> , _output: Task<'T> * Task<'U> ) , _mthd: Unzip ) = Task.map fst source, Task.map snd source
168168
#endif
169169
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
170-
static member Unzip ((source: ValueTask<'T * 'U> , _output: ValueTask<'T> * ValueTask<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
170+
static member Unzip ((source: ValueTask<'T * 'U> , _output: ValueTask<'T> * ValueTask<'U> ) , _mthd: Unzip ) = ValueTask.map fst source, ValueTask.map snd source
171171
#endif
172172
static member Unzip ((source: option<'T * 'U> , _output: option<'T> * option<'U> ) , _mthd: Unzip ) = Option.unzip source
173173
static member Unzip ((source: voption<'T * 'U> , _output: voption<'T> * voption<'U> ) , _mthd: Unzip ) = ValueOption.unzip source
@@ -179,17 +179,17 @@ type Unzip =
179179
static member Unzip ((struct (m: 'Monoid, t: ('T * 'U)) , _output: struct ('Monoid * 'T) * struct ('Monoid * 'U) ) , _mthd: Unzip ) = struct (m, fst t), struct (m, snd t)
180180
static member Unzip ((source: ('T * 'U) [] , _output: 'T [] * 'U [] ) , _mthd: Unzip ) = Array.unzip source
181181
#if !FABLE_COMPILER
182-
static member Unzip ((source: ('T * 'U) [,] , _output: 'T [,] * 'U [,] ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
183-
static member Unzip ((source: ('T * 'U) [,,] , _output: 'T [,,] * 'U [,,] ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
184-
static member Unzip ((source: ('T * 'U) [,,,] , _output: 'T [,,,] * 'U [,,,] ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
182+
static member Unzip ((source: ('T * 'U) [,] , _output: 'T [,] * 'U [,] ) , _mthd: Unzip ) = Array2D.map fst source, Array2D.map snd source
183+
static member Unzip ((source: ('T * 'U) [,,] , _output: 'T [,,] * 'U [,,] ) , _mthd: Unzip ) = Array3D.map fst source, Array3D.map snd source
184+
static member Unzip ((source: ('T * 'U) [,,,] , _output: 'T [,,,] * 'U [,,,] ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
185185
#endif
186186

187-
static member Unzip ((source: Async<'T * 'U> , _output: Async<'T> * Async<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
187+
static member Unzip ((source: Async<'T * 'U> , _output: Async<'T> * Async<'U> ) , _mthd: Unzip ) = Async.map fst source, Async.map snd source
188188
static member Unzip ((source: Result<'T * 'U, 'E> , _output: Result<'T,'E> * Result<'U,'E> ) , _mthd: Unzip ) = Result.unzip source
189-
static member Unzip ((source: Choice<'T * 'U, 'E> , _output: Choice<'T,'E> * Choice<'U,'E> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
189+
static member Unzip ((source: Choice<'T * 'U, 'E> , _output: Choice<'T,'E> * Choice<'U,'E> ) , _mthd: Unzip ) = Choice.map fst source, Choice.map snd source
190190
static member Unzip ((source: KeyValuePair<'Key, 'T * 'U> , _output: KeyValuePair<_, 'T> * KeyValuePair<_, 'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
191-
static member Unzip ((source: Map<'Key, 'T * 'U> , _output: Map<_, 'T> * Map<_, 'U> ) , _mthd: Unzip ) = Map.unzip source
192-
static member Unzip ((source: Dictionary<'Key, 'T * 'U> , _output: Dictionary<_, 'T> * Dictionary<_, 'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
191+
static member Unzip ((source: Map<'Key, 'T * 'U> , _output: Map<_, 'T> * Map<_, 'U> ) , _mthd: Unzip ) = Map.unzip source
192+
static member Unzip ((source: Dictionary<'Key, 'T * 'U> , _output: Dictionary<_, 'T> * Dictionary<_, 'U> ) , _mthd: Unzip ) = Dictionary.unzip source
193193

194194
#if !FABLE_COMPILER
195195
static member Unzip ((source: Expr<'T * 'U> , _output: Expr<'T> * Expr<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
@@ -200,10 +200,10 @@ type Unzip =
200200
static member Unzip ((source: seq<'T * 'U> , _output: seq<'T> * seq<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
201201
static member Unzip ((source: NonEmptySeq<'T * 'U> , _output: NonEmptySeq<'T> * NonEmptySeq<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
202202

203-
static member Unzip ((source: IEnumerator<'T * 'U> , _output: IEnumerator<'T> * ResizeArray<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
203+
static member Unzip ((source: IEnumerator<'T * 'U> , _output: IEnumerator<'T> * ResizeArray<'U> ) , _mthd: Unzip ) = Enumerator.map fst source, Enumerator.map snd source
204204
static member Unzip ((source: IDictionary<'Key, 'T * 'U> , _output: IDictionary<_,'T> * IDictionary<_,'U> ) , _mthd: Unzip ) = Dict.unzip source
205205
static member Unzip ((source: IReadOnlyDictionary<'Key,'T * 'U> , _output: IReadOnlyDictionary<_,'T> * IReadOnlyDictionary<_,'U>) , _mthd: Unzip ) = IReadOnlyDictionary.unzip source
206-
static member Unzip ((source: IObservable<'T * 'U> , _output: IObservable<'T> * ResizeArray<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source
206+
static member Unzip ((source: IObservable<'T * 'U> , _output: IObservable<'T> * ResizeArray<'U> ) , _mthd: Unzip ) = Observable.map fst source, Observable.map snd source
207207

208208
[<Obsolete>]static member Unzip (source: list<'T * 'U> , [<Optional>]_output: list<'T> * list<'U> , [<Optional>]_mthd:Unzip ) = List.unzip source
209209
[<Obsolete>]static member Unzip (source: ('T * 'U) [] , [<Optional>]_output: 'T [] * 'U [] , [<Optional>]_mthd:Unzip ) = Array.unzip source
@@ -224,7 +224,7 @@ type Zip =
224224
static member Zip ((x: NonEmptySeq<'T> , y: NonEmptySeq<'U> , _output: NonEmptySeq<'T*'U> ), _mthd: Zip) = NonEmptySeq.zip x y
225225
static member Zip ((x: IDictionary<'K, 'T> , y: IDictionary<'K,'U> , _output: IDictionary<'K,'T*'U> ), _mthd: Zip) = Dict.zip x y
226226
static member Zip ((x: IReadOnlyDictionary<'K, 'T>, y: IReadOnlyDictionary<'K,'U>, _output: IReadOnlyDictionary<'K,'T*'U>), _mthd: Zip) = IReadOnlyDictionary.zip x y
227-
static member Zip ((x: Dictionary<'K, 'T> , y: Dictionary<'K,'U> , _output: Dictionary<'K,'T*'U> ), _mthd: Zip) = Dict.zip x y :?> Dictionary<'K,'T*'U>
227+
static member Zip ((x: Dictionary<'K, 'T> , y: Dictionary<'K,'U> , _output: Dictionary<'K,'T*'U> ), _mthd: Zip) = Dictionary.zip x y
228228
static member Zip ((x: Map<'K, 'T> , y: Map<'K,'U> , _output: Map<'K,'T*'U> ), _mthd: Zip) = Map.zip x y
229229
static member Zip ((f: 'R -> 'T , g: 'R -> 'U , _output: 'R -> 'T * 'U ), _mthd: Zip) = fun x -> (f x, g x)
230230
static member Zip ((f: Func<'R, 'T> , g: Func<'R, 'U> , _output: Func<'R, 'T * 'U> ), _mthd: Zip) = Func<_,_> (fun x -> (f.Invoke x, g.Invoke x))

0 commit comments

Comments
 (0)