Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
ebf3e7ee
Commit
ebf3e7ee
authored
Feb 27, 2015
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement vararg map()
parent
f58caf8c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
9 deletions
+74
-9
src/codegen/ast_interpreter.cpp
src/codegen/ast_interpreter.cpp
+3
-5
src/runtime/builtin_modules/builtins.cpp
src/runtime/builtin_modules/builtins.cpp
+50
-1
src/runtime/objmodel.h
src/runtime/objmodel.h
+8
-0
test/tests/map.py
test/tests/map.py
+13
-3
No files found.
src/codegen/ast_interpreter.cpp
View file @
ebf3e7ee
...
@@ -450,11 +450,9 @@ Value ASTInterpreter::visit_jump(AST_Jump* node) {
...
@@ -450,11 +450,9 @@ Value ASTInterpreter::visit_jump(AST_Jump* node) {
}
}
CompiledFunction
*
partial_func
=
compilePartialFuncInternal
(
&
exit
);
CompiledFunction
*
partial_func
=
compilePartialFuncInternal
(
&
exit
);
Box
*
arg1
=
arg_array
.
size
()
>=
1
?
arg_array
[
0
]
:
0
;
auto
arg_tuple
=
getTupleFromArgsArray
(
&
arg_array
[
0
],
arg_array
.
size
());
Box
*
arg2
=
arg_array
.
size
()
>=
2
?
arg_array
[
1
]
:
0
;
return
partial_func
->
call
(
std
::
get
<
0
>
(
arg_tuple
),
std
::
get
<
1
>
(
arg_tuple
),
std
::
get
<
2
>
(
arg_tuple
),
Box
*
arg3
=
arg_array
.
size
()
>=
3
?
arg_array
[
2
]
:
0
;
std
::
get
<
3
>
(
arg_tuple
));
Box
**
args
=
arg_array
.
size
()
>=
4
?
&
arg_array
[
3
]
:
0
;
return
partial_func
->
call
(
arg1
,
arg2
,
arg3
,
args
);
}
}
}
}
...
...
src/runtime/builtin_modules/builtins.cpp
View file @
ebf3e7ee
...
@@ -559,6 +559,54 @@ Box* map2(Box* f, Box* container) {
...
@@ -559,6 +559,54 @@ Box* map2(Box* f, Box* container) {
return
rtn
;
return
rtn
;
}
}
Box
*
map
(
Box
*
f
,
BoxedTuple
*
args
)
{
assert
(
args
->
cls
==
tuple_cls
);
auto
num_iterable
=
args
->
elts
.
size
();
if
(
num_iterable
<
1
)
raiseExcHelper
(
TypeError
,
"map() requires at least two args"
);
// performance optimization for the case where we only have one iterable
if
(
num_iterable
==
1
)
return
map2
(
f
,
args
->
elts
[
0
]);
std
::
vector
<
BoxIterator
>
args_it
;
std
::
vector
<
BoxIterator
>
args_end
;
for
(
auto
&
e
:
args
->
elts
)
{
auto
range
=
e
->
pyElements
();
args_it
.
emplace_back
(
range
.
begin
());
args_end
.
emplace_back
(
range
.
end
());
}
assert
(
args_it
.
size
()
==
num_iterable
);
assert
(
args_end
.
size
()
==
num_iterable
);
Box
*
rtn
=
new
BoxedList
();
std
::
vector
<
Box
*>
current_val
(
num_iterable
);
while
(
true
)
{
int
num_done
=
0
;
for
(
int
i
=
0
;
i
<
num_iterable
;
++
i
)
{
if
(
args_it
[
i
]
==
args_end
[
i
])
{
++
num_done
;
current_val
[
i
]
=
None
;
}
else
{
current_val
[
i
]
=
*
args_it
[
i
];
}
}
if
(
num_done
==
num_iterable
)
break
;
auto
v
=
getTupleFromArgsArray
(
&
current_val
[
0
],
num_iterable
);
listAppendInternal
(
rtn
,
runtimeCall
(
f
,
ArgPassSpec
(
num_iterable
),
std
::
get
<
0
>
(
v
),
std
::
get
<
1
>
(
v
),
std
::
get
<
2
>
(
v
),
std
::
get
<
3
>
(
v
),
NULL
));
for
(
int
i
=
0
;
i
<
num_iterable
;
++
i
)
{
if
(
args_it
[
i
]
!=
args_end
[
i
])
++
args_it
[
i
];
}
}
return
rtn
;
}
Box
*
reduce
(
Box
*
f
,
Box
*
container
,
Box
*
initial
)
{
Box
*
reduce
(
Box
*
f
,
Box
*
container
,
Box
*
initial
)
{
Box
*
current
=
initial
;
Box
*
current
=
initial
;
...
@@ -1042,7 +1090,8 @@ void setupBuiltins() {
...
@@ -1042,7 +1090,8 @@ void setupBuiltins() {
builtins_module
->
giveAttr
(
"execfile"
,
builtins_module
->
giveAttr
(
"execfile"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
execfile
,
UNKNOWN
,
1
),
"execfile"
));
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
execfile
,
UNKNOWN
,
1
),
"execfile"
));
builtins_module
->
giveAttr
(
"map"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
map2
,
LIST
,
2
),
"map"
));
builtins_module
->
giveAttr
(
"map"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
map
,
LIST
,
1
,
0
,
true
,
false
),
"map"
));
builtins_module
->
giveAttr
(
builtins_module
->
giveAttr
(
"reduce"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
reduce
,
UNKNOWN
,
3
,
1
,
false
,
false
),
"reduce"
,
"reduce"
,
new
BoxedBuiltinFunctionOrMethod
(
boxRTFunction
((
void
*
)
reduce
,
UNKNOWN
,
3
,
1
,
false
,
false
),
"reduce"
,
{
NULL
}));
{
NULL
}));
...
...
src/runtime/objmodel.h
View file @
ebf3e7ee
...
@@ -148,5 +148,13 @@ static const char* objectNewParameterTypeErrorMsg() {
...
@@ -148,5 +148,13 @@ static const char* objectNewParameterTypeErrorMsg() {
}
}
bool
exceptionMatches
(
const
ExcInfo
&
e
,
BoxedClass
*
cls
);
bool
exceptionMatches
(
const
ExcInfo
&
e
,
BoxedClass
*
cls
);
inline
std
::
tuple
<
Box
*
,
Box
*
,
Box
*
,
Box
**>
getTupleFromArgsArray
(
Box
**
args
,
int
num_args
)
{
Box
*
arg1
=
num_args
>=
1
?
args
[
0
]
:
nullptr
;
Box
*
arg2
=
num_args
>=
2
?
args
[
1
]
:
nullptr
;
Box
*
arg3
=
num_args
>=
3
?
args
[
2
]
:
nullptr
;
Box
**
argtuple
=
num_args
>=
4
?
&
args
[
3
]
:
nullptr
;
return
std
::
make_tuple
(
arg1
,
arg2
,
arg3
,
argtuple
);
}
}
}
#endif
#endif
test/tests/map.py
View file @
ebf3e7ee
def
f
(
x
):
def
f
(
*
args
):
print
"f(%s)"
%
x
print
"f("
,
return
-
x
for
a
in
args
:
print
a
,
print
")"
s
=
-
1
try
:
s
=
sum
(
args
)
except
:
pass
return
s
print
map
(
f
,
range
(
10
))
print
map
(
f
,
range
(
10
))
print
map
(
f
,
range
(
9
),
range
(
20
,
31
),
range
(
30
,
40
),
range
(
40
,
50
),
range
(
60
,
70
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment