Skip to content

Commit dd010ee

Browse files
committed
+ List.zip3Shortest
1 parent e19a4fe commit dd010ee

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/FSharpPlus/Extensions/List.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,30 @@ module List =
395395
loop (list1, list2)
396396
#endif
397397

398+
399+
/// <summary>
400+
/// Zip safely three lists. If one list is shorter, excess elements are discarded from the right end of the longer list.
401+
/// </summary>
402+
/// <param name="list1">First input list.</param>
403+
/// <param name="list2">Second input list.</param>
404+
/// <param name="list3">Third input list.</param>
405+
/// <returns>List with corresponding triples of input lists.</returns>
406+
let zip3Shortest (list1: list<'T1>) (list2: list<'T2>) (list3: list<'T3>) : list<'T1 * 'T2 * 'T3> =
407+
#if FABLE_COMPILER
408+
let rec loop acc = function
409+
| (l1::l1s, l2::l2s, l3::l3s) -> loop ((l1, l2, l3)::acc) (l1s, l2s, l3s)
410+
| (_, _, _) -> acc
411+
loop [] (list1, list2, list3) |> List.rev
412+
#else
413+
let mutable coll = new ListCollector<'T1 * 'T2 * 'T3> ()
414+
let rec loop = function
415+
| ([], _, _) | (_, [], _)| (_, _, []) -> coll.Close ()
416+
| (l1::l1s, l2::l2s, l3::l3s) ->
417+
coll.Add (l1, l2, l3)
418+
loop (l1s, l2s, l3s)
419+
loop (list1, list2, list3)
420+
#endif
421+
398422
/// <summary>
399423
/// Chunks the list up into groups with the same projected key by applying
400424
/// the key-generating projection function to each element and yielding a list of

0 commit comments

Comments
 (0)