Index 扉へ 首頁
人類の品種・宇宙人・系外銀河など
For, Each, Mul 函数です
0   商湯      2026年2月5日 木 9:35
Download source files:
http://www.jkofsp.net/files/

using System;
using System.Collections;
using System.Collections.Generic;

public static partial class TakakuG
{
   public delegate T _For_FAdd<T>(T a, T b);
   public delegate T _For_FElem<I, T>(I i);
   public delegate T _For_FAddElem<T, I>(T sum, I i);

   public static T For<T>(int from, int to, _For_FAdd<T> fAdd, _For_FElem<int, T> fElem)
   {
       if (from > to)
           throw new Exception();

       T ret = fElem(from);

       for (int i = from + 1; i <= to; i++)
           ret = fAdd(ret, fElem(i));

       return ret;
   }

   public static T For<T>(int from, int to, T zero, _For_FAdd<T> fAdd, _For_FElem<int, T> fElem)
   {
       if (from > to)
           return zero;

       T ret = fElem(from);

       for (int i = from + 1; i <= to; i++)
           ret = fAdd(ret, fElem(i));

       return ret;
   }

   public static T For<T>(int from, int to, T sum0, _For_FAddElem<T, int> fAddElem)
   {
       T ret = sum0;

       for (int i = from; i <= to; i++)
           ret = fAddElem(ret, i);

       return ret;
   }

   public static T For<I, T>(IEnumerable<I> _in, _For_FAdd<T> fAdd, _For_FElem<I, T> fElem)
   {
       IEnumerator<I> i = _in.GetEnumerator();

       if (!i.MoveNext())
           throw new Exception();

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<I, T>(IEnumerable<I> _in, T zero, _For_FAdd<T> fAdd, _For_FElem<I, T> fElem)
   {
       IEnumerator<I> i = _in.GetEnumerator();

       if (!i.MoveNext())
           return zero;

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<I, T>(IEnumerable<I> _in, T sum0, _For_FAddElem<T, I> fAddElem)
   {
       T ret = sum0;

       foreach (I i in _in)
           ret = fAddElem(ret, i);

       return ret;
   }

   public static T For<T>(IEnumerable I, _For_FAdd<T> fAdd, _For_FElem<object, T> fElem)
   {
       IEnumerator i = I.GetEnumerator();

       if (!i.MoveNext())
           throw new Exception();

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<T>(IEnumerable I, T zero, _For_FAdd<T> fAdd, _For_FElem<object, T> fElem)
   {
       IEnumerator i = I.GetEnumerator();

       if (!i.MoveNext())
           return zero;

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<T>(IEnumerable I, T sum0, _For_FAddElem<T, object> fAddElem)
   {
       T ret = sum0;

       foreach (object i in I)
           ret = fAddElem(ret, i);

       return ret;
   }
}
1   商湯      2026年2月5日 木 9:35
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public static partial class TakakuG
{
   public delegate R _Each_F<T, R>(T x);

   public static R[] Each<T, R>(T[] X, _Each_F<T, R> f)
   {
       R[] Y = new R[X.Length];

       for (int i = 0; i < X.Length; i++)
           Y[i] = f(X[i]);

       return Y;
   }

   public static List<R> Each<T, R>(List<T> X, _Each_F<T, R> f)
   {
       List<R> Y = new List<R>(X.Count);

       foreach (T x in X)
           Y.Add(f(x));

       return Y;
   }

   public static R Each<R>(IEnumerable X, _Each_F<object, object> f)
       where R : IList, new()
   {
       R Y = new R();

       foreach (object x in X)
           Y.Add(f(x));

       return Y;
   }

   public static Dictionary<K, R> Each<K, T, R>(Dictionary<K, T> X, _Each_F<T, R> f)
   {
       Dictionary<K, R> Y = new Dictionary<K, R>(X.Count);

       foreach (KeyValuePair<K, T> x in X)
           Y[x.Key] = f(x.Value);

       return Y;
   }

   public static string Each(string X, _Each_F<char, char> f)
   {
       char[] Y = X.ToCharArray();

       for (int i = 0; i < X.Length; i++)
           Y[i] = f(X[i]);

       return new string(Y);
   }

   public static StringBuilder Each(StringBuilder X, _Each_F<char, char> f)
   {
       StringBuilder Y = new StringBuilder(X.Length);

       for (int i = 0; i < X.Length; i++)
           Y[i] = f(X[i]);

       return Y;
   }
}
2   商湯      2026年2月5日 木 9:36
using System;
using System.Collections.Generic;
using System.Text;

public static partial class TakakuG
{
   public delegate T _Mul_FAdd<T>(T a, T b);

   public static T Mul<T>(T x, int k, _Mul_FAdd<T> fAdd)
   {
       if (k <=0)
           throw new Exception();

       if (k == 1)
           return x;

       T ret = x;

       for (int i = 0; i <= k - 2; i++)
           ret = fAdd(ret, x);

       return ret;
   }
}
名前      編集用パスワード(省略可)    パスワードの記憶
数式 αβγ IPA http:// 画像 B I U 上 下
(添付ファイルは http://www.jkofsp.net/up/ にアップロードしてください)