Operators as functions

Steve Holden steve at holdenweb.com
Mon Dec 20 23:52:39 CET 2004


Anders Andersson wrote:

> Hello
> 
> I want to concatinate (I apologize for bad English, but it is not my 
> native language) a list of strings to a string. I could use (I think):
> 
> s = ""
> map(lambda x: s.append(x), theList)
> 
> But I want to do something like (I think that the code above is clumsy):
> 
> s = reduce("concatinating function", theList, "")
> 
> And here is the questions: What to replace "concatinating function" 
> with? Can I in some way give the +-operator as an argument to the reduce 
> function? I know operators can be sent as arguments in Haskell and since 
> Python has functions as map, filter and listcomprehension etc. I hope it 
> is possible in Python too. In Haskell I would write:
> 
> foldr (++) []
> 
> Thank you for answering!
> 

  >>> l = ["abc", "def", "ghi", "jkl"]
  >>> "".join(l)
'abcdefghijkl'
  >>> ", ".join(l)
'abc, def, ghi, jkl'
  >>>

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list