| For, Each, Mul 函数です |
|---|
|
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; } } |
|
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; } } |
|
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; } } |