-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples.cs
86 lines (76 loc) · 2.37 KB
/
Examples.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace PullStream.Tests;
public static class Examples
{
public static Stream Strings(IEnumerable<string> strings) =>
SequenceStream.FromStrings(strings, Encoding.UTF8, Environment.NewLine);
public static Stream Bytes(IEnumerable<byte[]> chunks) =>
SequenceStream.UsingStream()
.On(chunks)
.Writing(
(stream, bytes) => { stream.Write(bytes, 0, bytes.Length); }
);
public static Stream BinaryContext(IEnumerable<Person> persons) =>
SequenceStream.Using(
stream => new BinaryWriter(stream, Encoding.UTF8)
)
.On(persons)
.Writing(
(binaryWriter, person) =>
{
binaryWriter.Write(person.Name);
binaryWriter.Write(person.Age);
}
);
public static void ItemMetaInformation(IEnumerable<string> names)
{
var enrichedNames = names.AsItems();
foreach (var (index, kind, name) in enrichedNames)
{
if (kind.IsFirst())
{
Console.WriteLine("Names");
}
Console.Write($"{index}: {name}");
if (!kind.IsLast())
{
Console.WriteLine();
}
}
}
public static Stream ItemMetaInformationOnBuilder(IEnumerable<string> names)
{
return SequenceStream.Using(
stream => new StreamWriter(stream, Encoding.UTF8)
)
.On(names)
.AsItems()
.Writing(
(writer, item) =>
{
if (item.Kind.IsFirst())
{
writer.WriteLine("Names");
}
writer.Write($"{item.Index}: {item.Value}");
if (!item.Kind.IsLast())
{
writer.WriteLine();
}
}
);
}
public class Person
{
public Person(string name, int age)
{
Name = name;
Age = age;
}
public string Name { get; }
public int Age { get; }
}
}