Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions HW5/HW5.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyLinq", "MyLinq\MyLinq.csproj", "{9E0833E5-8DE4-4505-BB56-7C17CFBB40E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyLinq.Test", "MyLinq.Test\MyLinq.Test.csproj", "{A48D14A0-31F1-43A2-9A88-2FF814891FF3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9E0833E5-8DE4-4505-BB56-7C17CFBB40E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E0833E5-8DE4-4505-BB56-7C17CFBB40E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E0833E5-8DE4-4505-BB56-7C17CFBB40E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E0833E5-8DE4-4505-BB56-7C17CFBB40E6}.Release|Any CPU.Build.0 = Release|Any CPU
{A48D14A0-31F1-43A2-9A88-2FF814891FF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A48D14A0-31F1-43A2-9A88-2FF814891FF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A48D14A0-31F1-43A2-9A88-2FF814891FF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A48D14A0-31F1-43A2-9A88-2FF814891FF3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
39 changes: 39 additions & 0 deletions HW5/MyLinq.Test/MyLinq.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MyLinq\MyLinq.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions HW5/MyLinq.Test/MyLinqTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// <copyright file="MyLinqTests.cs" company="khusainovilas">
// Copyright (c) khusainovilas. All rights reserved.
// </copyright>

namespace MyLinq.Test;

/// <summary>
/// Unit tests for MyLinq.
/// </summary>
public class MyLinqTests
{
/// <summary>
/// test method GetPrimes by taking first 5 numbers.
/// </summary>
[Test]
public void Linq_GetPrimes_FirstFiveNumbers()
=> Assert.That(Linq.GetPrimes().Take(5), Is.EquivalentTo(new[] { 2L, 3L, 5L, 7L, 11L }));

/// <summary>
/// test method Take by taking first 4 elements of char array.
/// </summary>
[Test]
public void Linq_Take_FirstFourElements()
=> Assert.That(new[] { 'a', 'b', 'c', 'd', 'e', 'f' }.Take(4), Is.EquivalentTo(new[] { 'a', 'b', 'c', 'd' }));

/// <summary>
/// test method Skip by skipping first 8 elements of char array.
/// </summary>
[Test]
public void Linq_Skip_SkipEightElements()
{
var text = "Hello, World!".ToCharArray();
Assert.That(text.Skip(8), Is.EquivalentTo("orld!".ToCharArray()));
}

/// <summary>
/// test method take and skip combination by getting 5 elements after skipping first 3 primes.
/// </summary>
[Test]
public void Linq_SkipAndTakeCombination()
=> Assert.That(Linq.GetPrimes().Skip(3).Take(5), Is.EquivalentTo(new[] { 7L, 11L, 13L, 17L, 19L }));

/// <summary>
/// test skip with negative count should return empty sequence.
/// </summary>
[Test]
public void Linq_Skip_NegativeCount()
=> Assert.That(Linq.GetPrimes().Skip(-5), Is.Empty);

/// <summary>
/// test take with negative count should return empty sequence.
/// </summary>
[Test]
public void Linq_Take_NegativeCount()
=> Assert.That(Linq.GetPrimes().Take(-10), Is.Empty);
}
9 changes: 9 additions & 0 deletions HW5/MyLinq.Test/stylecop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
"settings": {
"documentationRules": {
"companyName": "khusainovilas",
"copyrightText": "Copyright (c) {companyName}. All rights reserved."
}
}
}
103 changes: 103 additions & 0 deletions HW5/MyLinq/Linq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// <copyright file="Linq.cs" company="khusainovilas">
// Copyright (c) khusainovilas. All rights reserved.
// </copyright>

namespace MyLinq;

/// <summary>
/// methods for IEnumerable collections.
/// </summary>
public static class Linq
{
/// <summary>
/// Generates an infinite lazy sequence of prime numbers.
/// </summary>
/// <returns>An IEnumerable of prime numbers.</returns>
public static IEnumerable<long> GetPrimes()
{
yield return 2;

var knownPrimes = new List<long> { 2 };

long current = 3;

while (true)
{
var isPrime = knownPrimes.TakeWhile(prime => prime * prime <= current).All(prime => current % prime != 0);

if (isPrime)
{
yield return current;
knownPrimes.Add(current);
}

checked
{
current += 2;
}
}
}

/// <summary>
/// Extension method to take the first n elements from a sequence lazily.
/// </summary>
/// <typeparam name="T">Type of elements in the sequence.</typeparam>
/// <param name="sequence">The source sequence.</param>
/// <param name="count">Number of elements to take.</param>
/// <returns>An IEnumerable with the first n elements.</returns>
public static IEnumerable<T> Take<T>(this IEnumerable<T> sequence, int count)
{
ArgumentNullException.ThrowIfNull(sequence);

if (count <= 0)
{
yield break;
}

var taken = 0;
foreach (var item in sequence)
{
yield return item;
taken++;
if (taken >= count)
{
yield break;
}
}
}

/// <summary>
/// Extension method to skip the first n elements of a sequence lazily.
/// </summary>
/// <typeparam name="T">Type of elements in the sequence.</typeparam>
/// <param name="sequence">The source sequence.</param>
/// <param name="count">Number of elements to skip.</param>
/// <returns>An IEnumerable without the first n elements.</returns>
public static IEnumerable<T> Skip<T>(this IEnumerable<T> sequence, int count)
{
ArgumentNullException.ThrowIfNull(sequence);

if (count <= 0)
{
if (count == 0)
{
foreach (var item in sequence)
{
yield return item;
}
}

yield break;
}

var skipped = 0;
foreach (var item in sequence)
{
skipped++;
if (skipped > count)
{
yield return item;
}
}
}
}
21 changes: 21 additions & 0 deletions HW5/MyLinq/MyLinq.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions HW5/MyLinq/stylecop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
"settings": {
"documentationRules": {
"companyName": "khusainovilas",
"copyrightText": "Copyright (c) {companyName}. All rights reserved."
}
}
}