<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LanguageExt;

namespace SJP.Schematic.Core.Extensions
{
    public static class OptionExtensions
    {
        public static T UnwrapSome<T>(this Option<T> input)
        {
            if (input.IsNone)
                throw new ArgumentException("The given optional object does not have a value.", nameof(input));

            return input.IfNoneUnsafe(default(T)!);
        }

        public static async Task<T> UnwrapSomeAsync<T>(this OptionAsync<T> input)
        {
            var isNone = await input.IsNone.ConfigureAwait(false);
            if (isNone)
                throw new ArgumentException("The given optional object does not have a value.", nameof(input));

            return await input.IfNoneUnsafe(default(T)!).ConfigureAwait(false);
        }

        public static Option<T> FirstSome<T>(this IEnumerable<Option<T>> input)
        {
            if (input == null)
                throw new ArgumentNullException(nameof(input));

            return input.FirstOrDefault(x => x.IsSome);
        }

        public static OptionAsync<T> FirstSome<T>(this IEnumerable<OptionAsync<T>> input, CancellationToken cancellationToken = default)
        {
            if (input == null)
                throw new ArgumentNullException(nameof(input));

            return FirstSomeAsyncCore(input, cancellationToken).ToAsync();
        }

        private static async Task<Option<T>> FirstSomeAsyncCore<T>(IEnumerable<OptionAsync<T>> input, CancellationToken cancellationToken)
        {
            foreach (var option in input)
            {
                if (cancellationToken.IsCancellationRequested)
                    throw new OperationCanceledException(cancellationToken);

                var resolvedOption = await option.ToOption().ConfigureAwait(false);
                if (resolvedOption.IsSome)
                    return resolvedOption;
            }

            return Option<T>.None;
        }
    }
}<commit_msg>Simplify cancellation check.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LanguageExt;

namespace SJP.Schematic.Core.Extensions
{
    public static class OptionExtensions
    {
        public static T UnwrapSome<T>(this Option<T> input)
        {
            if (input.IsNone)
                throw new ArgumentException("The given optional object does not have a value.", nameof(input));

            return input.IfNoneUnsafe(default(T)!);
        }

        public static async Task<T> UnwrapSomeAsync<T>(this OptionAsync<T> input)
        {
            var isNone = await input.IsNone.ConfigureAwait(false);
            if (isNone)
                throw new ArgumentException("The given optional object does not have a value.", nameof(input));

            return await input.IfNoneUnsafe(default(T)!).ConfigureAwait(false);
        }

        public static Option<T> FirstSome<T>(this IEnumerable<Option<T>> input)
        {
            if (input == null)
                throw new ArgumentNullException(nameof(input));

            return input.FirstOrDefault(x => x.IsSome);
        }

        public static OptionAsync<T> FirstSome<T>(this IEnumerable<OptionAsync<T>> input, CancellationToken cancellationToken = default)
        {
            if (input == null)
                throw new ArgumentNullException(nameof(input));

            return FirstSomeAsyncCore(input, cancellationToken).ToAsync();
        }

        private static async Task<Option<T>> FirstSomeAsyncCore<T>(IEnumerable<OptionAsync<T>> input, CancellationToken cancellationToken)
        {
            foreach (var option in input)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var resolvedOption = await option.ToOption().ConfigureAwait(false);
                if (resolvedOption.IsSome)
                    return resolvedOption;
            }

            return Option<T>.None;
        }
    }
}
{'fraction_non_alphanumeric': 0.09020179917335279, 'fraction_numerical': 0.0, 'mean_word_length': 2.3392857142857144, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2270667', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1031, 'n_words': 256}