diff --git a/MoreLinq.Test/ZipMapTest.cs b/MoreLinq.Test/ZipMapTest.cs new file mode 100644 index 000000000..b453fe3f6 --- /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 b25c873fb..1813b9341 100644 --- a/MoreLinq/Extensions.g.cs +++ b/MoreLinq/Extensions.g.cs @@ -7031,6 +7031,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 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 4397916e4..cb71aefba 100644 --- a/MoreLinq/MoreLinq.csproj +++ b/MoreLinq/MoreLinq.csproj @@ -112,6 +112,7 @@ - WindowLeft - WindowRight - ZipLongest + - ZipMap - ZipShortest $([System.Text.RegularExpressions.Regex]::Replace($(Description), `\s+`, ` `).Trim().Replace(` - `, `, `).Replace(`:,`, `:`)) diff --git a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt index 7dc5c5811..26c769aa3 100644 --- a/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt @@ -1 +1,4 @@ #nullable enable +MoreLinq.Extensions.ZipMapExtension +static MoreLinq.Extensions.ZipMapExtension.ZipMap(this System.Collections.Generic.IEnumerable! source, System.Func! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! +static MoreLinq.MoreEnumerable.ZipMap(this System.Collections.Generic.IEnumerable! source, System.Func! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! diff --git a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 7dc5c5811..26c769aa3 100644 --- a/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +1,4 @@ #nullable enable +MoreLinq.Extensions.ZipMapExtension +static MoreLinq.Extensions.ZipMapExtension.ZipMap(this System.Collections.Generic.IEnumerable! source, System.Func! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! +static MoreLinq.MoreEnumerable.ZipMap(this System.Collections.Generic.IEnumerable! source, System.Func! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! diff --git a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt index 7dc5c5811..26c769aa3 100644 --- a/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt +++ b/MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt @@ -1 +1,4 @@ #nullable enable +MoreLinq.Extensions.ZipMapExtension +static MoreLinq.Extensions.ZipMapExtension.ZipMap(this System.Collections.Generic.IEnumerable! source, System.Func! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! +static MoreLinq.MoreEnumerable.ZipMap(this System.Collections.Generic.IEnumerable! source, System.Func! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! 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 ece3287fc..687ac2b2b 100644 --- a/README.md +++ b/README.md @@ -710,6 +710,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