Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace MathNet.Numerics
- {
- public static class Zipper
- {
- public static IEnumerable<T> Zip<A, B, T>(
- this IEnumerable<A> seqA, IEnumerable<B> seqB, Func<A, B, T> func)
- {
- if (seqA == null) throw new ArgumentNullException("seqA");
- if (seqB == null) throw new ArgumentNullException("seqB");
- return Zip35Deferred(seqA, seqB, func);
- }
- private static IEnumerable<T> Zip35Deferred<A, B, T>(
- this IEnumerable<A> seqA, IEnumerable<B> seqB, Func<A, B, T> func)
- {
- using (var iteratorA = seqA.GetEnumerator())
- using (var iteratorB = seqB.GetEnumerator())
- {
- while (iteratorA.MoveNext() && iteratorB.MoveNext())
- {
- yield return func(iteratorA.Current, iteratorB.Current);
- }
- }
- }
- }
- public class Tuple<T1, T2>
- {
- public T1 Item1 { get; set; }
- public T2 Item2 { get; set; }
- public Tuple(T1 Item1, T2 Item2)
- {
- this.Item1 = Item1;
- this.Item2 = Item2;
- }
- }
- public class Tuple<T1, T2, T3>
- {
- public T1 Item1 { get; set; }
- public T2 Item2 { get; set; }
- public T3 Item3 { get; set; }
- public Tuple(T1 Item1, T2 Item2, T3 Item3)
- {
- this.Item1 = Item1;
- this.Item2 = Item2;
- this.Item3 = Item3;
- }
- }
- /// <summary>
- /// Provides support for lazy initialization.
- /// </summary>
- /// <typeparam name="T">Specifies the type of object that is being lazily initialized.</typeparam>
- public sealed class Lazy<T>
- {
- private readonly object padlock = new object();
- private readonly Func<T> createValue;
- private bool isValueCreated;
- private T value;
- /// <summary>
- /// Gets the lazily initialized value of the current Lazy{T} instance.
- /// </summary>
- public T Value
- {
- get
- {
- if (!isValueCreated)
- {
- lock (padlock)
- {
- if (!isValueCreated)
- {
- value = createValue();
- isValueCreated = true;
- }
- }
- }
- return value;
- }
- }
- /// <summary>
- /// Gets a value that indicates whether a value has been created for this Lazy{T} instance.
- /// </summary>
- public bool IsValueCreated
- {
- get
- {
- lock (padlock)
- {
- return isValueCreated;
- }
- }
- }
- /// <summary>
- /// Initializes a new instance of the Lazy{T} class.
- /// </summary>
- /// <param name="createValue">The delegate that produces the value when it is needed.</param>
- public Lazy(Func<T> createValue)
- {
- if (createValue == null) throw new ArgumentNullException("createValue");
- this.createValue = createValue;
- }
- /// <summary>
- /// Creates and returns a string representation of the Lazy{T}.Value.
- /// </summary>
- /// <returns>The string representation of the Lazy{T}.Value property.</returns>
- public override string ToString()
- {
- return Value.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement