Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add ZipMap #763

wants to merge 4 commits into from

Conversation

matachi
Copy link

@matachi matachi commented Oct 1, 2020

Implementation of ZipMap proposed in #661.

@viceroypenguin
Copy link
Contributor

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.
Copy link
Contributor

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

Copy link
Member

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:

Suggested change
// Copyright (c) 2008 Jonathan Skeet. All rights reserved.
// Copyright (c) 2020 Daniel Jonsson. All rights reserved.

Copy link
Member

@atifaziz atifaziz left a 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);
Copy link
Member

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:

Suggested change
_ = 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);
Copy link
Member

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:

Suggested change
var result = objects.ZipMap(o => o);
var result = objects.ZipMap(BreakingFunc.Of<object, object>());

[Test]
public void ZipMapEmptySequence()
{
object[] objects = {};
Copy link
Member

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:

Suggested change
object[] objects = {};
using var objects = Enumerable.Empty<object>().AsTestingSequence();

[Test]
public void ZipMapStrings()
{
string[] strings = { "foo", "bar", "baz" };
Copy link
Member

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:

Suggested change
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);
Copy link
Member

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:

Suggested change
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);

Comment on lines +26 to +27
/// Applies a function on each element and returns a sequence of
/// tuples with the source element and the result from the function.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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>(
Copy link
Member

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?

Comment on lines +55 to +58
foreach (var item in source)
{
yield return (item, func(item));
}
Copy link
Member

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):

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

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:

Suggested change
// Copyright (c) 2008 Jonathan Skeet. All rights reserved.
// Copyright (c) 2020 Daniel Jonsson. All rights reserved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants