-
Couldn't load subscription status.
- Fork 4
Improve Usability Of CycleRange And RepeatRange For Already Materialized Sequences
#773
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
Merged
Merged
Changes from all commits
Commits
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,47 @@ | ||
| using FsCheck; | ||
| using FsCheck.Xunit; | ||
| using Funcky.Test.TestUtils; | ||
|
|
||
| namespace Funcky.Test; | ||
|
|
||
| public sealed class CycleMaterializedTest | ||
| { | ||
| [Fact] | ||
| public void IsEnumeratedLazily() | ||
| { | ||
| var doNotEnumerate = new FailOnEnumerateReadOnlyCollection<object>(Count: 1); | ||
| _ = Sequence.CycleMaterialized(doNotEnumerate); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CyclingAnEmptySetThrowsAnException() | ||
| => Assert.Throws<InvalidOperationException>(CycleEmptySequence); | ||
|
|
||
| [Property] | ||
| public Property CanProduceArbitraryManyItems(NonEmptySet<int> sequence, PositiveInt arbitraryElements) | ||
| { | ||
| var cycleRange = Sequence.CycleMaterialized(sequence.Get.Materialize()); | ||
|
|
||
| return (cycleRange.Take(arbitraryElements.Get).Count() == arbitraryElements.Get) | ||
| .ToProperty(); | ||
| } | ||
|
|
||
| [Property] | ||
| public Property RepeatsTheElementsArbitraryManyTimes(NonEmptySet<int> sequence, PositiveInt arbitraryElements) | ||
| { | ||
| var cycleRange = Sequence.CycleMaterialized(sequence.Get.Materialize()); | ||
|
|
||
| return cycleRange | ||
| .IsSequenceRepeating(sequence.Get) | ||
| .NTimes(arbitraryElements.Get) | ||
| .ToProperty(); | ||
| } | ||
|
|
||
| private static void CycleEmptySequence() | ||
| { | ||
| var cycledRange = Sequence.CycleMaterialized(Array.Empty<int>()); | ||
| using var enumerator = cycledRange.GetEnumerator(); | ||
|
|
||
| enumerator.MoveNext(); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using FsCheck; | ||
| using FsCheck.Xunit; | ||
| using Funcky.Test.TestUtils; | ||
|
|
||
| namespace Funcky.Test; | ||
|
|
||
| public sealed class RepeatMaterializedTest | ||
| { | ||
| [Fact] | ||
| public void IsEnumeratedLazily() | ||
| { | ||
| var doNotEnumerate = new FailOnEnumerateReadOnlyCollection<object>(Count: 0); | ||
| _ = Sequence.RepeatMaterialized(doNotEnumerate, 2); | ||
| } | ||
|
|
||
| [Property] | ||
| public Property ARepeatedEmptySequenceIsStillEmpty(NonNegativeInt count) | ||
| { | ||
| var repeated = Sequence.RepeatMaterialized(Array.Empty<object>(), count.Get); | ||
| return (!repeated.Any()).ToProperty(); | ||
| } | ||
|
|
||
| [Property] | ||
| public Property TheLengthOfTheGeneratedSequenceIsCorrect(List<int> list, NonNegativeInt count) | ||
| { | ||
| var repeatRange = Sequence.RepeatMaterialized(list, count.Get); | ||
|
|
||
| var materialized = repeatRange.ToList(); | ||
|
|
||
| return (materialized.Count == list.Count * count.Get).ToProperty(); | ||
| } | ||
|
|
||
| [Property] | ||
| public Property TheSequenceRepeatsTheGivenNumberOfTimes(List<int> list, NonNegativeInt count) | ||
| { | ||
| var repeatRange = Sequence.RepeatMaterialized(list, count.Get); | ||
|
|
||
| return repeatRange | ||
| .IsSequenceRepeating(list) | ||
| .NTimes(count.Get) | ||
| .ToProperty(); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
Funcky.Test/TestUtils/FailOnEnumerateReadOnlyCollection.cs
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,11 @@ | ||
| using System.Collections; | ||
| using Xunit.Sdk; | ||
|
|
||
| namespace Funcky.Test.TestUtils; | ||
|
|
||
| internal record FailOnEnumerateReadOnlyCollection<T>(int Count) : IReadOnlyCollection<T> | ||
| { | ||
| public IEnumerator<T> GetEnumerator() => throw new XunitException("Should not be enumerated"); | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace Funcky; | ||
|
|
||
| public static partial class Sequence | ||
| { | ||
| /// <summary> | ||
| /// Generates a sequence that contains the same sequence of elements over and over again as an endless generator. | ||
| /// </summary> | ||
| /// <typeparam name="TSource">Type of the elements to be cycled.</typeparam> | ||
| /// <param name="source">The sequence of elements which are cycled. Throws an exception if the sequence is empty.</param> | ||
| /// <returns>Returns an infinite IEnumerable repeating the same sequence of elements.</returns> | ||
| /// <remarks>Use <see cref="CycleRange{TSource}"/> if you need to cycle a lazy sequence.</remarks> | ||
| [Pure] | ||
| public static IEnumerable<TSource> CycleMaterialized<TSource>(IReadOnlyCollection<TSource> source) | ||
| => source.Count > 0 | ||
| ? Cycle(source).SelectMany(Identity) | ||
| : throw new InvalidOperationException("you cannot cycle an empty enumerable"); | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace Funcky; | ||
|
|
||
| public static partial class Sequence | ||
| { | ||
| /// <summary> | ||
| /// Generates a sequence that contains the same sequence of elements the given number of times. | ||
| /// </summary> | ||
| /// <typeparam name="TSource">Type of the elements to be repeated.</typeparam> | ||
| /// <param name="source">The sequence of elements to be repeated.</param> | ||
| /// <param name="count">The number of times to repeat the value in the generated sequence.</param> | ||
| /// <returns>Returns an infinite IEnumerable cycling through the same elements.</returns> | ||
| /// <remarks>Use <see cref="RepeatRange{TSource}"/> if you need to cycle a lazy sequence.</remarks> | ||
| [Pure] | ||
| public static IEnumerable<TSource> RepeatMaterialized<TSource>(IReadOnlyCollection<TSource> source, int count) | ||
| => Enumerable.Repeat(source, count).SelectMany(Identity); | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.