Last active
July 6, 2019 20:37
-
-
Save jul/b52a9f3ddb3bfa6c398cd866e13c8452 to your computer and use it in GitHub Desktop.
sum vs reduce v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
class string(str): | |
def __radd__(self, other): | |
other = other or "" | |
return reduce(str.__add__,("L" , other , "R+" , "R" , self)) | |
def __add__(self, other): | |
other = other or "" | |
return reduce(str.__add__,("L" , self , "+" , "R" , other)) | |
print(sum(map(lambda s:string(s),range(10)))) | |
print(reduce(string.__add__,map(lambda s:string(s),range(10)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment