ソースコード:Map(params object[])

静的メソッド Map が、組み込み関数 map に相当します。


# IronPython-1.1.2/Src/IronPython/Modules/Builtin.cs
[PythonType("__builtin__")]
public static partial class Builtin {
...
[PythonName("map")]
public static List Map(params object[] param) {
if (param == null || param.Length < 2) {
throw Ops.TypeError("at least 2 arguments required to map");
}
List ret = new List();
object func = param[0];
if (param.Length == 2) {
IEnumerator i = Ops.GetEnumerator(param[1]);
while (i.MoveNext()) {
if (func == null) ret.AddNoLock(i.Current);
else ret.AddNoLock(Ops.Call(func, i.Current));
}
return ret;
} else {
IEnumerator[] enums = new IEnumerator[param.Length - 1];
for (int i = 0; i < enums.Length; i++) {
enums[i] = Ops.GetEnumerator(param[i + 1]);
}

object[] args = new object[enums.Length];
while (true) {
bool done = true;
for (int i = 0; i < enums.Length; i++) {
if (enums[i].MoveNext()) {
args[i] = enums[i].Current;
done = false;
} else {
args[i] = null;
}
}
if (done) {
return ret;
}
if (func != null) {
ret.AddNoLock(Ops.Call(func, args));
} else {
ret.AddNoLock(Tuple.MakeTuple(args));
args = new object[enums.Length]; // Tuple does not copy the array, allocate new one.
}
}
}
}