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?
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’s one possibility:
def compose(f,g)
return lambda {|x| f.call(g.call(x))}
end
f = lambda { |s| s.upcase }
g = lambda { |s| "message is: " + s }
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 programming.
Not to discourage the use of Ruby, but is there a reason you don’t just pick
a functional programming language?
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?
Ruby does a fine job at functional programming. You can even do partial
evaluation(aka currying):
class Object
def curry(symbol, *fixedArgs)
lambda { |args| method(symbol).call((fixedArgs + args)) }
end
end
def sum(a, b)
a + b
end
plus4 = curry(:sum, 4)
plus4.call(5) # => 9
arrayOfFive = Array.curry(:new, 5)
arrayOfFive.call(“A”) # => [“A”, “A”, “A”, “A”, “A”]
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
···
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?