Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
py: implement map
  • Loading branch information
wetor committed Jun 8, 2023
commit 487fbd9f5878147c7cd5edb10718709dfde014ae
60 changes: 60 additions & 0 deletions py/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package py

// A python Map object
type Map struct {
iters Tuple
fun Object
}

var MapType = NewTypeX("filter", `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.`,
MapTypeNew, nil)

// Type of this object
func (m *Map) Type() *Type {
return FilterType
}

// MapType
func MapTypeNew(metatype *Type, args Tuple, kwargs StringDict) (res Object, err error) {
numargs := len(args)
if numargs < 2 {
return nil, ExceptionNewf(TypeError, "map() must have at least two arguments.")
}
iters := make(Tuple, numargs-1)
for i := 1; i < numargs; i++ {
iters[i-1], err = Iter(args[i])
if err != nil {
return nil, err
}
}
return &Map{iters: iters, fun: args[0]}, nil
}

func (m *Map) M__iter__() (Object, error) {
return m, nil
}

func (m *Map) M__next__() (Object, error) {
numargs := len(m.iters)
argtuple := make(Tuple, numargs)

for i := 0; i < numargs; i++ {
val, err := Next(m.iters[i])
if err != nil {
return nil, err
}
argtuple[i] = val
}
return Call(m.fun, argtuple, nil)
}

// Check interface is satisfied
var _ I__iter__ = (*Map)(nil)
var _ I__next__ = (*Map)(nil)
54 changes: 54 additions & 0 deletions py/tests/map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# test_builtin.py:BuiltinTest.test_map()
from libtest import assertRaises

doc="map"
class Squares:
def __init__(self, max):
self.max = max
self.sofar = []

def __len__(self): return len(self.sofar)

def __getitem__(self, i):
if not 0 <= i < self.max: raise IndexError
n = len(self.sofar)
while n <= i:
self.sofar.append(n*n)
n += 1
return self.sofar[i]

assert list(map(lambda x: x*x, range(1,4))) == [1, 4, 9]
try:
from math import sqrt
except ImportError:
def sqrt(x):
return pow(x, 0.5)
assert list(map(lambda x: list(map(sqrt, x)), [[16, 4], [81, 9]])) == [[4.0, 2.0], [9.0, 3.0]]
assert list(map(lambda x, y: x+y, [1,3,2], [9,1,4])) == [10, 4, 6]

def plus(*v):
accu = 0
for i in v: accu = accu + i
return accu
assert list(map(plus, [1, 3, 7])) == [1, 3, 7]
assert list(map(plus, [1, 3, 7], [4, 9, 2])) == [1+4, 3+9, 7+2]
assert list(map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])) == [1+4+1, 3+9+1, 7+2+0]
assert list(map(int, Squares(10))) == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
def Max(a, b):
if a is None:
return b
if b is None:
return a
return max(a, b)
assert list(map(Max, Squares(3), Squares(2))) == [0, 1]
assertRaises(TypeError, map)
assertRaises(TypeError, map, lambda x: x, 42)
class BadSeq:
def __iter__(self):
raise ValueError
yield None
assertRaises(ValueError, list, map(lambda x: x, BadSeq()))
def badfunc(x):
raise RuntimeError
assertRaises(RuntimeError, list, map(badfunc, range(5)))
doc="finished"
6 changes: 3 additions & 3 deletions stdlib/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func init() {
"float": py.FloatType,
"frozenset": py.FrozenSetType,
// "property": py.PropertyType,
"int": py.IntType, // FIXME LongType?
"list": py.ListType,
// "map": py.MapType,
"int": py.IntType, // FIXME LongType?
"list": py.ListType,
"map": py.MapType,
"object": py.ObjectType,
"range": py.RangeType,
// "reversed": py.ReversedType,
Expand Down