-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
CustomSequence-Sample-1.cs
36 lines (32 loc) · 1.19 KB
/
CustomSequence-Sample-1.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace CustomSequence
{
public static class CustomSequenceSample1
{
private static IEnumerable<T> Combine<T>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, T, T> func)
{
using (IEnumerator<T> e1 = first.GetEnumerator(),
e2 = second.GetEnumerator())
{
while (e1.MoveNext() && e2.MoveNext())
{
yield return func(e1.Current, e2.Current);
}
}
}
// This sample calculates the dot product of two integer vectors. It uses a user-created sequence operator,
// Combine, to calculate the dot product, passing it a lambda function to multiply two arrays, element by element,
// and sum the result.
// Output:
// Dot product: 109
public static void Example()
{
int[] vectorA = { 0, 2, 4, 5, 6 };
int[] vectorB = { 1, 3, 5, 7, 8 };
int dotProduct = vectorA.Combine(vectorB, (a, b) => a * b).Sum();
Console.WriteLine("Dot product: {0}", dotProduct);
}
}
}