Skip to content

Commit 7e95ff4

Browse files
author
Rafal Gwozdzinski
committed
Make generic zip safe. Remove generic zipShortest
1 parent b3a1c59 commit 7e95ff4

File tree

5 files changed

+32
-65
lines changed

5 files changed

+32
-65
lines changed

src/FSharpPlus/Control/Collection.fs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ open System.Collections
66
open System.Collections.Generic
77
open System.Runtime.InteropServices
88
open FSharpPlus
9-
open FSharpPlus.Data
109
open FSharpPlus.Internals
1110

1211
#if !FABLE_COMPILER
@@ -437,25 +436,4 @@ type Intercalate =
437436
let inline call (a: 'a, b: 'b, s) = call_2 (a, b, s)
438437
call (Unchecked.defaultof<Intercalate>, source, sep) : 'Monoid
439438

440-
type ZipShortest =
441-
inherit Default1
442-
443-
static member ZipShortest ((x1: seq<'T1> , x2: seq<'T2> , _output: seq<'T1*'T2>), _mthd: ZipShortest) = Seq.zipShortest x1 x2
444-
static member ZipShortest ((x1: 'T1 [] , x2: 'T2 [] , _output: ('T1*'T2) []), _mthd: ZipShortest) = Array.zipShortest x1 x2
445-
static member ZipShortest ((x1: ResizeArray<'T1>, x2: ResizeArray<'T2> , _output: ResizeArray<'T1*'T2>), _mthd: ZipShortest) = ResizeArray.zipShortest x1 x2
446-
static member ZipShortest ((x1: list<'T1> , x2: list<'T2> , _output: list<'T1*'T2>), _mthd: ZipShortest) = List.zipShortest x1 x2
447-
static member ZipShortest ((x1: NonEmptySeq<'T1> , x2: NonEmptySeq<'T2>, _output: NonEmptySeq<'T1*'T2>), _mthd: ZipShortest) = NonEmptySeq.zipShortest x1 x2
448-
449-
static member inline Invoke (source1: '``ZipShortestCollection<'T1>``) (source2: '``ZipShortestCollection<'T2>``) =
450-
let inline call_4 (a: ^a, b: ^b, c: ^c, d: ^d) = ((^a or ^b or ^c or ^d) : (static member ZipShortest : (_*_*_)*_ -> _) (b, c, d), a)
451-
let inline call (a: 'a, b: 'b, c: 'c) = call_4 (a, b, c, Unchecked.defaultof<'r>) : 'r
452-
call (Unchecked.defaultof<ZipShortest>, source1, source2) : '``ZipShortestCollection<'T1 * 'T2>``
453-
454-
static member inline InvokeOnInstance (source1: '``ZipShortestCollection<'T1>``) (source2: '``ZipShortestCollection<'T2>``) : '``ZipShortestCollection<'T1 * 'T2>`` =
455-
((^``ZipShortestCollection<'T1>`` or ^``ZipShortestCollection<'T2>`` or ^``ZipShortestCollection<'T1 * 'T2>``) : (static member ZipShortest : _*_ -> _) source1, source2)
456-
457-
type ZipShortest with
458-
static member inline ZipShortest ((_: ^t when ^t : null and ^t: struct, _: ^u when ^u : null and ^u: struct, _output: ^r when ^r : null and ^r: struct), _mthd: Default1) = id
459-
static member inline ZipShortest ((x: '``ZipShortestCollection<'T1>`` , y: '``ZipShortestCollection<'T2>`` , _output: '``ZipShortestCollection<'T1 * 'T2>`` ), _mthd: Default1) = ZipShortest.InvokeOnInstance x y : '``ZipShortestCollection<'T1 * 'T2>``
460-
461439
#endif

src/FSharpPlus/Control/Functor.fs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,21 @@ type Unzip =
169169
type Zip =
170170
inherit Default1
171171

172-
static member Zip ((x: IEnumerator<'T> , y: IEnumerator<'U> , _output: IEnumerator<'T*'U> ), _mthd: Zip) = Enumerator.zip x y
173-
static member Zip ((x: seq<'T> , y: seq<'U> , _output: seq<'T*'U> ), _mthd: Zip) = Seq.zip x y
174-
static member Zip ((x: NonEmptySeq<'T> , y: NonEmptySeq<'U> , _output: NonEmptySeq<'T*'U> ), _mthd: Zip) = NonEmptySeq.zip x y
175-
static member Zip ((x: IDictionary<'K, 'T> , y: IDictionary<'K,'U> , _output: IDictionary<'K,'T*'U> ), _mthd: Zip) = Dict.zip x y
172+
static member Zip ((x: IEnumerator<'T> , y: IEnumerator<'U> , _output: IEnumerator<'T*'U> ), _mthd: Zip) = Enumerator.zip x y
173+
static member Zip ((x: seq<'T> , y: seq<'U> , _output: seq<'T*'U> ), _mthd: Zip) = Seq.zipShortest x y
174+
static member Zip ((x: NonEmptySeq<'T> , y: NonEmptySeq<'U> , _output: NonEmptySeq<'T*'U> ), _mthd: Zip) = NonEmptySeq.zipShortest x y
175+
static member Zip ((x: IDictionary<'K, 'T> , y: IDictionary<'K,'U> , _output: IDictionary<'K,'T*'U> ), _mthd: Zip) = Dict.zip x y
176176
static member Zip ((x: IReadOnlyDictionary<'K, 'T>, y: IReadOnlyDictionary<'K,'U>, _output: IReadOnlyDictionary<'K,'T*'U>), _mthd: Zip) = IReadOnlyDictionary.zip x y
177177
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>
178178
static member Zip ((x: Map<'K, 'T> , y: Map<'K,'U> , _output: Map<'K,'T*'U> ), _mthd: Zip) = Map.zip x y
179179
static member Zip ((f: 'R -> 'T , g: 'R -> 'U , _output: 'R -> 'T * 'U ), _mthd: Zip) = fun x -> (f x, g x)
180180
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))
181-
static member Zip ((x: list<'T> , y: list<'U> , _output: list<'T*'U> ), _mthd: Zip) = List.zip x y
182-
static member Zip ((x: 'T [] , y: 'U [] , _output: ('T*'U) [] ), _mthd: Zip) = Array.zip x y
183-
static member Zip ((x: option<'T> , y: option<'U> , _output: option<'T*'U> ), _mthd: Zip) = Option.zip x y
184-
static member Zip ((x: Async<'T> , y: Async<'U> , _output: Async<'T*'U> ), _mthd: Zip) = Async.zip x y
185-
static member Zip ((x: Task<'T> , y: Task<'U> , _output: Task<'T*'U> ), _mthd: Zip) = Task.zip x y
181+
static member Zip ((x: list<'T> , y: list<'U> , _output: list<'T*'U> ), _mthd: Zip) = List.zipShortest x y
182+
static member Zip ((x: 'T [] , y: 'U [] , _output: ('T*'U) [] ), _mthd: Zip) = Array.zipShortest x y
183+
static member Zip ((x: ResizeArray<'T> , y: ResizeArray<'U> , _output: ResizeArray<'T*'U> ), _mthd: Zip) = ResizeArray.zipShortest x y
184+
static member Zip ((x: option<'T> , y: option<'U> , _output: option<'T*'U> ), _mthd: Zip) = Option.zip x y
185+
static member Zip ((x: Async<'T> , y: Async<'U> , _output: Async<'T*'U> ), _mthd: Zip) = Async.zip x y
186+
static member Zip ((x: Task<'T> , y: Task<'U> , _output: Task<'T*'U> ), _mthd: Zip) = Task.zip x y
186187

187188
static member inline Invoke (source1: '``ZipFunctor<'T1>``) (source2: '``ZipFunctor<'T2>``) =
188189
let inline call_4 (a: ^a, b: ^b, c: ^c, d: ^d) = ((^a or ^b or ^c or ^d) : (static member Zip : (_*_*_)*_ -> _) (b, c, d), a)

src/FSharpPlus/Data/NonEmptyList.fs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@ type NonEmptyList<'t> with
211211
static member Unzip s = NonEmptyList.unzip s
212212

213213
[<EditorBrowsable(EditorBrowsableState.Never)>]
214-
static member Zip (x, y) = NonEmptyList.zip x y
215-
216-
[<EditorBrowsable(EditorBrowsableState.Never)>]
217-
static member ZipShortest (x, y) = NonEmptyList.zipShortest x y
214+
static member Zip (x, y) = NonEmptyList.zipShortest x y
218215

219216
static member (>>=) ({Head = x; Tail = xs}, f: _->NonEmptyList<'b>) =
220217
let {Head = y; Tail = ys} = f x

src/FSharpPlus/Operators.fs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ module Operators =
9393
// Un-zips (un-tuple) two functors.
9494
let inline unzip (source: '``Functor<'T1 * 'T2>``) = Unzip.Invoke source : '``Functor<'T1>`` * '``Functor<'T2>``
9595

96-
// Zips (tuple) two functors.
97-
let inline zip (source1: '``ZipFunctor<'T1>``) (source2: '``ZipFunctor<'T2>``) : '``ZipFunctor<'T1 * 'T2>`` = Zip.Invoke source1 source2
98-
9996
// Zips safely two collections. If one collection is shorter, excess elements are discarded from the right end of the longer collection.
100-
let inline zipShortest (source1: '``ZipShortestCollection<'T1>``) (source2: '``ZipShortestCollection<'T2>``) : '``ZipShortestCollection<'T1 * 'T2>`` = ZipShortest.Invoke source1 source2
97+
let inline zip (source1: '``ZipFunctor<'T1>``) (source2: '``ZipFunctor<'T2>``) : '``ZipFunctor<'T1 * 'T2>`` = Zip.Invoke source1 source2
10198

99+
102100
// Applicative ------------------------------------------------------------
103101

104102

tests/FSharpPlus.Tests/General.fs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,25 @@ module Functor =
490490

491491
()
492492

493+
[<Test>]
494+
let genericZipShortest () =
495+
let s = zip (seq [1; 2]) (seq ["a"; "b"; "c"])
496+
CollectionAssert.AreEqual (seq [1,"a"; 2,"b"], s)
497+
498+
let a = zip [|1; 2; 3|] [|"a"; "b"|]
499+
CollectionAssert.AreEqual ([|1,"a"; 2,"b"|], a)
500+
501+
let l = zip [1; 2] ["a"; "b"; "c"]
502+
CollectionAssert.AreEqual ([1,"a"; 2,"b"], l)
503+
504+
let e = zip (ResizeArray [1; 2]) (ResizeArray ["a"; "b"; "c"])
505+
CollectionAssert.AreEqual (ResizeArray [1,"a"; 2,"b"], e)
506+
507+
let nes = zip (NonEmptySeq.ofList [1; 2]) (NonEmptySeq.ofList ["a"; "b"; "c"])
508+
CollectionAssert.AreEqual (NonEmptySeq.ofList [1,"a"; 2,"b"], nes)
509+
510+
let nel = zip (NonEmptyList.ofList [1; 2]) (NonEmptyList.ofList ["a"; "b"; "c"])
511+
CollectionAssert.AreEqual (NonEmptyList.ofList [1,"a"; 2,"b"], nel)
493512

494513
module Collections =
495514

@@ -690,32 +709,6 @@ module Collections =
690709
let rl = ReadOnlyListOnlyIndex [1..10]
691710
Assert.AreEqual (2, nth 1 rl)
692711

693-
[<Test>]
694-
let zipShortest () =
695-
let s = zipShortest (seq [1; 2]) (seq ["a"; "b"; "c"])
696-
CollectionAssert.AreEqual (seq [1,"a"; 2,"b"], s)
697-
Assert.IsInstanceOf<seq<int*string>> s
698-
699-
let a = zipShortest [|1; 2; 3|] [|"a"; "b"|]
700-
CollectionAssert.AreEqual ([|1,"a"; 2,"b"|], a)
701-
Assert.IsInstanceOf<array<int*string>> a
702-
703-
let l = zipShortest [1; 2] ["a"; "b"; "c"]
704-
CollectionAssert.AreEqual ([1,"a"; 2,"b"], l)
705-
Assert.IsInstanceOf<list<int*string>> l
706-
707-
let e = zipShortest (ResizeArray [1; 2]) (ResizeArray ["a"; "b"; "c"])
708-
CollectionAssert.AreEqual (ResizeArray [1,"a"; 2,"b"], e)
709-
Assert.IsInstanceOf<ResizeArray<int*string>> e
710-
711-
let nes = zipShortest (NonEmptySeq.ofList [1; 2]) (NonEmptySeq.ofList ["a"; "b"; "c"])
712-
CollectionAssert.AreEqual (NonEmptySeq.ofList [1,"a"; 2,"b"], nes)
713-
Assert.IsInstanceOf<neseq<int*string>> nes
714-
715-
let nel = zipShortest (NonEmptyList.ofList [1; 2]) (NonEmptyList.ofList ["a"; "b"; "c"])
716-
CollectionAssert.AreEqual (NonEmptyList.ofList [1,"a"; 2,"b"], nel)
717-
Assert.IsInstanceOf<nelist<int*string>> nel
718-
719712
module Foldable =
720713

721714
let foldables =

0 commit comments

Comments
 (0)