Skip to content

Commit 221911c

Browse files
committed
Simplify code
1 parent 9431c75 commit 221911c

File tree

3 files changed

+43
-56
lines changed

3 files changed

+43
-56
lines changed

src/FSharpPlus/Control/Monad.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ type Return =
165165

166166
static member Return (_: seq<'a> , _: Default4) = fun x -> Seq.singleton x : seq<'a>
167167
static member Return (_: IEnumerator<'a>, _: Default3) = fun x -> Enumerator.upto None (fun _ -> x) : IEnumerator<'a>
168-
static member Return (_: IDictionary<'k,'t> , _: Default2) = fun x -> Dict.initInfinite x : IDictionary<'k,'t>
168+
static member Return (_: IDictionary<'k,'t> , _: Default2) = fun x -> Dict.emptyWithDefault x : IDictionary<'k,'t>
169169
#if (!FABLE_COMPILER_3) // TODO Dummy overload for now
170170
static member Return (_: IReadOnlyDictionary<'k,'t>, _: Default3) = fun x -> readOnlyDict [Unchecked.defaultof<'k>, x] : IReadOnlyDictionary<'k,'t>
171171
#endif

src/FSharpPlus/Control/ZipApplicative.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Pure =
3131

3232
static member Pure (_: seq<'a> , _: Default4 ) = fun x -> Seq.initInfinite (fun _ -> x) : seq<'a>
3333
static member Pure (_: IEnumerator<'a> , _: Default3 ) = fun x -> Enumerator.upto None (fun _ -> x) : IEnumerator<'a>
34-
static member Pure (_: IDictionary<'k,'t>, _: Default2) = fun x -> Dict.initInfinite x: IDictionary<'k,'t>
34+
static member Pure (_: IDictionary<'k,'t>, _: Default2) = fun x -> Dict.emptyWithDefault x: IDictionary<'k,'t>
3535
#if (!FABLE_COMPILER_3) // TODO Dummy overload for now
3636
static member Pure (_: IReadOnlyDictionary<'k,'t>, _: Default3) = fun x -> readOnlyDict [Unchecked.defaultof<'k>, x] : IReadOnlyDictionary<'k,'t>
3737
#endif

src/FSharpPlus/Extensions/Dict.fs

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ module Auto =
2929
| _ -> value <- konst
3030
true
3131
member _.Count = source.Count
32-
// if typeof<'TKey>.IsValueType then
33-
// if typeof<'TKey> = typeof<bool> then 2
34-
// else
35-
// let s = sizeof<'TKey>
36-
// if s < 4 then pown 2 (sizeof<'TKey> * 8)
37-
// else -1 // infinity
38-
// elif typeof<'TKey> = typeof<unit> then 1
39-
// else -1
4032
member _.ContainsKey (_key: 'TKey) = true
4133
member _.Contains (item: KeyValuePair<'TKey,'TValue>) =
4234
match source.TryGetValue item.Key with
@@ -67,10 +59,15 @@ module Dict =
6759
open System.Collections.ObjectModel
6860
open Auto
6961

70-
/// <summary>Creates a conceptually infinite dictionary containing the same value for all possible keys.</summary>
71-
/// <param name="source">The value for all possible keys.</param>
72-
let initInfinite<'TKey,'TValue when 'TKey : equality> (source: 'TValue) : IDictionary<'TKey,'TValue> = new DefaultableDict<'TKey,'TValue>(source, Dictionary<'TKey,'TValue> ())
73-
let initHybrid<'TKey,'TValue> (konst: 'TValue) (source: IDictionary<'TKey,'TValue>) : IDictionary<'TKey,'TValue> = new DefaultableDict<'TKey,'TValue>(konst, source)
62+
/// <summary>Creates a defaultable dictionary.</summary>
63+
/// <param name="konst">The value for all missing keys.</param>
64+
/// <param name="source">The source dictionary.</param>
65+
let emptyWithDefault<'TKey,'TValue when 'TKey : equality> (konst: 'TValue) : IDictionary<'TKey,'TValue> = new DefaultableDict<'TKey,'TValue>(konst, dict [])
66+
67+
/// <summary>Creates a defaultable dictionary.</summary>
68+
/// <param name="konst">The value for all missing keys.</param>
69+
/// <param name="source">The source dictionary.</param>
70+
let initWithDefault<'TKey,'TValue> (konst: 'TValue) (source: IDictionary<'TKey,'TValue>) : IDictionary<'TKey,'TValue> = new DefaultableDict<'TKey,'TValue>(konst, source)
7471

7572
#if !FABLE_COMPILER
7673
open System.Linq
@@ -128,18 +125,13 @@ module Dict =
128125
///
129126
/// <returns>The mapped dictionary.</returns>
130127
let map mapper (source: IDictionary<'Key, 'T>) =
131-
match source with
132-
| :? DefaultableDict<'Key, 'T> as s ->
133-
let dct = DefaultableDict<'Key, 'U> (mapper s.DefaultValue, Dictionary<'Key, 'U> ())
134-
let dct = dct :> IDictionary<'Key, 'U>
135-
for KeyValue(k, v) in source do
136-
dct.Add (k, mapper v)
137-
dct
138-
| _ ->
139-
let dct = Dictionary<'Key, 'U> ()
140-
for KeyValue(k, v) in source do
141-
dct.Add (k, mapper v)
142-
dct :> IDictionary<'Key, 'U>
128+
let dct =
129+
match source with
130+
| :? DefaultableDict<'Key, 'T> as s -> emptyWithDefault (mapper s.DefaultValue)
131+
| _ -> Dictionary<'Key, 'U> () :> IDictionary<'Key, 'U>
132+
for KeyValue(k, v) in source do
133+
dct.Add (k, mapper v)
134+
dct
143135

144136
/// <summary>Creates a Dictionary value from a pair of Dictionaries, using a function to combine them.</summary>
145137
/// <remarks>Keys that are not present on both dictionaries are dropped.</remarks>
@@ -149,38 +141,41 @@ module Dict =
149141
///
150142
/// <returns>The combined dictionary.</returns>
151143
let map2 mapper (source1: IDictionary<'Key, 'T1>) (source2: IDictionary<'Key, 'T2>) =
152-
let map k1 k2 =
144+
let map () =
153145
let dct = Dictionary<'Key, 'U> ()
154146
let f = OptimizedClosures.FSharpFunc<_, _, _>.Adapt mapper
155-
for k in set source1.Keys + set source2.Keys do
156-
match tryGetValue k source1, tryGetValue k source2, k1, k2 with
157-
| Some vx, Some vy, _ , _
158-
| None , Some vy, Some vx, _
159-
| Some vx, None , _ , Some vy -> dct.Add (k, f.Invoke (vx, vy))
160-
| _ , _ , _ , _ -> ()
147+
let keys = Seq.append source1.Keys source2.Keys |> Seq.distinct
148+
for k in keys do
149+
match tryGetValue k source1, tryGetValue k source2 with
150+
| Some vx, Some vy -> dct.Add (k, f.Invoke (vx, vy))
151+
| _ , _ -> ()
161152
dct :> IDictionary<'Key, 'U>
162153
match source1, source2 with
163-
| (:? DefaultableDict<'Key,'T1> as s1), (:? DefaultableDict<'Key,'T2> as s2) -> initHybrid (mapper s1.DefaultValue s2.DefaultValue) (map (Some s1.DefaultValue) (Some s2.DefaultValue))
164-
| (:? DefaultableDict<'Key,'T1> as s1), _ -> map (Some s1.DefaultValue) None
165-
| _, (:? DefaultableDict<'Key,'T2> as s2) -> map None (Some s2.DefaultValue)
166-
| _, _ -> map None None
154+
| (:? DefaultableDict<'Key,'T1> as s1), (:? DefaultableDict<'Key,'T2> as s2) -> initWithDefault (mapper s1.DefaultValue s2.DefaultValue) (map ())
155+
| _, _ -> map ()
167156

168157
/// <summary>Combines values from three dictionaries using mapping function.</summary>
169158
/// <remarks>Keys that are not present on every dictionary are dropped.</remarks>
170-
/// <param name="mapping">The mapping function.</param>
159+
/// <param name="mapper">The mapping function.</param>
171160
/// <param name="source1">First input dictionary.</param>
172161
/// <param name="source2">Second input dictionary.</param>
173162
/// <param name="source3">Third input dictionary.</param>
174163
///
175164
/// <returns>The mapped dictionary.</returns>
176-
let map3 mapping (source1: IDictionary<'Key, 'T1>) (source2: IDictionary<'Key, 'T2>) (source3: IDictionary<'Key, 'T3>) =
177-
let dct = Dictionary<'Key, 'U> ()
178-
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt mapping
179-
for KeyValue(k, vx) in source1 do
180-
match tryGetValue k source2, tryGetValue k source3 with
181-
| Some vy, Some vz -> dct.Add (k, f.Invoke (vx, vy, vz))
182-
| _ , _ -> ()
183-
dct :> IDictionary<'Key, 'U>
165+
let map3 mapper (source1: IDictionary<'Key, 'T1>) (source2: IDictionary<'Key, 'T2>) (source3: IDictionary<'Key, 'T3>) =
166+
let map () =
167+
let dct = Dictionary<'Key, 'U> ()
168+
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt mapper
169+
let keys = source1.Keys |> Seq.append source2.Keys |> Seq.append source3.Keys |> Seq.distinct
170+
for k in keys do
171+
match tryGetValue k source1, tryGetValue k source2, tryGetValue k source3 with
172+
| Some vx, Some vy, Some vz -> dct.Add (k, f.Invoke (vx, vy, vz))
173+
| _ , _ , _ -> ()
174+
dct :> IDictionary<'Key, 'U>
175+
match source1, source2, source3 with
176+
| (:? DefaultableDict<'Key,'T1> as s1), (:? DefaultableDict<'Key,'T2> as s2), (:? DefaultableDict<'Key,'T3> as s3) ->
177+
initWithDefault (mapper s1.DefaultValue s2.DefaultValue s3.DefaultValue) (map ())
178+
| _, _, _ -> map ()
184179

185180
/// <summary>Applies given function to each value of the given dictionary.</summary>
186181
/// <param name="chooser">The mapping function.</param>
@@ -223,17 +218,9 @@ module Dict =
223218
for KeyValue(k, v ) in source1 do d.[k] <- v
224219
for KeyValue(k, v') in source2 do d.[k] <- match d.TryGetValue k with true, v -> f.Invoke (v, v') | _ -> v'
225220
d :> IDictionary<'Key,'Value>
226-
// let combineWithKonst source konst =
227-
// let d = Dictionary<'Key,'Value> ()
228-
// for KeyValue(k, v) in source do d.[k] <- combiner v konst
229-
// d :> IDictionary<'Key,'Value>
230-
// let combineKonstWith konst source =
231-
// let d = Dictionary<'Key,'Value> ()
232-
// for KeyValue(k, v) in source do d.[k] <- combiner konst v
233-
// d :> IDictionary<'Key,'Value>
234221
match source1, source2 with
235-
| (:? DefaultableDict<'Key,'Value> as s1), (:? DefaultableDict<'Key,'Value> as s2) -> initHybrid (combiner s1.DefaultValue s2.DefaultValue) (combine())
236-
| (:? DefaultableDict<'Key,'Value> as s), _ | _, (:? DefaultableDict<'Key,'Value> as s) -> initHybrid s.DefaultValue (combine())
222+
| (:? DefaultableDict<'Key,'Value> as s1) , (:? DefaultableDict<'Key,'Value> as s2) -> initWithDefault (combiner s1.DefaultValue s2.DefaultValue) (combine())
223+
| (:? DefaultableDict<'Key,'Value> as s), _ | _, (:? DefaultableDict<'Key,'Value> as s) -> initWithDefault s.DefaultValue (combine())
237224
| s, empty | empty, s when empty.Count = 0 -> s
238225
| _, _ -> combine()
239226

0 commit comments

Comments
 (0)