<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CallMeMaybe
{
    /// <summary>
    /// This class contains extension methods for helping <see cref="Maybe{T}"/> interact with
    /// other types.
    /// </summary>
    public static class MaybeExtensions
    {
        // TODO: Fail-fast checks on all methods (specifically watch for lambdas)
        // TODO: Comment all methods and types

        /// <summary>
        /// Produces a <see cref="Maybe{T}"/> value that will contain the value corresponding 
        /// to the given <see cref="key"/> in the dictionary if one exists, or which will be
        /// empty otherwise.
        /// </summary>
        /// <typeparam name="TKey">The type of the source dictionary's key.</typeparam>
        /// <typeparam name="TValue">The type of the source dictionary's value.</typeparam>
        /// <param name="dictionary">The dictionary to look in.</param>
        /// <param name="key">The key to search for.</param>
        /// <returns>
        /// A <see cref="Maybe{T}"/> that is empty if the given <paramref name="dictionary"/> 
        /// does not contain the given <paramref name="key"/></returns>, or if the value
        /// with that <see cref="key"/> is null. Otherwise, the <see cref="Maybe{T}"/> will
        /// contain the value with that <see cref="key"/>.
        public static Maybe<TValue> GetMaybe<TKey, TValue>(
            this IDictionary<TKey, TValue> dictionary,
            TKey key)
        {
            TValue value;
            if (dictionary.TryGetValue(key, out value))
            {
                return new Maybe<TValue>(value);
            }
            return new Maybe<TValue>();
        }

        /// <summary>
        /// A LINQ-like flattening method to only select items from non-empty <see cref="Maybe{T}"/>s.
        /// It's easiest to use LINQ Query Expression syntax for this.
        /// </summary>
        /// <example><code>
        /// var luckyNumbers =
        ///      from n in Enumerable.Range(1, 20)
        ///      from s in HowLuckyIs(n)
        ///      where s.Contains("lucky")
        ///      select new {number = n, howLucky = s};
        /// </code></example>
        /// <typeparam name="TSource">The type of item in the original source.</typeparam>
        /// <typeparam name="TCollection">The type of item in the <see cref="Maybe{T}"/> that you're selecting.</typeparam>
        /// <typeparam name="TResult">The type of item to select from the <see cref="Maybe{T}"/></typeparam>
        /// <param name="source">The source collection.</param>
        /// <param name="collectionSelector">A function yielding a <see cref="Maybe{T}"/></param>
        /// <param name="resultSelector">A function which, given a value from the <see cref="Maybe{T}"/>,
        /// will yield the desired result.</param>
        /// <returns>
        /// A result from the <paramref name="resultSelector"/> for every item in the source<commit_msg>Use new C# syntax to inline out parameter.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CallMeMaybe
{
    /// <summary>
    /// This class contains extension methods for helping <see cref="Maybe{T}"/> interact with
    /// other types.
    /// </summary>
    public static class MaybeExtensions
    {
        // TODO: Fail-fast checks on all methods (specifically watch for lambdas)
        // TODO: Comment all methods and types

        /// <summary>
        /// Produces a <see cref="Maybe{T}"/> value that will contain the value corresponding 
        /// to the given <see cref="key"/> in the dictionary if one exists, or which will be
        /// empty otherwise.
        /// </summary>
        /// <typeparam name="TKey">The type of the source dictionary's key.</typeparam>
        /// <typeparam name="TValue">The type of the source dictionary's value.</typeparam>
        /// <param name="dictionary">The dictionary to look in.</param>
        /// <param name="key">The key to search for.</param>
        /// <returns>
        /// A <see cref="Maybe{T}"/> that is empty if the given <paramref name="dictionary"/> 
        /// does not contain the given <paramref name="key"/></returns>, or if the value
        /// with that <see cref="key"/> is null. Otherwise, the <see cref="Maybe{T}"/> will
        /// contain the value with that <see cref="key"/>.
        public static Maybe<TValue> GetMaybe<TKey, TValue>(
            this IDictionary<TKey, TValue> dictionary,
            TKey key)
        {
            if (dictionary.TryGetValue(key, out var value))
            {
                return new Maybe<TValue>(value);
            }
            return new Maybe<TValue>();
        }

        /// <summary>
        /// A LINQ-like flattening method to only select items from non-empty <see cref="Maybe{T}"/>s.
        /// It's easiest to use LINQ Query Expression syntax for this.
        /// </summary>
        /// <example><code>
        /// var luckyNumbers =
        ///      from n in Enumerable.Range(1, 20)
        ///      from s in HowLuckyIs(n)
        ///      where s.Contains("lucky")
        ///      select new {number = n, howLucky = s};
        /// </code></example>
        /// <typeparam name="TSource">The type of item in the original source.</typeparam>
        /// <typeparam name="TCollection">The type of item in the <see cref="Maybe{T}"/> that you're selecting.</typeparam>
        /// <typeparam name="TResult">The type of item to select from the <see cref="Maybe{T}"/></typeparam>
        /// <param name="source">The source collection.</param>
        /// <param name="collectionSelector">A function yielding a <see cref="Maybe{T}"/></param>
        /// <param name="resultSelector">A function which, given a value from the <see cref="Maybe{T}"/>,
        /// will yield the desired result.</param>
        /// <returns>
        /// A result from the <paramref name="resultSelector"/> for every item in the source
{'fraction_non_alphanumeric': 0.14361968306922435, 'fraction_numerical': 0.001000834028356964, 'mean_word_length': 2.1196670135275753, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 109, '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': '6789763', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1565, 'n_words': 569}