Skip to content

Instantly share code, notes, and snippets.

@jul
Last active July 6, 2019 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jul/b52a9f3ddb3bfa6c398cd866e13c8452 to your computer and use it in GitHub Desktop.
Save jul/b52a9f3ddb3bfa6c398cd866e13c8452 to your computer and use it in GitHub Desktop.
sum vs reduce v2
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