-
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
Conversation
FYI: This operator has been included in SuperLinq 4.0.0, which has now been published. |
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Should be changed to:
// Copyright (c) 2008 Jonathan Skeet. All rights reserved. | |
// Copyright (c) 2020 Daniel Jonsson. All rights reserved. |
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.
@matachi Thanks for contributing this and sorry for having taken so long to get back with a review. Usually, I don't pay attention to a new PR unless the CI build errors are fixed. Anyway, since master
had moved on considerably since this PR was opened, I've aligned it with a merge and added my review. Hope you'll find the time to address the comments soon.
[Test] | ||
public void ZipMapIsLazy() | ||
{ | ||
_ = new BreakingSequence<object>().ZipMap(o => o); |
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.
This could be more strict here:
_ = new BreakingSequence<object>().ZipMap(o => o); | |
_ = new BreakingSequence<object>().ZipMap(BreakingFunc.Of<object, object>()); |
public void ZipMapEmptySequence() | ||
{ | ||
object[] objects = {}; | ||
var result = objects.ZipMap(o => o); |
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.
This could be more strict:
var result = objects.ZipMap(o => o); | |
var result = objects.ZipMap(BreakingFunc.Of<object, object>()); |
[Test] | ||
public void ZipMapEmptySequence() | ||
{ | ||
object[] objects = {}; |
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 that ZipMap
disposed it:
object[] objects = {}; | |
using var objects = Enumerable.Empty<object>().AsTestingSequence(); |
[Test] | ||
public void ZipMapStrings() | ||
{ | ||
string[] strings = { "foo", "bar", "baz" }; |
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 that ZipMap
disposed it:
string[] strings = { "foo", "bar", "baz" }; | |
using var strings = TestingSequence.Of("foo", "bar", "baz"); |
[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 comment
The reason will be displayed to describe this comment to others. Learn more.
Use TestingSequence<>
as the source since that asserts on disposal that ZipMap
disposed it:
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); |
/// Applies a function on each element and returns a sequence of | ||
/// tuples with the source element and the result from the function. |
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.
/// 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. |
/// ("foo", false"), ("bar", true) and ("baz", true), in turn. | ||
/// </example> | ||
|
||
public static IEnumerable<(TSource Item, TResult Result)> ZipMap<TSource, TResult>( |
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
or Input
instead of Item
. Thoughts?
foreach (var item in source) | ||
{ | ||
yield return (item, func(item)); | ||
} |
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.
The NullArgumentTest
tests are breaking. The fix is to validate the arguments eagerly and wrap the iterator in a local function as follows (otherwise arguments are validated when the sequence is iterated rather than when ZipMap
is called):
foreach (var item in source) | |
{ | |
yield return (item, func(item)); | |
} | |
return _(); IEnumerable<(TSource, TResult)> _() | |
{ | |
foreach (var item in source) | |
{ | |
yield return (item, func(item)); | |
} | |
} |
### 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 comment
The reason will be displayed to describe this comment to others. Learn more.
a function applied on each element. | |
a function applied to each element. |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Should be changed to:
// Copyright (c) 2008 Jonathan Skeet. All rights reserved. | |
// Copyright (c) 2020 Daniel Jonsson. All rights reserved. |
Implementation of
ZipMap
proposed in #661.