Skip to content

Instantly share code, notes, and snippets.

@jul
Last active July 6, 2019 19:55
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/955ce7a333529182bfe5a827043f0e1b to your computer and use it in GitHub Desktop.
Save jul/955ce7a333529182bfe5a827043f0e1b to your computer and use it in GitHub Desktop.
sum_vs_rediuce.py
from functools import reduce
class string(str):
def __ladd__(self, other):
other = other or ""
return reduce(str.__add__,("L" , self , "L+" , "R" , other))
def __radd__(self, other):
other = other or ""
return reduce(str.__add__,("L" , self , "R+" , "R" , other))
def __add__(self, other):
other = other or ""
return reduce(str.__add__,("L" , self , "+" , "R" , other))
def __iadd__(self, other):
other = other or ""
return reduce(str.__add__,("L" , self , "i+" , "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