From d4e12a157edde69f77a1df631975c8c656ea3d83 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 1 Oct 2020 21:57:06 +0200 Subject: [PATCH 01/13] Add ZipMap --- MoreLinq.Test/ZipMapTest.cs | 55 +++++++++++++++++++++++++++++++++ MoreLinq/Extensions.g.cs | 35 +++++++++++++++++++++ MoreLinq/MoreLinq.csproj | 1 + MoreLinq/ZipMap.cs | 61 +++++++++++++++++++++++++++++++++++++ README.md | 5 +++ 5 files changed, 157 insertions(+) create mode 100644 MoreLinq.Test/ZipMapTest.cs create mode 100644 MoreLinq/ZipMap.cs diff --git a/MoreLinq.Test/ZipMapTest.cs b/MoreLinq.Test/ZipMapTest.cs new file mode 100644 index 000000000..498090b57 --- /dev/null +++ b/MoreLinq.Test/ZipMapTest.cs @@ -0,0 +1,55 @@ +#region License and Terms +// MoreLINQ - Extensions to LINQ to Objects +// Copyright (c) 2008 Jonathan Skeet. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +namespace MoreLinq.Test +{ + using System.Text.RegularExpressions; + using NUnit.Framework; + + [TestFixture] + public class ZipMapTest + { + [Test] + public void ZipMapIsLazy() + { + new BreakingSequence().ZipMap(o => o); + } + + [Test] + public void ZipMapEmptySequence() + { + object[] objects = {}; + var result = objects.ZipMap(o => o); + result.AssertSequenceEqual(); + } + + [Test] + public void ZipMapStrings() + { + string[] strings = { "foo", "bar", "baz" }; + var result = strings.ZipMap(s => Regex.IsMatch(s, @"^b")); + result.AssertSequenceEqual(("foo", false), ("bar", true), ("baz", true)); + } + + [Test] + public void ZipMapFromSequence() + { + var result = MoreEnumerable.Sequence(5, 8).ZipMap(i => i % 2 == 0); + result.AssertSequenceEqual((5, false), (6, true), (7, false), (8, true)); + } + } +} diff --git a/MoreLinq/Extensions.g.cs b/MoreLinq/Extensions.g.cs index 2bab62b83..988822cd9 100644 --- a/MoreLinq/Extensions.g.cs +++ b/MoreLinq/Extensions.g.cs @@ -6844,6 +6844,41 @@ public static IEnumerable ZipLongest( } + /// ZipMap extension. + + [GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")] + public static partial class ZipMapExtension + { + /// + /// Applies a function on each element and returns a sequence of + /// tuples with the source element and the result from the function. + /// + /// The type of the elements of . + /// The type of the elements returned by . + /// The sequence to iterate over. + /// The function to apply on each element. + /// + /// Returns a sequence of tuples with the the source element and the + /// result from parameter. + /// + /// + /// This operator uses deferred execution and streams its results. + /// + /// + /// Regex.IsMatch(s, @"^b")); + /// ]]> + /// The result variable, when iterated over, will yield + /// ("foo", false"), ("bar", true) and ("baz", true), in turn. + /// + + public static IEnumerable<(TSource Item, TResult Result)> ZipMap( + this IEnumerable source, Func func) + => MoreEnumerable.ZipMap(source, func); + + } + /// ZipShortest extension. [GeneratedCode("MoreLinq.ExtensionsGenerator", "1.0.0.0")] diff --git a/MoreLinq/MoreLinq.csproj b/MoreLinq/MoreLinq.csproj index ee4dfcdba..0837783a3 100644 --- a/MoreLinq/MoreLinq.csproj +++ b/MoreLinq/MoreLinq.csproj @@ -111,6 +111,7 @@ - WindowLeft - WindowRight - ZipLongest + - ZipMap - ZipShortest $([System.Text.RegularExpressions.Regex]::Replace($(Description), `\s+`, ` `).Trim().Replace(` - `, `, `).Replace(`:,`, `:`)) diff --git a/MoreLinq/ZipMap.cs b/MoreLinq/ZipMap.cs new file mode 100644 index 000000000..06da48222 --- /dev/null +++ b/MoreLinq/ZipMap.cs @@ -0,0 +1,61 @@ +#region License and Terms +// MoreLINQ - Extensions to LINQ to Objects +// Copyright (c) 2008 Jonathan Skeet. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +namespace MoreLinq +{ + using System; + using System.Collections.Generic; + + static partial class MoreEnumerable + { + /// + /// Applies a function on each element and returns a sequence of + /// tuples with the source element and the result from the function. + /// + /// The type of the elements of . + /// The type of the elements returned by . + /// The sequence to iterate over. + /// The function to apply on each element. + /// + /// Returns a sequence of tuples with the source element and the + /// result from parameter. + /// + /// + /// This operator uses deferred execution and streams its results. + /// + /// + /// Regex.IsMatch(s, @"^b")); + /// ]]> + /// The result variable, when iterated over, will yield + /// ("foo", false"), ("bar", true) and ("baz", true), in turn. + /// + + public static IEnumerable<(TSource Item, TResult Result)> ZipMap( + this IEnumerable source, Func func) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + if (func == null) throw new ArgumentNullException(nameof(func)); + + foreach (var item in source) + { + yield return (item, func(item)); + } + } + } +} diff --git a/README.md b/README.md index 03631fdef..8c9406b25 100644 --- a/README.md +++ b/README.md @@ -717,6 +717,11 @@ for padding. This method has 3 overloads. +### ZipMap + +Returns a sequence of tuples containing the source elements and the result of +a function applied on each element. + ### ZipShortest Returns a projection of tuples, where each tuple contains the N-th From 05c210e03a52a442ab46fda26530c783ddea6666 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sat, 1 Apr 2023 16:35:00 +0200 Subject: [PATCH 02/13] Refresh generated code --- MoreLinq/Extensions.g.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq/Extensions.g.cs b/MoreLinq/Extensions.g.cs index 988822cd9..8860a853e 100644 --- a/MoreLinq/Extensions.g.cs +++ b/MoreLinq/Extensions.g.cs @@ -6858,7 +6858,7 @@ public static partial class ZipMapExtension /// The sequence to iterate over. /// The function to apply on each element. /// - /// Returns a sequence of tuples with the the source element and the + /// Returns a sequence of tuples with the source element and the /// result from parameter. /// /// From 0e43bd1113f574bc37a08ca3cf66f72f6963cc7f Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:27:17 +0200 Subject: [PATCH 03/13] Update MoreLinq/ZipMap.cs Co-authored-by: Atif Aziz --- MoreLinq/ZipMap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq/ZipMap.cs b/MoreLinq/ZipMap.cs index 06da48222..71931f226 100644 --- a/MoreLinq/ZipMap.cs +++ b/MoreLinq/ZipMap.cs @@ -1,6 +1,6 @@ #region License and Terms // MoreLINQ - Extensions to LINQ to Objects -// Copyright (c) 2008 Jonathan Skeet. All rights reserved. +// Copyright (c) 2020 Daniel Jonsson. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 259296517703e5cdeadc8b5c60c5efc7c168803c Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:27:54 +0200 Subject: [PATCH 04/13] Update MoreLinq.Test/ZipMapTest.cs Co-authored-by: Atif Aziz --- MoreLinq.Test/ZipMapTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq.Test/ZipMapTest.cs b/MoreLinq.Test/ZipMapTest.cs index b453fe3f6..820e5371c 100644 --- a/MoreLinq.Test/ZipMapTest.cs +++ b/MoreLinq.Test/ZipMapTest.cs @@ -26,7 +26,7 @@ public class ZipMapTest [Test] public void ZipMapIsLazy() { - _ = new BreakingSequence().ZipMap(o => o); + _ = new BreakingSequence().ZipMap(BreakingFunc.Of()); } [Test] From 0f3244fe50574bf793c5ff5f2198ba860a607680 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:28:04 +0200 Subject: [PATCH 05/13] Update MoreLinq.Test/ZipMapTest.cs Co-authored-by: Atif Aziz --- MoreLinq.Test/ZipMapTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq.Test/ZipMapTest.cs b/MoreLinq.Test/ZipMapTest.cs index 820e5371c..ed2296849 100644 --- a/MoreLinq.Test/ZipMapTest.cs +++ b/MoreLinq.Test/ZipMapTest.cs @@ -33,7 +33,7 @@ public void ZipMapIsLazy() public void ZipMapEmptySequence() { object[] objects = {}; - var result = objects.ZipMap(o => o); + var result = objects.ZipMap(BreakingFunc.Of()); result.AssertSequenceEqual(); } From ee1adf04dfe93100ed7bee574c2d8b199160d2f0 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:28:24 +0200 Subject: [PATCH 06/13] Update MoreLinq.Test/ZipMapTest.cs Co-authored-by: Atif Aziz --- MoreLinq.Test/ZipMapTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq.Test/ZipMapTest.cs b/MoreLinq.Test/ZipMapTest.cs index ed2296849..e60e7033a 100644 --- a/MoreLinq.Test/ZipMapTest.cs +++ b/MoreLinq.Test/ZipMapTest.cs @@ -32,7 +32,7 @@ public void ZipMapIsLazy() [Test] public void ZipMapEmptySequence() { - object[] objects = {}; + using var objects = Enumerable.Empty().AsTestingSequence(); var result = objects.ZipMap(BreakingFunc.Of()); result.AssertSequenceEqual(); } From 5a0bf3b87471f7310805f38cba779cb397f781e1 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:28:34 +0200 Subject: [PATCH 07/13] Update MoreLinq.Test/ZipMapTest.cs Co-authored-by: Atif Aziz --- MoreLinq.Test/ZipMapTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq.Test/ZipMapTest.cs b/MoreLinq.Test/ZipMapTest.cs index e60e7033a..1b8acd72f 100644 --- a/MoreLinq.Test/ZipMapTest.cs +++ b/MoreLinq.Test/ZipMapTest.cs @@ -40,7 +40,7 @@ public void ZipMapEmptySequence() [Test] public void ZipMapStrings() { - string[] strings = { "foo", "bar", "baz" }; + using var strings = TestingSequence.Of("foo", "bar", "baz"); var result = strings.ZipMap(s => Regex.IsMatch(s, @"^b")); result.AssertSequenceEqual(("foo", false), ("bar", true), ("baz", true)); } From f92fd01e69831c5b8d34616f163a7376de9c6952 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:28:43 +0200 Subject: [PATCH 08/13] Update MoreLinq.Test/ZipMapTest.cs Co-authored-by: Atif Aziz --- MoreLinq.Test/ZipMapTest.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MoreLinq.Test/ZipMapTest.cs b/MoreLinq.Test/ZipMapTest.cs index 1b8acd72f..80d334f75 100644 --- a/MoreLinq.Test/ZipMapTest.cs +++ b/MoreLinq.Test/ZipMapTest.cs @@ -48,7 +48,8 @@ public void ZipMapStrings() [Test] public void ZipMapFromSequence() { - var result = MoreEnumerable.Sequence(5, 8).ZipMap(i => i % 2 == 0); + using var xs = TestingSequence.Of(5, 6, 7, 8); + var result = xs.ZipMap(i => i % 2 == 0); result.AssertSequenceEqual((5, false), (6, true), (7, false), (8, true)); } } From 28e314d3e4cc09474e9ae90de1df33e4ef1d5c01 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:28:56 +0200 Subject: [PATCH 09/13] Update MoreLinq/ZipMap.cs Co-authored-by: Atif Aziz --- MoreLinq/ZipMap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq/ZipMap.cs b/MoreLinq/ZipMap.cs index 71931f226..1f01c322f 100644 --- a/MoreLinq/ZipMap.cs +++ b/MoreLinq/ZipMap.cs @@ -29,7 +29,7 @@ static partial class MoreEnumerable /// The type of the elements of . /// The type of the elements returned by . /// The sequence to iterate over. - /// The function to apply on each element. + /// The function to apply to each element. /// /// Returns a sequence of tuples with the source element and the /// result from parameter. From 84bd9db3c92d2defc59c577f3322387651d7fc8d Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:29:27 +0200 Subject: [PATCH 10/13] Update MoreLinq/ZipMap.cs Co-authored-by: Atif Aziz --- MoreLinq/ZipMap.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MoreLinq/ZipMap.cs b/MoreLinq/ZipMap.cs index 1f01c322f..9b20ae658 100644 --- a/MoreLinq/ZipMap.cs +++ b/MoreLinq/ZipMap.cs @@ -42,8 +42,10 @@ static partial class MoreEnumerable /// string[] strings = { "foo", "bar", "baz" }; /// var result = strings.ZipMap(s => Regex.IsMatch(s, @"^b")); /// ]]> + /// /// The result variable, when iterated over, will yield - /// ("foo", false"), ("bar", true) and ("baz", true), in turn. + /// ("foo", false), ("bar", true) and + /// ("baz", true), in turn. /// public static IEnumerable<(TSource Item, TResult Result)> ZipMap( From edd79f8a7d17536b4dae0e5d4ff2fac4aec4735f Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:29:39 +0200 Subject: [PATCH 11/13] Update MoreLinq/ZipMap.cs Co-authored-by: Atif Aziz --- MoreLinq/ZipMap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoreLinq/ZipMap.cs b/MoreLinq/ZipMap.cs index 9b20ae658..8c140b9bd 100644 --- a/MoreLinq/ZipMap.cs +++ b/MoreLinq/ZipMap.cs @@ -27,7 +27,7 @@ static partial class MoreEnumerable /// tuples with the source element and the result from the function. /// /// The type of the elements of . - /// The type of the elements returned by . + /// The type of value returned by . /// The sequence to iterate over. /// The function to apply to each element. /// From 5285f2fbf9e95805d4c4a8401c1f0638fd884579 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:29:51 +0200 Subject: [PATCH 12/13] Update MoreLinq/ZipMap.cs Co-authored-by: Atif Aziz --- MoreLinq/ZipMap.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/MoreLinq/ZipMap.cs b/MoreLinq/ZipMap.cs index 8c140b9bd..4303e3627 100644 --- a/MoreLinq/ZipMap.cs +++ b/MoreLinq/ZipMap.cs @@ -23,8 +23,9 @@ namespace MoreLinq static partial class MoreEnumerable { /// - /// Applies a function on each element and returns a sequence of - /// tuples with the source element and the result from the function. + /// Applies a function on each element of the sequence and returns a + /// sequence of tuples with the source element and the result from the + /// function. /// /// The type of the elements of . /// The type of value returned by . From 7b6ce58f92239a6499c12575c10b3ce198db8f0e Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 26 Jun 2025 06:30:43 +0200 Subject: [PATCH 13/13] Update README.md Co-authored-by: Atif Aziz --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 687ac2b2b..efcd1316f 100644 --- a/README.md +++ b/README.md @@ -713,7 +713,7 @@ This method has 3 overloads. ### ZipMap Returns a sequence of tuples containing the source elements and the result of -a function applied on each element. +a function applied to each element. ### ZipShortest