This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new LINQ method - ReplaceMultiple
- Loading branch information
Krzysztof Dobrzyński
committed
Oct 31, 2017
1 parent
7023b4b
commit 40a7ea7
Showing
11 changed files
with
181 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,25 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace KD.Linq | ||
{ | ||
/// <summary> | ||
/// Class used when replacement is needed. | ||
/// </summary> | ||
public abstract class ReplaceIterator<TValue> : ByIndexIterator<TValue> | ||
{ | ||
/// <summary> | ||
/// New value for which the selected one will be replaced. | ||
/// </summary> | ||
public TValue NewValue { get; } | ||
|
||
public ReplaceIterator(IEnumerable<TValue> source, int index, TValue newValue) : | ||
base(source, index) | ||
{ | ||
this.NewValue = newValue; | ||
} | ||
|
||
public override abstract void OnWantedIndexHit(); | ||
|
||
public override abstract void OnWrongIndexHit(); | ||
} | ||
} |
This file contains 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,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace KD.Linq | ||
{ | ||
internal class ReplaceMultipleIterator<TValue> : ReplaceIterator<TValue> | ||
{ | ||
public Func<TValue, bool> Selector { get; } | ||
|
||
public ReplaceMultipleIterator(IEnumerable<TValue> source, TValue newValue, Func<TValue, bool> selector) : | ||
base(source, -1, newValue) // -1 will prevent from any hit | ||
{ | ||
this.Selector = selector; | ||
} | ||
|
||
/// <summary> | ||
/// This should never be hit. | ||
/// </summary> | ||
public override void OnWantedIndexHit() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
/// <summary> | ||
/// All logic should be here. | ||
/// </summary> | ||
public override void OnWrongIndexHit() | ||
{ | ||
var canReplace = this.Selector(this.Enumerator.Current); | ||
if (this.Selector != null && canReplace) | ||
{ | ||
this.Current = this.NewValue; | ||
} | ||
else | ||
{ | ||
this.Current = this.Enumerator.Current; | ||
} | ||
} | ||
} | ||
} |
This file contains 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,28 @@ | ||
using KD.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Test_ReplaceAt | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var list = new List<int>() { 1, 2, 1, 3, 1, 4 }; | ||
list.ForEach(value => Console.Write(value + ", ")); | ||
|
||
Console.WriteLine(); | ||
|
||
var list2 = list.ReplaceAt(1, 9).ToList(); | ||
list2.ForEach(value => Console.Write(value + ", ")); | ||
|
||
Console.WriteLine(); | ||
|
||
var list3 = list2.ReplaceAt(2, 7).ToList(); | ||
list3.ForEach(value => Console.Write(value + ", ")); | ||
|
||
Console.ReadKey(); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains 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,30 @@ | ||
using KD.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Test_ReplaceMultiple | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var list = new List<int>() { 1, 2, 1, 3, 1, 4 }; | ||
list.ForEach(value => Console.Write(value + ", ")); | ||
|
||
Console.WriteLine(); | ||
|
||
// Replace value at each index where value equals 1 to new value which is 9 | ||
var list2 = list.ReplaceMultiple(9, value => value == 1).ToList(); | ||
list2.ForEach(value => Console.Write(value + ", ")); | ||
|
||
Console.WriteLine(); | ||
|
||
// Replace value at each index where value equals 2 to new value which is 8 | ||
var list3 = list2.ReplaceMultiple(8, value => value == 2).ToList(); | ||
list3.ForEach(value => Console.Write(value + ", ")); | ||
|
||
Console.ReadKey(); | ||
} | ||
} | ||
} |
This file contains 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,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\KD.Linq\KD.Linq.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file was deleted.
Oops, something went wrong.