/// <summary> /// Joins each element of an outer option to a grouping of inner elements with matching keys, /// then applies a transform to each grouping. /// </summary> /// <typeparam name="TOuter">The type of the elements of <paramref name="outer"/>.</typeparam> /// <typeparam name="TInner">The type of the elements of <paramref name="inner"/>.</typeparam> /// <typeparam name="TKey">The type of the key used to match and group elements.</typeparam> /// <typeparam name="TResult">The type of the result value for each grouping.</typeparam> /// <param name="outer">The outer option to join.</param> /// <param name="inner">The inner option to join.</param> /// <param name="outerKeySelector"> /// A function to select the key that corresponds to an element of <paramref name="outer"/>. /// </param> /// <param name="innerKeySelector"> /// A function to select the key that corresponds to an element of <paramref name="inner"/>. /// </param> /// <param name="resultSelector">A function to select a result value for each grouping.</param> /// <param name="comparer"> /// A comparer to determine whether keys are equal. (If <see langword="null"/>, <see /// cref="EqualityComparer{T}.Default"/> is used.) /// </param> /// <returns> /// An option that contains the result of applying <paramref name="resultSelector"/> to the /// grouping for the element of <paramref name="outer"/> and its corresponding elements of /// <paramref name="inner"/>, if <paramref name="outer"/> is full; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="outer"/>, <paramref name="inner"/>, <paramref name="outerKeySelector"/>, /// <paramref name="innerKeySelector"/>, or <paramref name="resultSelector"/> is <see langword="null"/>. /// </exception> public static IOpt <TResult> GroupJoin <TOuter, TInner, TKey, TResult>(this IOpt <TOuter> outer, IOpt <TInner> inner, Func <TOuter, TKey> outerKeySelector, Func <TInner, TKey> innerKeySelector, Func <TOuter, IOpt <TInner>, TResult> resultSelector, IEqualityComparer <TKey> comparer) { if (outer == null) { throw new ArgumentNullException(nameof(outer)); } if (inner == null) { throw new ArgumentNullException(nameof(inner)); } if (outerKeySelector == null) { throw new ArgumentNullException(nameof(outerKeySelector)); } if (innerKeySelector == null) { throw new ArgumentNullException(nameof(innerKeySelector)); } if (resultSelector == null) { throw new ArgumentNullException(nameof(resultSelector)); } return(outer.SelectRaw(outerValue => { var comparerNonNull = comparer.DefaultIfNull(); var outerKey = outerKeySelector(outerValue); var innerValues = inner.WhereOptRaw(innerValue => comparerNonNull.Equals(outerKey, innerKeySelector(innerValue))); return resultSelector(outerValue, innerValues); })); }
/// <summary> /// Joins elements of an outer option with elements of an inner option having matching keys. /// </summary> /// <typeparam name="TOuter">The type of the elements of <paramref name="outer"/>.</typeparam> /// <typeparam name="TInner">The type of the elements of <paramref name="inner"/>.</typeparam> /// <typeparam name="TKey">The type of the key used to match elements.</typeparam> /// <typeparam name="TResult">The type of the result value for each matched pair.</typeparam> /// <param name="outer">The outer option to join.</param> /// <param name="inner">The inner option to join.</param> /// <param name="outerKeySelector"> /// A function to select the key that corresponds to an element of <paramref name="outer"/>. /// </param> /// <param name="innerKeySelector"> /// A function to select the key that corresponds to an element of <paramref name="inner"/>. /// </param> /// <param name="resultSelector">A function to select a result value for each matched pair.</param> /// <param name="comparer"> /// A comparer to determine whether keys are equal. (If <see langword="null"/>, <see /// cref="EqualityComparer{T}.Default"/> is used.) /// </param> /// <returns> /// An option that contains the result of applying <paramref name="resultSelector"/> to the /// element of <paramref name="outer"/> and the element of <paramref name="inner"/>, if both /// options are full and their corresponding keys are equal according to <paramref /// name="comparer"/>; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="outer"/>, <paramref name="inner"/>, <paramref name="outerKeySelector"/>, /// <paramref name="innerKeySelector"/>, or <paramref name="resultSelector"/> is <see langword="null"/>. /// </exception> public static IOpt <TResult> Join <TOuter, TInner, TKey, TResult>(this IOpt <TOuter> outer, IOpt <TInner> inner, Func <TOuter, TKey> outerKeySelector, Func <TInner, TKey> innerKeySelector, Func <TOuter, TInner, TResult> resultSelector, IEqualityComparer <TKey> comparer) { if (outer == null) { throw new ArgumentNullException(nameof(outer)); } if (inner == null) { throw new ArgumentNullException(nameof(inner)); } if (outerKeySelector == null) { throw new ArgumentNullException(nameof(outerKeySelector)); } if (innerKeySelector == null) { throw new ArgumentNullException(nameof(innerKeySelector)); } if (resultSelector == null) { throw new ArgumentNullException(nameof(resultSelector)); } return(outer .CrossJoinRaw(inner) .WhereRaw(x => comparer.DefaultIfNull().Equals(outerKeySelector(x.first), innerKeySelector(x.second))) .SelectRaw(x => resultSelector(x.first, x.second))); }
/// <summary> /// Produces a new option by combining the value from an option with the value from another /// source using the specified result selector. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TFirst">The type of the elements of <paramref name="first"/>.</typeparam> /// <typeparam name="TSecond">The type of the elements of <paramref name="second"/>.</typeparam> /// <typeparam name="TResult">The type of the elements of the result option.</typeparam> /// <param name="first">The first source.</param> /// <param name="second">The second source.</param> /// <param name="resultSelector"> /// A function that specifies how to combine an element from <paramref name="first"/> with /// the corresponding element of <paramref name="second"/>. /// </param> /// <returns> /// An option containing the result of calling <paramref name="resultSelector"/> on the /// element of <paramref name="first"/> and the first element of <paramref name="second"/>, /// or an empty option if either <paramref name="first"/> or <paramref name="second"/> is empty. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="first"/> or <paramref name="second"/> is <see langword="null"/>. /// </exception> public static IOpt <TResult> Zip <TFirst, TSecond, TResult>(this IOpt <TFirst> first, IEnumerable <TSecond> second, Func <TFirst, TSecond, TResult> resultSelector) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } if (resultSelector == null) { throw new ArgumentNullException(nameof(resultSelector)); } return(first.MetaSelect(opt => { if (opt.HasValue) { foreach (var secondValue in second) { return Opt.Full(resultSelector(opt.ValueOrDefault, secondValue)); } } return Opt.Empty <TResult>(); })); }
/// <summary> /// Applies an accumulator function over the elements of an option. /// </summary> /// <remarks> /// <para> /// Since an option can only ever have zero elements or one element, <paramref name="func"/> /// is never called (but must still be supplied). /// </para> /// </remarks> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">A source option.</param> /// <param name="func">An accumulator function to be applied to an element of <paramref name="source"/>.</param> /// <returns>The final accumulator value, which is the single element of <paramref name="source"/>.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/> or <paramref name="func"/> is <see langword="null"/>. /// </exception> /// <exception cref="InvalidOperationException"><paramref name="source"/> is empty.</exception> public static TSource Aggregate <TSource>(this IOpt <TSource> source, Func <TSource, TSource, TSource> func) { if (func == null) { throw new ArgumentNullException(nameof(func)); } return(source.Single()); }
/// <summary> /// Applies an accumulator function over the elements of an option, using the specified seed /// value, and transforming the final result with the specified result selector. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="source">A source option.</param> /// <param name="seed">The initial value for the accumulator.</param> /// <param name="func"> /// An accumulator function to be applied to the accumulator and an element of <paramref name="source"/>. /// </param> /// <param name="resultSelector"> /// A function to transform the accumulator value into the result. /// </param> /// <returns> /// The result of applying <paramref name="resultSelector"/> to the final accumulator value, /// which is either <paramref name="seed"/>, if <paramref name="source"/> is empty, or the /// result of applying <paramref name="func"/> to <paramref name="seed"/> and the single /// element of <paramref name="source"/>, otherwise. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/>, <paramref name="func"/>, or <paramref name="resultSelector"/> /// is <see langword="null"/>. /// </exception> public static TResult Aggregate <TSource, TAccumulate, TResult>(this IOpt <TSource> source, TAccumulate seed, Func <TAccumulate, TSource, TAccumulate> func, Func <TAccumulate, TResult> resultSelector) { if (resultSelector == null) { throw new ArgumentNullException(nameof(resultSelector)); } return(resultSelector(source.Aggregate(seed, func))); }
/// <summary> /// Returns the remaining contents of an option after skipping a specified number of elements. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// <para> /// If <paramref name="count"/> is zero or negative, no elements are skipped. If <paramref /// name="count"/> is greater than or equal to the number of elements of <paramref name="source"/>, all /// elements are skipped. /// </para> /// </remarks> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">A source option.</param> /// <param name="count">The number of elements to skip.</param> /// <returns> /// <paramref name="source"/>, if <paramref name="count"/> is less than or equal to 0; /// otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static IOpt <TSource> Skip <TSource>(this IOpt <TSource> source, int count) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return((count <= 0) ? source : Opt.Empty <TSource>()); }
private static IOpt <TSource> SimpleNoop <TSource>(IOpt <TSource> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(source); }
/// <summary> /// Converts this option to another option based on its contents. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// <para>Compare <see cref="Match"/>, which uses immediate execution.</para> /// </remarks> /// <typeparam name="TSource">The element type of <paramref name="source"/>.</typeparam> /// <typeparam name="TResult">The element type for the result of this conversion.</typeparam> /// <param name="source">A source option.</param> /// <param name="ifEmpty">A function to produce the result if this option is empty.</param> /// <param name="ifFull"> /// A function to produce the result if this option is full, which accepts the contained /// element as a parameter. /// </param> /// <returns> /// An option equivalent to the result of calling <paramref name="ifEmpty"/>, if this option /// is empty; otherwise, an option equivalent to the result of calling <paramref name="ifFull"/>. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException"> /// <paramref name="ifEmpty"/> is <see langword="null"/> and this option is empty, or /// <paramref name="ifFull"/> is <see langword="null"/> and this option is full. /// </exception> public static IOpt <TResult> MatchOpt <TSource, TResult>(this IOpt <TSource> source, Func <Opt <TResult> > ifEmpty = null, Func <TSource, Opt <TResult> > ifFull = null) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(Opt.DeferRaw(() => source.Match(ifEmpty, ifFull))); }
/// <summary> /// Returns the element at a specified index in an option. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An option to return an element from.</param> /// <param name="index">The index of the element to retrieve.</param> /// <returns>The element at the specified position in <paramref name="source"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="source"/> is empty or <paramref name="index"/> is nonzero. /// </exception> public static TSource ElementAt <TSource>(this IOpt <TSource> source, int index) { if (index != 0) { throw new ArgumentOutOfRangeException(nameof(index)); } return(source.ToFixed().ElementAt(index)); }
/// <summary> /// Filters the elements of an option to those of a specified type. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TResult"> /// The type to which to filter the elements of <paramref name="source"/>. /// </typeparam> /// <param name="source">A source option.</param> /// <returns> /// An option having the same contents as <paramref name="source"/>, if it contains an /// element that is of type <typeparamref name="TResult"/> (according to the <see /// langword="is"/> operator); otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static IOpt <TResult> OfType <TResult>(this IOpt source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(Opt.DeferRaw(() => OfType <object, TResult>(source.ToFixed()))); }
/// <summary> /// Filters the contents of an option to include only non- <see langword="null"/> values, /// converting the results to their non-nullable type. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TSource"> /// The non-nullable basis type of the type of the elements of <paramref name="source"/>. /// </typeparam> /// <param name="source">An option to filter.</param> /// <returns> /// An option containing the same value as source, if it is full and contains a non- <see /// langword="null"/> value; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static IOpt <TSource> WhereNotNullSelectValue <TSource>(this IOpt <TSource?> source) where TSource : struct { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(source.MetaSelect(opt => opt.WhereNotNullSelectValueRaw())); }
/// <summary> /// Filters the contents of an option to include only non- <see langword="null"/> values. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An option to filter.</param> /// <returns> /// An option equivalent to <paramref name="source"/>, if it is full and contains a non- <see /// langword="null"/> value; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static IOpt <TSource> WhereNotNull <TSource>(this IOpt <TSource> source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(source.MetaSelect(opt => opt.WhereNotNullRaw())); }
/// <summary> /// Collapses an option containing another option into a single option. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TSource">The element type of the inner option.</typeparam> /// <param name="source">An outer option containing an inner option.</param> /// <returns> /// An option equivalent to the inner option, if it exists; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static IOpt <TSource> Flatten <TSource>(this IOpt <Opt <TSource> > source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(source.MetaSelect(FlattenFixedOpt)); }
/// <summary> /// Gets the value contained by an option, if it exists. /// </summary> /// <typeparam name="TSource">The element type of source.</typeparam> /// <param name="source">An option whose value to get.</param> /// <param name="value"> /// When this method returns, is set to the value contained by <paramref name="source"/>, if /// any, or the default value of <typeparamref name="TSource"/>, otherwise. /// </param> /// <returns><see langword="true"/>, if source contains a value; otherwise, <see langword="false"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static bool TryGetValue <TSource>(this IOpt <TSource> source, out TSource value) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(source.ToFixed().TryGetValue(out value)); }
/// <summary> /// Creates a new list having the same contents as an option. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">A source option.</param> /// <returns> /// A new single-element list containing the element of <paramref name="source"/>, if it is /// not empty; otherwise, a new empty list. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static List <TSource> ToList <TSource>(this IOpt <TSource> source) { var opt = source.ToFixed(); return(opt.HasValue ? new List <TSource>(1) { opt.ValueOrDefault } : new List <TSource>(0)); }
/// <summary> /// Returns whether any element of an option satisfies a predicate. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">A source option.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <returns> /// <see langword="true"/>, if <paramref name="source"/> contains an element that satisfies /// <paramref name="predicate"/>; otherwise, <see langword="false"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>. /// </exception> public static bool Any <TSource>(this IOpt <TSource> source, Func <TSource, bool> predicate) { if (predicate == null) { throw new ArgumentNullException(nameof(predicate)); } var opt = source.ToFixed(); return(opt.HasValue ? predicate(opt.ValueOrDefault) : false); }
/// <summary> /// Applies an accumulator function over the elements of an option, using the specified seed value. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam> /// <param name="source">A source option.</param> /// <param name="seed">The initial value for the accumulator.</param> /// <param name="func"> /// An accumulator function to be applied to the accumulator and an element of <paramref name="source"/>. /// </param> /// <returns> /// The final accumulator value, which is either <paramref name="seed"/>, if <paramref /// name="source"/> is empty, or the result of applying <paramref name="func"/> to <paramref /// name="seed"/> and the single element of <paramref name="source"/>, otherwise. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/> or <paramref name="func"/> is <see langword="null"/>. /// </exception> public static TAccumulate Aggregate <TSource, TAccumulate>(this IOpt <TSource> source, TAccumulate seed, Func <TAccumulate, TSource, TAccumulate> func) { if (func == null) { throw new ArgumentNullException(nameof(func)); } var opt = source.ToFixed(); return(opt.HasValue ? func(seed, opt.ValueOrDefault) : seed); }
/// <summary> /// Finds the elements of an option that do not appear in a sequence, using the specified /// comparer to determine equality. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TSource"> /// The type of the elements of <paramref name="first"/> and <paramref name="second"/>. /// </typeparam> /// <param name="first">An option whose elements are to be subtracted from.</param> /// <param name="second">A sequence of elements to subtract from <paramref name="first"/>.</param> /// <param name="comparer"> /// A comparer to determine whether elements are equal. (If <see langword="null"/>, <see /// cref="EqualityComparer{T}.Default"/> is used.) /// </param> /// <returns> /// An option equivalent to <paramref name="first"/>, if the element of <paramref /// name="first"/> does not appear in <paramref name="second"/>; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="first"/> or <paramref name="second"/> is <see langword="null"/>. /// </exception> public static IOpt <TSource> Except <TSource>(this IOpt <TSource> first, IEnumerable <TSource> second, IEqualityComparer <TSource> comparer) { if (first == null) { throw new ArgumentNullException(nameof(first)); } if (second == null) { throw new ArgumentNullException(nameof(second)); } return(first.MetaSelect(opt => opt.WhereNotRaw(value => second.Contains(value, comparer)))); }
/// <summary> /// Filters the elements of an option to those that satisfy a predicate. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">A source option.</param> /// <param name="predicate"> /// A function to test an element of <paramref name="source"/> and its index. /// </param> /// <returns> /// An option containing the same element as <paramref name="source"/>, if source is full and /// its element and index satisfy <paramref name="predicate"/>; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>. /// </exception> public static IOpt <TSource> Where <TSource>(this IOpt <TSource> source, Func <TSource, int, bool> predicate) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (predicate == null) { throw new ArgumentNullException(nameof(predicate)); } return(source.MetaSelect(opt => opt.WhereRaw(predicate))); }
/// <summary> /// Projects each element of an option to a new option and flattens the result. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <typeparam name="TResult"> /// The type of the elements of the option returned by <paramref name="selector"/>. /// </typeparam> /// <param name="source">A source option.</param> /// <param name="selector">A transform function applied to each element of <paramref name="source"/>.</param> /// <returns> /// A full option equivalent to the result of applying <paramref name="selector"/> to the /// element of <paramref name="source"/>, if <paramref name="source"/> is full; otherwise, an /// empty option. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>. /// </exception> public static IOpt <TResult> SelectMany <TSource, TResult>(this IOpt <TSource> source, Func <TSource, IOpt <TResult> > selector) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (selector == null) { throw new ArgumentNullException(nameof(selector)); } return(source.MetaSelect(opt => opt.SelectManyRaw(selector))); }
private static IOpt <TSource> OrderByNoop <TSource, TKey>(this IOpt <TSource> source, Func <TSource, TKey> keySelector) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (keySelector == null) { throw new ArgumentNullException(nameof(keySelector)); } return(source); }
/// <summary> /// Returns the element at a specified index in an option or the type's default value if the /// index is out of range. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <param name="source">An option to return an element from.</param> /// <param name="index">The index of the element to retrieve.</param> /// <returns> /// The default value of <typeparamref name="TSource"/> if the index is outside the bounds of /// <paramref name="source"/>; otherwise, the element at the specified position in <paramref name="source"/>. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> public static TSource ElementAtOrDefault <TSource>(this IOpt <TSource> source, int index) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (index == 0) { var opt = source.ToFixed(); return(opt.ValueOrDefault); } return(default);
public InlineTableNode( Token lcurly, IOpt <InlineTableItemNode> first, Token rcurly, IEnumerable <Comment> preComments, Comment appComment) { this.LCurly = new TerminalNode(lcurly).Req(); this.RCurly = new TerminalNode(rcurly).Req(); this.PreComments = preComments; this.AppComment = appComment; this.First = first; }
/// <summary> /// Casts the elements of an option to the specified type. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// <para> /// If <paramref name="source"/> is already an <see cref="IOpt{T}"/> with <typeparamref /// name="TResult"/> as its element type, it is returned unchanged. /// </para> /// </remarks> /// <typeparam name="TResult">The type to which to cast the elements of <paramref name="source"/>.</typeparam> /// <param name="source">A source option.</param> /// <returns> /// An option having the same contents as <paramref name="source"/>, cast to <typeparamref name="TResult"/>. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidCastException"> /// <paramref name="source"/> is not empty and its element cannot be cast to <typeparamref /// name="TResult"/>. (Deferred.) /// </exception> public static IOpt <TResult> Cast <TResult>(this IOpt source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (source is IOpt <TResult> alreadyCorrectSource) { return(alreadyCorrectSource); } return(Opt.DeferRaw(() => Cast <object, TResult>(source.ToFixed()))); }
private ArrayNode( IReq <TerminalNode> lbrac, IOpt <ArrayItemNode> item, IReq <TerminalNode> rbrac, IEnumerable <Comment> preComments, Comment appComment) { this.LBrac = lbrac; this.RBrac = rbrac; this.Item = item; this.PreComments = preComments; this.AppComment = appComment; }
private void MiRound_Click(object sender, EventArgs e) { LlText.Text = "圆角"; GbBody.Text = "圆角"; if (_Round == null) { _Round = new OptRound(); _Round.Init(); _Round.Dock = DockStyle.Fill; } GbBody.Controls.Clear(); GbBody.Controls.Add(_Round); _IOpt = _Round; }
private static bool PlainOptEqualsObjectRaw(IOpt a, object b) { if (b is ISingleResultOpt sb) { return(PlainOptEqualsSingleResultOpt(a, sb)); } else if (b is IOpt pb) { return(PlainOptEqualsPlainOpt(a, pb)); } else { return(false); } }
/// <summary> /// Implements equality for an IOpt that is not also an ISingleResultOpt. /// </summary> internal static bool PlainOptEqualsObject(IOpt _this, object obj) { if (_this == obj) { return(true); } else if (_this == null || obj == null) { return(false); } else { return(PlainOptEqualsObjectRaw(_this, obj)); } }
/// <summary> /// Projects each element of an option to a new option and flattens the result, then applies /// a transform to the result elements. /// </summary> /// <remarks> /// <para> /// This method is implemented using deferred execution; the query represented by this method /// is not performed until the contents of the returned option are resolved, such as by enumeration. /// </para> /// </remarks> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <typeparam name="TCollection"> /// The type of the elements of the intermediate option produced by applying <paramref /// name="collectionSelector"/> to an element of <paramref name="source"/> and its index. /// </typeparam> /// <typeparam name="TResult">The type of the elements of the result option.</typeparam> /// <param name="source">A source option.</param> /// <param name="collectionSelector"> /// A transform function applied to each element of <paramref name="source"/>. /// </param> /// <param name="resultSelector"> /// A transform function applied to each element of each intermediate option returned by /// <paramref name="collectionSelector"/> and its corresponding element from <paramref name="source"/>. /// </param> /// <returns> /// A full option containing the result of applying <paramref name="resultSelector"/> to the /// element of <paramref name="source"/> and the element of the intermediate option produced /// by applying <paramref name="collectionSelector"/> to the element of <paramref /// name="source"/> and its index, if <paramref name="source"/> is full and the intermediate /// option is also full; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/>, <paramref name="collectionSelector"/>, or <paramref /// name="resultSelector"/> is <see langword="null"/>. /// </exception> public static IOpt <TResult> SelectMany <TSource, TCollection, TResult>(this IOpt <TSource> source, Func <TSource, int, IOpt <TCollection> > collectionSelector, Func <TSource, TCollection, TResult> resultSelector) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (collectionSelector == null) { throw new ArgumentNullException(nameof(collectionSelector)); } if (resultSelector == null) { throw new ArgumentNullException(nameof(resultSelector)); } return(source.MetaSelect(opt => SelectManyRaw(opt, collectionSelector, resultSelector))); }
/// <summary> /// Returns the cross join of two options; if both options are full, the result is full and /// contains the combined option values; if either option is empty, the result is empty. /// </summary> private static IOpt <TResult> CrossJoinRaw <TFirst, TSecond, TResult>(this IOpt <TFirst> first, IOpt <TSecond> second, Func <TFirst, TSecond, TResult> resultSelector) { return(Opt.DeferRaw(() => { var firstOpt = Opt.Fix(first); if (firstOpt.HasValue) { var secondOpt = Opt.Fix(second); if (secondOpt.HasValue) { return Opt.Full(resultSelector(firstOpt.ValueOrDefault, secondOpt.ValueOrDefault)); } } return Opt.Empty <TResult>(); })); }
/// <summary> /// Groups the elements of an option by a selected key, then applies a transform to each grouping. /// </summary> /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam> /// <typeparam name="TKey">The type of the key used to group elements.</typeparam> /// <typeparam name="TResult">The type of the result value for each grouping.</typeparam> /// <param name="source">A source option.</param> /// <param name="keySelector"> /// A function to select the key that corresponds to an element of <paramref name="source"/>. /// </param> /// <param name="resultSelector">A function to select a result value for each grouping.</param> /// <param name="comparer"> /// A comparer to determine whether keys are equal. (This parameter is ignored.) /// </param> /// <returns> /// An option that contains the result of applying <paramref name="resultSelector"/> to the /// grouping for the element of <paramref name="source"/>, if <paramref name="source"/> is /// full; otherwise, an empty option. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/>, <paramref name="keySelector"/>, or <paramref /// name="resultSelector"/> is <see langword="null"/>. /// </exception> public static IOpt <TResult> GroupBy <TSource, TKey, TResult>(this IOpt <TSource> source, Func <TSource, TKey> keySelector, Func <TKey, IOpt <TSource>, TResult> resultSelector, IEqualityComparer <TKey> comparer) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (keySelector == null) { throw new ArgumentNullException(nameof(keySelector)); } if (resultSelector == null) { throw new ArgumentNullException(nameof(resultSelector)); } return(source.Select(value => resultSelector(keySelector(value), Opt.Full(value)))); }
private void MiScale_Click(object sender, EventArgs e) { LlText.Text = "缩放"; GbBody.Text = "缩放"; if (_Scale == null) { _Scale = new OptScale(); _Scale.Init(); _Scale.Dock = DockStyle.Fill; } GbBody.Controls.Clear(); GbBody.Controls.Add(_Scale); _IOpt = _Scale; }
private void ShowOpt(string key) { key = key.ToUpper(); if (_IOpt != null) { if (_IOpt.Name == key) { return; } _IOpt.HideView(GbOpt); } _IOpt = GetOpt(key); if (_IOpt == null) { return; } _IOpt.Name = key; _IOpt.InitView(GbOpt); GbOpt.Text = _IOpt.Text; UcUserSet.Enabled = !string.IsNullOrEmpty(key); }
private void MIWater_Click(object sender, EventArgs e) { LlText.Text = "水印"; GbBody.Text = "水印"; if (_Water == null) { _Water = new OptWater(); _Water.Init(); _Water.Dock = DockStyle.Fill; } GbBody.Controls.Clear(); GbBody.Controls.Add(_Water); _IOpt = _Water; }