Skip to content

Commit 118244c

Browse files
author
Rafal Gwozdzinski
committed
Add zipShortest to NonEmptyList and NonEmptySeq
1 parent d90f750 commit 118244c

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

src/FSharpPlus/Control/Collection.fs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,11 @@ type Intercalate =
439439
type ZipShortest =
440440
inherit Default1
441441

442-
static member ZipShortest ((x1: seq<'T1> , x2: seq<'T2> , _output: seq<'T1*'T2>), _mthd: ZipShortest) = Seq.zipShortest x1 x2
443-
static member ZipShortest ((x1: 'T1 [] , x2: 'T2 [] , _output: ('T1*'T2) []), _mthd: ZipShortest) = Array.zipShortest x1 x2
444-
static member ZipShortest ((x1: ResizeArray<'T1>, x2: ResizeArray<'T2>, _output: ResizeArray<'T1*'T2>), _mthd: ZipShortest) = ResizeArray.zipShortest x1 x2
445-
static member ZipShortest ((x1: list<'T1> , x2: list<'T2> , _output: list<'T1*'T2>), _mthd: ZipShortest) = List.zipShortest x1 x2
442+
static member ZipShortest ((x1: seq<'T1> , x2: seq<'T2> , _output: seq<'T1*'T2>), _mthd: ZipShortest) = Seq.zipShortest x1 x2
443+
static member ZipShortest ((x1: 'T1 [] , x2: 'T2 [] , _output: ('T1*'T2) []), _mthd: ZipShortest) = Array.zipShortest x1 x2
444+
static member ZipShortest ((x1: ResizeArray<'T1>, x2: ResizeArray<'T2> , _output: ResizeArray<'T1*'T2>), _mthd: ZipShortest) = ResizeArray.zipShortest x1 x2
445+
static member ZipShortest ((x1: list<'T1> , x2: list<'T2> , _output: list<'T1*'T2>), _mthd: ZipShortest) = List.zipShortest x1 x2
446+
static member ZipShortest ((x1: NonEmptySeq<'T1> , x2: NonEmptySeq<'T2>, _output: NonEmptySeq<'T1*'T2>), _mthd: ZipShortest) = NonEmptySeq.zipShortest x1 x2
446447

447448
static member inline Invoke (source1: '``ZipShortestCollection<'T1>``) (source2: '``ZipShortestCollection<'T2>``) =
448449
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)
@@ -452,4 +453,8 @@ type ZipShortest =
452453
static member inline InvokeOnInstance (source1: '``ZipShortestCollection<'T1>``) (source2: '``ZipShortestCollection<'T2>``) : '``ZipShortestCollection<'T1 * 'T2>`` =
453454
((^``ZipShortestCollection<'T1>`` or ^``ZipShortestCollection<'T2>`` or ^``ZipShortestCollection<'T1 * 'T2>``) : (static member ZipShortest : _*_ -> _) source1, source2)
454455

456+
type ZipShortest with
457+
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
458+
static member inline ZipShortest ((x: '``ZipShortestCollection<'T1>`` , y: '``ZipShortestCollection<'T2>`` , _output: '``ZipShortestCollection<'T1 * 'T2>`` ), _mthd: Default1) = ZipShortest.InvokeOnInstance x y : '``ZipShortestCollection<'T1 * 'T2>``
459+
455460
#endif

src/FSharpPlus/Data/NonEmptyList.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ module NonEmptyList =
9898
/// <param name="list2">The second input list.</param>
9999
/// <returns>A single list containing pairs of matching elements from the input lists.</returns>
100100
let zip (list1: NonEmptyList<'T>) (list2: NonEmptyList<'U>) = {Head = (list1.Head, list2.Head); Tail = List.zip list1.Tail list2.Tail}
101+
102+
/// <summary>
103+
/// Zip safely two lists. If one list is shorter, excess elements are discarded from the right end of the longer list.
104+
/// </summary>
105+
/// <param name="a1">First input list.</param>
106+
/// <param name="a2">Second input list.</param>
107+
/// <returns>List with corresponding pairs of input lists.</returns>
108+
let zipShortest (list1: NonEmptyList<'T>) (list2: NonEmptyList<'U>) =
109+
{ Head = (list1.Head, list2.Head); Tail = List.zipShortest list1.Tail list2.Tail }
110+
111+
101112
/// Returns a new NonEmptyList with the element added to the beginning.
102113
let cons e {Head = x; Tail = xs} = {Head = e ; Tail = x::xs}
103114
/// Returns the first element of a new non empty list. You can also use property nel.Head.
@@ -202,6 +213,9 @@ type NonEmptyList<'t> with
202213
[<EditorBrowsable(EditorBrowsableState.Never)>]
203214
static member Zip (x, y) = NonEmptyList.zip x y
204215

216+
[<EditorBrowsable(EditorBrowsableState.Never)>]
217+
static member ZipShortest (x, y) = NonEmptyList.zipShortest x y
218+
205219
static member (>>=) ({Head = x; Tail = xs}, f: _->NonEmptyList<'b>) =
206220
let {Head = y; Tail = ys} = f x
207221
let ys' = List.collect (NonEmptyList.toList << f) xs

src/FSharpPlus/Data/NonEmptySeq.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,14 @@ module NonEmptySeq =
464464
/// <returns>The result sequence.</returns>
465465
let zip3 (source1: NonEmptySeq<_>) (source2: NonEmptySeq<_>) (source3: NonEmptySeq<_>) = Seq.zip3 source1 source2 source3 |> unsafeOfSeq
466466

467+
/// <summary>
468+
/// Zip safely two sequences. If one sequence is shorter, excess elements are discarded from the right end of the longer sequence.
469+
/// </summary>
470+
/// <param name="a1">First input sequence.</param>
471+
/// <param name="a2">Second input sequence.</param>
472+
/// <returns>Sequence with corresponding pairs of input sequences.</returns>
473+
let zipShortest (source1: NonEmptySeq<_>) (source2: NonEmptySeq<_>) = Seq.zipShortest source1 source2 |> unsafeOfSeq
474+
467475
/// <summary>Applies the given function to each element of the NonEmptySequence and concatenates all the
468476
/// results.</summary>
469477
///

tests/FSharpPlus.Tests/Data.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ module NonEmptyList =
352352
[<Test>]
353353
let ``zip `` () =
354354
nonEmptyList |> NonEmptyList.zip nonEmptyList |> NonEmptyList.toList |> shoulSeqEqual [(1,1)]
355+
356+
[<Test>]
357+
let zipShortest () =
358+
let nonEmptyList' = nonEmptyList |> NonEmptyList.cons 2
359+
nonEmptyList |> NonEmptyList.zipShortest nonEmptyList' |> NonEmptyList.toList |> shoulSeqEqual [(2,1)]
355360

356361
[<Test>]
357362
let ``get head from NonEmptyList`` () =

tests/FSharpPlus.Tests/General.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,14 @@ module Collections =
708708
CollectionAssert.AreEqual (ResizeArray [1,"a"; 2,"b"], e)
709709
Assert.IsInstanceOf<ResizeArray<int*string>> e
710710

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+
711719
module Foldable =
712720

713721
let foldables =

0 commit comments

Comments
 (0)