Skip to content

Commit 2769fd0

Browse files
committed
Simplify code
1 parent 9431c75 commit 2769fd0

File tree

1 file changed

+36
-54
lines changed

1 file changed

+36
-54
lines changed

src/FSharpPlus/Extensions/Dict.fs

Lines changed: 36 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,10 @@ 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 initWithDefault<'TKey,'TValue> (konst: 'TValue) (source: IDictionary<'TKey,'TValue>) : IDictionary<'TKey,'TValue> = new DefaultableDict<'TKey,'TValue>(konst, source)
7466

7567
#if !FABLE_COMPILER
7668
open System.Linq
@@ -128,18 +120,13 @@ module Dict =
128120
///
129121
/// <returns>The mapped dictionary.</returns>
130122
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>
123+
let dct =
124+
match source with
125+
| :? DefaultableDict<'Key, 'T> as s -> initWithDefault (mapper s.DefaultValue) (Dictionary<'Key, 'U> ())
126+
| _ -> Dictionary<'Key, 'U> () :> IDictionary<'Key, 'U>
127+
for KeyValue(k, v) in source do
128+
dct.Add (k, mapper v)
129+
dct
143130

144131
/// <summary>Creates a Dictionary value from a pair of Dictionaries, using a function to combine them.</summary>
145132
/// <remarks>Keys that are not present on both dictionaries are dropped.</remarks>
@@ -149,38 +136,41 @@ module Dict =
149136
///
150137
/// <returns>The combined dictionary.</returns>
151138
let map2 mapper (source1: IDictionary<'Key, 'T1>) (source2: IDictionary<'Key, 'T2>) =
152-
let map k1 k2 =
139+
let map () =
153140
let dct = Dictionary<'Key, 'U> ()
154141
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-
| _ , _ , _ , _ -> ()
142+
let keys = Seq.append source1.Keys source2.Keys |> Seq.distinct
143+
for k in keys do
144+
match tryGetValue k source1, tryGetValue k source2 with
145+
| Some vx, Some vy -> dct.Add (k, f.Invoke (vx, vy))
146+
| _ , _ -> ()
161147
dct :> IDictionary<'Key, 'U>
162148
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
149+
| (:? DefaultableDict<'Key,'T1> as s1), (:? DefaultableDict<'Key,'T2> as s2) -> initWithDefault (mapper s1.DefaultValue s2.DefaultValue) (map ())
150+
| _, _ -> map ()
167151

168152
/// <summary>Combines values from three dictionaries using mapping function.</summary>
169153
/// <remarks>Keys that are not present on every dictionary are dropped.</remarks>
170-
/// <param name="mapping">The mapping function.</param>
154+
/// <param name="mapper">The mapping function.</param>
171155
/// <param name="source1">First input dictionary.</param>
172156
/// <param name="source2">Second input dictionary.</param>
173157
/// <param name="source3">Third input dictionary.</param>
174158
///
175159
/// <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>
160+
let map3 mapper (source1: IDictionary<'Key, 'T1>) (source2: IDictionary<'Key, 'T2>) (source3: IDictionary<'Key, 'T3>) =
161+
let map () =
162+
let dct = Dictionary<'Key, 'U> ()
163+
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt mapper
164+
let keys = source1.Keys |> Seq.append source2.Keys |> Seq.append source3.Keys |> Seq.distinct
165+
for k in keys do
166+
match tryGetValue k source1, tryGetValue k source2, tryGetValue k source3 with
167+
| Some vx, Some vy, Some vz -> dct.Add (k, f.Invoke (vx, vy, vz))
168+
| _ , _ , _ -> ()
169+
dct :> IDictionary<'Key, 'U>
170+
match source1, source2, source3 with
171+
| (:? DefaultableDict<'Key,'T1> as s1), (:? DefaultableDict<'Key,'T2> as s2), (:? DefaultableDict<'Key,'T3> as s3) ->
172+
initWithDefault (mapper s1.DefaultValue s2.DefaultValue s3.DefaultValue) (map ())
173+
| _, _, _ -> map ()
184174

185175
/// <summary>Applies given function to each value of the given dictionary.</summary>
186176
/// <param name="chooser">The mapping function.</param>
@@ -223,17 +213,9 @@ module Dict =
223213
for KeyValue(k, v ) in source1 do d.[k] <- v
224214
for KeyValue(k, v') in source2 do d.[k] <- match d.TryGetValue k with true, v -> f.Invoke (v, v') | _ -> v'
225215
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>
234216
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())
217+
| (:? DefaultableDict<'Key,'Value> as s1), (:? DefaultableDict<'Key,'Value> as s2) -> initWithDefault (combiner s1.DefaultValue s2.DefaultValue) (combine())
218+
| (:? DefaultableDict<'Key,'Value> as s), _ | _, (:? DefaultableDict<'Key,'Value> as s) -> initWithDefault s.DefaultValue (combine())
237219
| s, empty | empty, s when empty.Count = 0 -> s
238220
| _, _ -> combine()
239221

0 commit comments

Comments
 (0)