From: Gavin Sinclair Date: 2003-03-27T08:45:22+09:00 Subject: Re: How can one implement composition of functions in Ruby? On Thursday, March 27, 2003, 12:07:38 AM, sdieselil wrote: > Hi > I'm deliberating about what language to choose: Ruby or Python. One of > the most important issues for me is the ability to write in a > functional style of programing. So far I saw than python supports it > more but may be I'm wrong? > Here comes the question: how can I implement composition of functions > in Ruby,i.e. how can I write a function that takes two other functions > as arguments and returns a function that composes them? > Here is a way to do it in python: > def compose(f,g): > return lambda x: f(g(x)) This is probably more a Python idiom than a Ruby idiom, since in Python, methods (functions) are first-level objects. So, while you can manipulate Ruby lambdas like you can in Python, the code gets messy if you want to pass actual methods around. Ruby's strength over Python is its purity of OO design. Python's strength over Ruby is support for more functional paradigms. I'm sure you'll find a lot to interest you in both languages. Gavin