From d4e12a157edde69f77a1df631975c8c656ea3d83 Mon Sep 17 00:00:00 2001 From: Daniel Jonsson Date: Thu, 1 Oct 2020 21:57:06 +0200 Subject: [PATCH 1/2] 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 2/2] 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. /// ///