本文整理汇总了C#中System.Zip方法的典型用法代码示例。如果您正苦于以下问题:C# System.Zip方法的具体用法?C# System.Zip怎么用?C# System.Zip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System
的用法示例。
在下文中一共展示了System.Zip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public void Execute()
{
//
// Zip拡張メソッド.
//
// Zip拡張メソッドは、Pythonのzip関数と同じ動きをするものである。
// つまり、2つのシーケンスを同時にループさせることが出来る。
//
// 第二引数には、resultSelectorを指定する必要があり、好きなデータを返す事ができる。
//
// このメソッドは、どちらかのシーケンスが終わるまで処理を続けるという仕様になっているので
// 2つのシーケンスの要素数が異なる場合は、注意が必要である。
//
// つまり、片方のシーケンスが空の場合、このメソッドは一度もループされない。
//
IEnumerable<int> numbers1 = new[] {1, 2, 3, 4, 5};
IEnumerable<int> numbers2 = new[] {6, 7, 8, 9, 0};
var query = numbers1.Zip(numbers2, Tuple.Create);
Output.WriteLine("========= 2つのシーケンスの要素数が同じ場合 ===========");
foreach (var item in query)
{
Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
}
numbers1 = new[] {1, 2, 3};
numbers2 = new[] {6, 7, 8, 9, 0};
query = numbers1.Zip(numbers2, Tuple.Create);
Output.WriteLine("========= 1つ目のシーケンスの要素が2つ目よりも少ない場合 ===========");
foreach (var item in query)
{
Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
}
numbers1 = new[] {1, 2, 3, 4, 5};
numbers2 = new[] {6, 7, 8};
query = numbers1.Zip(numbers2, Tuple.Create);
Output.WriteLine("========= 2つ目のシーケンスの要素が1つ目よりも少ない場合 ===========");
foreach (var item in query)
{
Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
}
numbers1 = Enumerable.Empty<int>();
numbers2 = new[] {6, 7, 8};
query = numbers1.Zip(numbers2, Tuple.Create);
Output.WriteLine("========= どちらかのシーケンスが空の場合 ===========");
foreach (var item in query)
{
Output.WriteLine("FIRST={0}, SECOND={1}", item.Item1, item.Item2);
}
}
示例2: CouldMoveOnFirstAndLastPositionOfThreeSeries
public void CouldMoveOnFirstAndLastPositionOfThreeSeries() {
var sm1 = new SortedMap<int, int>(new Dictionary<int, int>()
{
//{ 1, 1},
{ 2, 2},
//{ 3, 3}
});
var sm2 = new SortedMap<int, int>(new Dictionary<int, int>()
{
{ 1, 2},
{ 2, 4},
{ 3, 6}
});
var sm3 = new SortedMap<int, int>(new Dictionary<int, int>()
{
{ 1, 3},
{ 2, 6},
{ 3, 9}
});
var series = new[] { sm1, sm2, sm3 };
var sum = series.Zip((k, varr) => k * varr.Sum());
var zipNCursor = sum.GetCursor();
var movedFirst = zipNCursor.MoveFirst();
Assert.IsTrue(movedFirst);
Assert.AreEqual((2 + 4 + 6) * 2, zipNCursor.CurrentValue);
var movedLast = zipNCursor.MoveLast();
Assert.IsTrue(movedLast);
Assert.AreEqual((2 + 4 + 6) * 2, zipNCursor.CurrentValue);
}
示例3: Solve
//private static void Main(string[] args) {
// new ProblemB().Solve(Console.In);
//}
private void Solve(TextReader input)
{
var ns = input.ReadLine().Split(' ').ToArray();
var ns2 = new[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
var ns3 = new[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
var count = int.Parse(input.ReadLine());
var vs = new List<string>();
for (int i = 0; i < count; i++) {
vs.Add(input.ReadLine().Trim());
}
var answer = vs.Select(
v => {
var result = v;
foreach (var t in ns.Zip(ns2, Tuple.Create)) {
result = result.Replace(t.Item1, t.Item2);
}
foreach (var t in ns2.Zip(ns3, Tuple.Create)) {
result = result.Replace(t.Item1, t.Item2);
}
return Tuple.Create(v, result);
}).Select(t => Tuple.Create(t.Item1, int.Parse(t.Item2))).OrderBy(t => t.Item2);
foreach (var t in answer) {
Console.WriteLine(t.Item1);
}
}
示例4: AdjacentElements
public void AdjacentElements()
{
IEnumerableWithCount<string> elements = new[] { "a", "b", "c", "d", "e" }.AsEnumerableWithCount();
IEnumerableWithCount<string> query = elements.Zip(elements.Skip(1), (x, y) => x + y);
Assert.AreEqual(4, query.Count);
query.AssertSequenceEqual("ab", "bc", "cd", "de");
}
示例5: SecondIsInfinite
public void SecondIsInfinite()
{
var arr1 = new[] { 1, 2 };
var result = arr1.Zip(Infinite(), Tuple.Create);
CollectionAssert.AreEqual(new[] { Tuple.Create(1, 42), Tuple.Create(2, 42) }, result);
}
示例6: Zip_two_equal_collections
public void Zip_two_equal_collections()
{
var ints = new[] { 1, 2, 3 };
var strings = new[] { "a", "b", "c" };
var zipped = ints.Zip(strings, (i, s) => i + s);
Assert.That(zipped, Is.EqualTo(new[] { "1a", "2b", "3c" }));
}
示例7: Main
public static void Main () {
var ints1 = new[] { 1, 2, 3, 4 };
var ints2 = new List<int> { 5, 6, 7 };
foreach (var prod in ints1.Zip(ints2, (l, r) => l * r ))
Console.WriteLine(prod);
}
示例8: EqualLengthSequences
public void EqualLengthSequences()
{
IEnumerableWithCount<string> first = new[] { "a", "b", "c" }.AsEnumerableWithCount();
IEnumerableWithCount<int> second = EnumerableWithCount.Range(5, 3);
Func<string, int, string> resultSelector = (x, y) => x + ":" + y;
IEnumerableWithCount<string> query = first.Zip(second, resultSelector);
query.AssertSequenceEqual("a:5", "b:6", "c:7");
}
示例9: EqualSizeArrays
public void EqualSizeArrays()
{
var arr1 = new[] { 1 };
var arr2 = new[] { 2 };
var result = arr1.Zip(arr2, Tuple.Create);
Assert.AreEqual(new[] { Tuple.Create(1, 2) }, result);
}
示例10: SecondIsEmpty
public void SecondIsEmpty()
{
var arr1 = new[] { 1, 2 };
var arr2 = new int[0];
var result = arr1.Zip(arr2, Tuple.Create);
CollectionAssert.IsEmpty(result);
}
示例11: ShouldZipEvenNumberedCollection
public void ShouldZipEvenNumberedCollection()
{
var data = new[] {1L, 2L, 3L, 4L};
var results = new[] {3L, 7L};
Func<long,long,long> map = (a, b) => a + b;
var zipped = data.Zip(map);
Assert.True(results.SequenceEqual(zipped));
}
示例12: GetLines
public static string[] GetLines(this string @this)
{
int[] linesIndex = @this.IndexOfAll("\n");
IEnumerable<int> linesStartIndex = new [] { 0 }.Concat(linesIndex.Select(index => index + 1));
IEnumerable<int> linesEndIndex = linesIndex.Concat(new[] { @this.Length });
return linesStartIndex.Zip(linesEndIndex, (lineStartIndex, lineEndIndex) => @this.Substring(lineStartIndex, lineEndIndex - lineStartIndex).TrimEnd('\r')).ToArray();
}
示例13: SplitLinesInBufferTest
public void SplitLinesInBufferTest()
{
string input = "Hello World\nTest\n\nOther Stuff\n\n";
string[] expected = new[] { "Hello World", "Test", "Other Stuff" };
var output = ActivityRecord.splitLinesInBuffer(input).ToArray();
Assert.Equal(expected.Length, output.Length);
expected.Zip(output, (e, a) => new { e, a }).Run(x => Assert.Equal(x.e, x.a));
}
示例14: PhoneNumberTest
public void PhoneNumberTest()
{
var target = createFixture();
var tests = new[] { "555.555.1234", null, "Elephant Robot", "333.3333.3333", "444.444.2222" };
var results = new[] { true, false, false, false, true };
foreach(var testcase in tests.Zip(results, (test, result) => new {test, result})) {
target.PhoneNumber = testcase.test;
Assert.AreEqual(testcase.result, target.IsValid(), "Testcase: " + (testcase.test ?? "(null)"));
}
}
示例15: WhenAnyWithDependencyObjectTest
public void WhenAnyWithDependencyObjectTest()
{
var inputs = new[] {"Foo", "Bar", "Baz"};
var fixture = new DepObjFixture();
var outputs = fixture.WhenAnyValue(x => x.TestString).CreateCollection();
inputs.ForEach(x => fixture.TestString = x);
Assert.Null(outputs.First());
Assert.Equal(4, outputs.Count);
Assert.True(inputs.Zip(outputs.Skip(1), (expected, actual) => expected == actual).All(x => x));
}