-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectGeneration.cs
58 lines (52 loc) · 1.88 KB
/
ObjectGeneration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections.Generic;
using System.Linq;
using LazySequence;
namespace Samples
{
/// <summary>
/// Examples of Object Generation
/// </summary>
public class ObjectGeneration
{
/// <summary>
/// An example to show creation of list of objects.
/// </summary>
public static void GenerateListOfPersons()
{
/*
* Consider a scenario during testing where you need a list of
* objects matching a certain pattern. For example creating a list
* of Persons.
*/
IEnumerable<Person?> infinitePeople = LazySequence<Person?>.Create(
null,
(prev, idx) => (
new Person($"name_{idx}"),
isLastElement: false));
// A list of Person objects with unique `Name` values
// created on demand by our Lazy Sequence.
// [Person(name+1), Person(name_2)...Person(name_10)]
var tenPeople = infinitePeople.Take(10).ToList();
}
/// <summary>
/// An example to show generation of unique names.
/// </summary>
public static void GenerateUniqueNames()
{
/*
* Consider a scenario where you need to generate unique names.
*/
IEnumerable<string> uniqueNames = LazySequence<string>.Create(
string.Empty,
(prev, idx) => ($"name_{idx}", false));
// Skipping the first element since the first element is an emoty string.
IEnumerator<string>? uniqueNameProvider = uniqueNames.Skip(1).GetEnumerator();
if (uniqueNameProvider.TryGetNext(out var name))
{
// `name` here would be unique.
// example: "name_1"
}
}
private record Person(string Name);
}
}