<commit_before>﻿using System;
using System.Collections.Generic;
using Scripting.SSharp.Runtime.Operators;

namespace Scripting.SSharp
{
  public static class DynamicMath
  {
    static Dictionary<Type, Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>>> converters = new Dictionary<Type, Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>>>();

    public static void AddConverter<TLeft, TRight>(Func<dynamic, dynamic, Tuple<dynamic, dynamic>> function)
    {
      Type leftType = typeof(TLeft);
      Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>> funcs = null;
      if (!converters.TryGetValue(leftType, out funcs))
      {
        funcs = new Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>>();
        converters[leftType] = funcs;
      }

      funcs[typeof(TRight)] = function;
    }

    public static Tuple<dynamic, dynamic> TryConvert(dynamic left, dynamic right)
    {
      Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>> funcs = null;
      if (converters.TryGetValue(left.GetType(), out funcs))
      {
        Func<dynamic, dynamic, Tuple<dynamic, dynamic>> function = null;
        if (funcs.TryGetValue(right.GetType(), out function))
          return function(left, right);
      }

      return null;
    }

    static DynamicMath()
    {
      AddConverter<decimal, double>((left, right) => new Tuple<dynamic, dynamic>((double)left, right));
      AddConverter<double, decimal>((left, right) => new Tuple<dynamic, dynamic>(left, (double)right));
<commit_msg>fixed Dynamic Math code to be mono 2.8 compatible<commit_after>﻿using System;
using System.Collections.Generic;
using Scripting.SSharp.Runtime.Operators;

namespace Scripting.SSharp
{
  public static class DynamicMath
  {
    static Dictionary<Type, Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>>> converters = new Dictionary<Type, Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>>>();

    public static void AddConverter<TLeft, TRight>(Func<dynamic, dynamic, Tuple<dynamic, dynamic>> function)
    {
      Type leftType = typeof(TLeft);
      Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>> funcs = null;
      if (!converters.TryGetValue(leftType, out funcs))
      {
        funcs = new Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>>();
        converters[leftType] = funcs;
      }

      funcs[typeof(TRight)] = function;
    }

    public static Tuple<dynamic, dynamic> TryConvert(dynamic left, dynamic right)
    {
      // <NAME>: the code below crash for mono 2.8 though should work fine, 
      // slightly rewriting to make the code mono-compatible
      /*
      Dictionary<Type, Func<dynamic, dynamic, Tuple<dynamic, dynamic>>> funcs = null;
      if (converters.TryGetValue(left.GetType(), out funcs))
      {
        Func<dynamic, dynamic, Tuple<dynamic, dynamic>> function = null;
        if (funcs.TryGetValue(right.GetType(), out function))
          return function(left, right);
      }
      */

      Type leftType = left.GetType();
      if (converters.ContainsKey(leftType))
      {
        var funcs = converters[leftType];
        Type rightType = right.GetType();

        if (funcs.ContainsKey(rightType))
          return funcs[rightType](left, right);
      }

      return null;
    }

    static DynamicMath()
    {
      AddConverter<decimal, double>((left, right) => new Tuple<dynamic, dynamic>((double)left, right));
      AddConverter<double, decimal>((left, right) => new Tuple<dynamic, dynamic>(left, (double)right));

{'fraction_non_alphanumeric': 0.12611358574610246, 'fraction_numerical': 0.0011135857461024498, 'mean_word_length': 3.672301690507152, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 62, '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': '4592986', 'n_tokens_mistral': 1067, 'n_tokens_neox': 1035, 'n_words': 265}