Skip to content

Commit 32f9086

Browse files
wetorsbinet
authored andcommitted
py: implement map
1 parent 17dddcd commit 32f9086

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

py/map.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2023 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package py
6+
7+
// A python Map object
8+
type Map struct {
9+
iters Tuple
10+
fun Object
11+
}
12+
13+
var MapType = NewTypeX("filter", `map(func, *iterables) --> map object
14+
15+
Make an iterator that computes the function using arguments from
16+
each of the iterables. Stops when the shortest iterable is exhausted.`,
17+
MapTypeNew, nil)
18+
19+
// Type of this object
20+
func (m *Map) Type() *Type {
21+
return FilterType
22+
}
23+
24+
// MapType
25+
func MapTypeNew(metatype *Type, args Tuple, kwargs StringDict) (res Object, err error) {
26+
numargs := len(args)
27+
if numargs < 2 {
28+
return nil, ExceptionNewf(TypeError, "map() must have at least two arguments.")
29+
}
30+
iters := make(Tuple, numargs-1)
31+
for i := 1; i < numargs; i++ {
32+
iters[i-1], err = Iter(args[i])
33+
if err != nil {
34+
return nil, err
35+
}
36+
}
37+
return &Map{iters: iters, fun: args[0]}, nil
38+
}
39+
40+
func (m *Map) M__iter__() (Object, error) {
41+
return m, nil
42+
}
43+
44+
func (m *Map) M__next__() (Object, error) {
45+
numargs := len(m.iters)
46+
argtuple := make(Tuple, numargs)
47+
48+
for i := 0; i < numargs; i++ {
49+
val, err := Next(m.iters[i])
50+
if err != nil {
51+
return nil, err
52+
}
53+
argtuple[i] = val
54+
}
55+
return Call(m.fun, argtuple, nil)
56+
}
57+
58+
// Check interface is satisfied
59+
var _ I__iter__ = (*Map)(nil)
60+
var _ I__next__ = (*Map)(nil)

py/tests/map.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# test_builtin.py:BuiltinTest.test_map()
2+
from libtest import assertRaises
3+
4+
doc="map"
5+
class Squares:
6+
def __init__(self, max):
7+
self.max = max
8+
self.sofar = []
9+
10+
def __len__(self): return len(self.sofar)
11+
12+
def __getitem__(self, i):
13+
if not 0 <= i < self.max: raise IndexError
14+
n = len(self.sofar)
15+
while n <= i:
16+
self.sofar.append(n*n)
17+
n += 1
18+
return self.sofar[i]
19+
20+
assert list(map(lambda x: x*x, range(1,4))) == [1, 4, 9]
21+
try:
22+
from math import sqrt
23+
except ImportError:
24+
def sqrt(x):
25+
return pow(x, 0.5)
26+
assert list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])) == [[4.0, 2.0], [9.0, 3.0]]
27+
assert list(map(lambda x, y: x+y, [1,3,2], [9,1,4])) == [10, 4, 6]
28+
29+
def plus(*v):
30+
accu = 0
31+
for i in v: accu = accu + i
32+
return accu
33+
assert list(map(plus, [1, 3, 7])) == [1, 3, 7]
34+
assert list(map(plus, [1, 3, 7], [4, 9, 2])) == [1+4, 3+9, 7+2]
35+
assert list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])) == [1+4+1, 3+9+1, 7+2+0]
36+
assert list(map(int, Squares(10))) == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
37+
def Max(a, b):
38+
if a is None:
39+
return b
40+
if b is None:
41+
return a
42+
return max(a, b)
43+
assert list(map(Max, Squares(3), Squares(2))) == [0, 1]
44+
assertRaises(TypeError, map)
45+
assertRaises(TypeError, map, lambda x: x, 42)
46+
class BadSeq:
47+
def __iter__(self):
48+
raise ValueError
49+
yield None
50+
assertRaises(ValueError, list, map(lambda x: x, BadSeq()))
51+
def badfunc(x):
52+
raise RuntimeError
53+
assertRaises(RuntimeError, list, map(badfunc, range(5)))
54+
doc="finished"

stdlib/builtin/builtin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ func init() {
8181
"float": py.FloatType,
8282
"frozenset": py.FrozenSetType,
8383
// "property": py.PropertyType,
84-
"int": py.IntType, // FIXME LongType?
85-
"list": py.ListType,
86-
// "map": py.MapType,
84+
"int": py.IntType, // FIXME LongType?
85+
"list": py.ListType,
86+
"map": py.MapType,
8787
"object": py.ObjectType,
8888
"range": py.RangeType,
8989
// "reversed": py.ReversedType,

0 commit comments

Comments
 (0)