forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cs
More file actions
52 lines (42 loc) · 1.74 KB
/
Map.cs
File metadata and controls
52 lines (42 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Linq;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
namespace IronPython.Runtime {
[PythonType("map")]
[Documentation(@"map(func, *iterables) -> map object
Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.")]
public class Map : IEnumerable {
private CodeContext _context;
private object _func;
private object[] _iters;
public Map(CodeContext context, object func, params object[] iters){
_context = context;
_func = func;
_iters = iters;
if(iters.Length == 0) {
throw PythonOps.TypeError("map() must have at least two arguments.");
}
if(!PythonOps.IsCallable(context, func)) {
throw PythonOps.UncallableError(func);
}
foreach(object o in iters) {
IEnumerator e;
if(!PythonOps.TryGetEnumerator(context, o, out e)) {
throw PythonOps.TypeErrorForNotIterable(o);
}
}
}
IEnumerator IEnumerable.GetEnumerator() {
IEnumerator[] enumerators = _iters.Select(x => PythonOps.GetEnumerator(x)).ToArray();
while (enumerators.All(x => x.MoveNext())) {
yield return PythonOps.CallWithContext(_context, _func, enumerators.Select(x => x.Current).ToArray());
}
}
}
}