-
Notifications
You must be signed in to change notification settings - Fork 418
Add ZipMap #763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanOpcode
wants to merge
15
commits into
morelinq:master
Choose a base branch
from
DanOpcode:zipmap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ZipMap #763
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d4e12a1
Add ZipMap
05c210e
Refresh generated code
atifaziz 0e9a406
Merge remote-tracking branch 'upstream/master' into zipmap
atifaziz ac093b4
Merge remote-tracking branch 'upstream/master' into zipmap
atifaziz 0e43bd1
Update MoreLinq/ZipMap.cs
DanOpcode 2592965
Update MoreLinq.Test/ZipMapTest.cs
DanOpcode 0f3244f
Update MoreLinq.Test/ZipMapTest.cs
DanOpcode ee1adf0
Update MoreLinq.Test/ZipMapTest.cs
DanOpcode 5a0bf3b
Update MoreLinq.Test/ZipMapTest.cs
DanOpcode f92fd01
Update MoreLinq.Test/ZipMapTest.cs
DanOpcode 28e314d
Update MoreLinq/ZipMap.cs
DanOpcode 84bd9db
Update MoreLinq/ZipMap.cs
DanOpcode edd79f8
Update MoreLinq/ZipMap.cs
DanOpcode 5285f2f
Update MoreLinq/ZipMap.cs
DanOpcode 7b6ce58
Update README.md
DanOpcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#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<object>().ZipMap(BreakingFunc.Of<object, object>()); | ||
} | ||
|
||
[Test] | ||
public void ZipMapEmptySequence() | ||
{ | ||
using var objects = Enumerable.Empty<object>().AsTestingSequence(); | ||
var result = objects.ZipMap(BreakingFunc.Of<object, object>()); | ||
result.AssertSequenceEqual(); | ||
} | ||
|
||
[Test] | ||
public void ZipMapStrings() | ||
{ | ||
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)); | ||
} | ||
|
||
[Test] | ||
public void ZipMapFromSequence() | ||
{ | ||
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)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
#nullable enable | ||
MoreLinq.Extensions.ZipMapExtension | ||
static MoreLinq.Extensions.ZipMapExtension.ZipMap<TSource, TResult>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, TResult>! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! | ||
static MoreLinq.MoreEnumerable.ZipMap<TSource, TResult>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, TResult>! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
#nullable enable | ||
MoreLinq.Extensions.ZipMapExtension | ||
static MoreLinq.Extensions.ZipMapExtension.ZipMap<TSource, TResult>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, TResult>! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! | ||
static MoreLinq.MoreEnumerable.ZipMap<TSource, TResult>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, TResult>! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
#nullable enable | ||
MoreLinq.Extensions.ZipMapExtension | ||
static MoreLinq.Extensions.ZipMapExtension.ZipMap<TSource, TResult>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, TResult>! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! | ||
static MoreLinq.MoreEnumerable.ZipMap<TSource, TResult>(this System.Collections.Generic.IEnumerable<TSource>! source, System.Func<TSource, TResult>! func) -> System.Collections.Generic.IEnumerable<(TSource Item, TResult Result)>! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,64 @@ | ||||||||||||||||||||||||
#region License and Terms | ||||||||||||||||||||||||
// MoreLINQ - Extensions to LINQ to Objects | ||||||||||||||||||||||||
// 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. | ||||||||||||||||||||||||
// 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 | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||
/// 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. | ||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> | ||||||||||||||||||||||||
/// <typeparam name="TResult">The type of value returned by <paramref name="func"/>.</typeparam> | ||||||||||||||||||||||||
/// <param name="source">The sequence to iterate over.</param> | ||||||||||||||||||||||||
/// <param name="func">The function to apply to each element.</param> | ||||||||||||||||||||||||
/// <returns> | ||||||||||||||||||||||||
/// Returns a sequence of tuples with the source element and the | ||||||||||||||||||||||||
/// result from <paramref name="func"/> parameter. | ||||||||||||||||||||||||
/// </returns> | ||||||||||||||||||||||||
/// <remarks> | ||||||||||||||||||||||||
/// This operator uses deferred execution and streams its results. | ||||||||||||||||||||||||
/// </remarks> | ||||||||||||||||||||||||
/// <example> | ||||||||||||||||||||||||
/// <code><![CDATA[ | ||||||||||||||||||||||||
/// string[] strings = { "foo", "bar", "baz" }; | ||||||||||||||||||||||||
/// var result = strings.ZipMap(s => Regex.IsMatch(s, @"^b")); | ||||||||||||||||||||||||
/// ]]></code> | ||||||||||||||||||||||||
/// <para> | ||||||||||||||||||||||||
/// The <c>result</c> variable, when iterated over, will yield | ||||||||||||||||||||||||
/// <c>("foo", false)</c>, <c>("bar", true)</c> and | ||||||||||||||||||||||||
/// <c>("baz", true)</c>, in turn.</para> | ||||||||||||||||||||||||
/// </example> | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public static IEnumerable<(TSource Item, TResult Result)> ZipMap<TSource, TResult>( | ||||||||||||||||||||||||
this IEnumerable<TSource> source, Func<TSource, TResult> 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)); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the first tuple element should be named
Source
orInput
instead ofItem
. Thoughts?