-
Notifications
You must be signed in to change notification settings - Fork 416
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
Add ZipMap #763
base: master
Are you sure you want to change the base?
Add ZipMap #763
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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<object>().ZipMap(o => o); | ||||||||
} | ||||||||
|
||||||||
[Test] | ||||||||
public void ZipMapEmptySequence() | ||||||||
{ | ||||||||
object[] objects = {}; | ||||||||
var result = objects.ZipMap(o => o); | ||||||||
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. This could be more strict:
Suggested change
|
||||||||
result.AssertSequenceEqual(); | ||||||||
} | ||||||||
|
||||||||
[Test] | ||||||||
public void ZipMapStrings() | ||||||||
{ | ||||||||
string[] strings = { "foo", "bar", "baz" }; | ||||||||
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. Use
Suggested change
|
||||||||
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); | ||||||||
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. Use
Suggested change
|
||||||||
result.AssertSequenceEqual((5, false), (6, true), (7, false), (8, true)); | ||||||||
} | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,61 @@ | ||||||||||||||||||||||||
#region License and Terms | ||||||||||||||||||||||||
// MoreLINQ - Extensions to LINQ to Objects | ||||||||||||||||||||||||
// Copyright (c) 2008 Jonathan Skeet. All rights reserved. | ||||||||||||||||||||||||
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. Notice has incorrect date and author 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. Right. Should be changed to:
Suggested change
|
||||||||||||||||||||||||
// | ||||||||||||||||||||||||
// 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 and returns a sequence of | ||||||||||||||||||||||||
/// tuples with the source element and the result from the function. | ||||||||||||||||||||||||
Comment on lines
+26
to
+27
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.
Suggested change
|
||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> | ||||||||||||||||||||||||
/// <typeparam name="TResult">The type of the elements returned by <paramref name="func"/>.</typeparam> | ||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||
/// <param name="source">The sequence to iterate over.</param> | ||||||||||||||||||||||||
/// <param name="func">The function to apply on each element.</param> | ||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||
/// <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> | ||||||||||||||||||||||||
/// The <c>result</c> variable, when iterated over, will yield | ||||||||||||||||||||||||
/// ("foo", false"), ("bar", true) and ("baz", true), in turn. | ||||||||||||||||||||||||
Comment on lines
+45
to
+46
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.
Suggested change
|
||||||||||||||||||||||||
/// </example> | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public static IEnumerable<(TSource Item, TResult Result)> ZipMap<TSource, TResult>( | ||||||||||||||||||||||||
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. I think the first tuple element should be named |
||||||||||||||||||||||||
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
+55
to
+58
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
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. | ||||||
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.
Suggested change
|
||||||
|
||||||
### ZipShortest | ||||||
|
||||||
Returns a projection of tuples, where each tuple contains the N-th | ||||||
|
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.
Use
TestingSequence<>
as the source since that asserts on disposal thatZipMap
disposed it: