Quantcast

Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Wednesday, November 08, 2006

Functional Programming in Ruby

In Ruby, iteration is done mainly functionally with methods like each and map. We call this type of iteration functional because we are passing a function to be called on each item in the collection.

[1,2,3].each { |item| puts item + 1 }

In this example. the function being passed is { |item| puts item + 1 }. In fact!

print_function = lambda { |item| puts item + 1 }
[1,2,3].each &print_function

Accomplishes the exact same behavior.

This is very much from the functional programing paradigm. Now, one thing that I can not stand to see in Ruby code is this.

def convert(old)
new = Array.new
old.each do |item|
new << transform(item)
end
new
end

The first hint that there is something wrong is the very odd call to return the new variable at the end of the convert method. It seems odd and awkward, and that is a hint that you are working against the grain. Here is how it should look.

def convert(old)
old.map { |item| transform(item) }
end

Or alternatively:

def convert(old)
old.map &method(:transform)
end

Notice how clean and beautiful this is. We are working with the grain of functional programming here. Not only that, but there is less to debug, less to maintain and it takes less time to grok what is going on.

If you want to learn more about the functional programming roots of Ruby, I recommend learning OCaml, I am really enjoying it.

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!

20 Comments:

Anonymous j`ey said...

Although, some of this only works with Symbol#to_proc of course!

11:57 PM, November 08, 2006

 
Anonymous zerohalo said...

Thanks, Lucas - though I've read and coded this sort of thing before, having some of these things spelled out from different perspectives is nice for those of us learning Ruby.

I understand passing a proc to old.map, but I don't get how &method(:transform) works. I'm assuming 'transform' is a custom method defined in the class. Does that mean that &method will call the 'transform' method, automatically passing each element in 'old' to it and returning the value? Is there any advantage of using that over { |item| transform(item) } ?

11:58 AM, November 09, 2006

 
Anonymous Anonymous said...

A real functional language is even better:

convert = map transform

(Haskell)

12:28 PM, November 10, 2006

 
Anonymous Anonymous said...


A real functional language is even better:

convert = map transform

(Haskell)

Damn straight!

2:56 PM, November 10, 2006

 
Anonymous Anonymous said...

As an aside, what OCaml book(s) do you recommend? I almost bought the Practical Ocaml book that just came out, but I'm seeing lots of bad reviews of that book.

Also, how about a PDX OCaml meetup?

3:19 PM, November 10, 2006

 
Anonymous Olle Jonsson said...

Thanks, Lucas, I found some neat stuff in libxml-ruby connected to this while implementing your more idiomatic way of expression in some code I had inherited.

Here is the blog post about it.

8:08 AM, November 12, 2006

 
Anonymous Curt Sampson said...

I don't recommend Practical OCaml; I have it and it's certainly not a good book for learning a functional programming style. I've got a longer review up on lambda-the-ultimate.org, in the item on that book.

In fact, the book put me off OCaml so much that I've switched to developing in Haskell instead. Haskell syntax is much more elegant.

However, if you're new to functional programming, a very good book to start with is The Little Schemer. It will really give you the feel of FP.

8:11 PM, November 30, 2006

 
Anonymous Curt Sampson said...

I don't recommend Practical OCaml; I have it and it's certainly not a good book for learning a functional programming style. I've got a longer review up on lambda-the-ultimate.org, in the item on that book.

In fact, the book put me off OCaml so much that I've switched to developing in Haskell instead. Haskell syntax is much more elegant.

However, if you're new to functional programming, a very good book to start with is The Little Schemer. It will really give you the feel of FP.

8:12 PM, November 30, 2006

 
Anonymous Anonymous said...

i buy hydrocodone at buy hydrocodone - can't find any cheaper

7:02 AM, January 28, 2007

 
Blogger CarlH said...

Why don't you use collect!?

8:58 AM, April 03, 2007

 
Blogger Kerry Buckley said...

CarlH said...
Why don't you use collect!?

collect and map are synonyms, and can be used interchangably.

5:01 AM, July 03, 2007

 
Anonymous Anonymous said...

i downloaded ruby, and i want to learn to program and everyone says its the easiest language to learn and ive read alot of tutorials on how to program for beginners but i still have literally no idea where to start... will someone please help me understand

4:36 PM, December 26, 2007

 
Anonymous Andras said...

Hello!
My pick for the easiest language to learn would be definitely Python. I recommend you start with that. It is simple and clean, yet powerful. Ruby is really nice but Python is way more straightforward for a beginner, and is well documented.

3:00 AM, January 17, 2008

 
Blogger Geira said...

Instead of Practical OCaml, check out The Functional Approach to Programming by Cousineau and Mauny. Admittedly it uses Caml Light instead of OCaml, but the differences are very small (except that it doesn't cover OOP). It's gotten good revies and covers nicely the main points of FP.

There's also OCaml for Scientist by Harrop which I haven't read, plus a French O'Reilly book that's been translated into English.

12:37 PM, June 05, 2008

 
Blogger Zhenhong Bao said...

hermes outlet
ferragamo outlet
swarovski outlet
coach outlet online
toms shoes
ray-ban sunglasses
beats headphones
coach outlet online
mulberry handbags
chaussure louboutin
tory burch outlet online
cheap ray ban sunglasses
ray ban sunglasses
louis vuitton neverfull
michael kors wholesale
true religion outlet
mont blanc pens
cheap football shirts
kate spade handbags
mulberry bags
gucci outlet online
cheap jordans
nfl jersey wholesale
ralph lauren outlet
rolex watches for sale
tory burch outlet online
christian louboutin shoes
mulberry handbags
true religion jeans sale
true religion uk outlet
christian louboutin outlet
lebron james shoes
coach outlet
nike blazer pas cher
longchamp pliage
20160531zhenhong

6:18 PM, May 30, 2016

 
Blogger Zhenhong Bao said...

toms outlet
tiffany jewellery
beats headphones
oakley sunglasses wholesale
ray-ban sunglasses
oakley sunglasses wholesale
michael kors uk
rolex watches
gucci sunglasses uk
michael kors outlet store
michael kors outlet
fitflops sale clearance
prada sunglasses for women
michael kors outlet
dior sunglasses
rolex uk
ralph lauren outlet
michael kors outlet
lululemon outlet
longchamp pas cher
camisetas futbol baratas
ferragamo outlet
air force 1 shoes
louis vuitton sunglasses for women
police sunglasses
futbol baratas
reebok shoes
louis vuitton handbags outlet
cartier watches
cheap nba jerseys
prada handbags
versace sunglasses
polo ralph lauren
jordan pas cher
ray-ban sunglasses
20160531zhenhong

6:26 PM, May 30, 2016

 
Blogger Hua Cai said...

true religion jeans
snapbacks wholesale
michael kors outlet
nike air max 90
juicy couture outlet
ugg boots
air max 2015
fred perry polo
ralph lauren uk
nike roshe run
air max 90
fitflop sale
ugg outlet
nike roshe run shoes
nike roshe run
nike air max
oakley sunglasses
true religion jeans
longchamp handbags
tiffany jewellery
ugg outlet
cheap nfl jerseys
air max 90
nike outlet online
cheap soccer jerseys
nike air force 1
tory burch outlet online
rolex watches
tiffany jewelry
coach outlet online
chicago blackhawks jerseys
louis vuitton handbags outlet
lululemon outlet
reebok trainers
michael kors outlet
20160804caihuali

10:42 PM, August 03, 2016

 
Blogger Fangyaya said...

abercrombie and fitch
tod's shoes
ugg boots uk
jordan 13
michael kors handbags
ugg outlet
uggs uk
toms shoes
uggs
michael kors outlet
canada goose jackets
coach outlet store online clearances
michael kors outlet clearance
louis vuitton outlet
canada goose outlet
michael kors handbags
oakley vault
louis vuitton handbags
ugg boots
michael kors outlet
the north face jackets
gucci outlet
louis vuitton outlet
louis vuitton outlet stores
michael kors handbags
celine handbags
christian louboutin outlet
true religion outlet
adidas nmd
chenyingying20160908

6:56 PM, September 07, 2016

 
Blogger Fangyaya said...

cleveland cavaliers jerseys
canada goose
rolex watches outlet
nike factory outlet
ralph lauren uk
basketball shoes
tory burch outlet
polo ralph kids
coach factory outlet
ralph lauren polo
michael kors outlet clearance
coach factory online
uggs on sale
christian louboutin pas cher
nike air max 90
ray ban sunglasses outlet
true religion sale
toms shoes
authentic louis vuitton handbags
coach factory outlet online
washington wizards jerseys
ugg boots
michael kors purses
jordan 11
ugg boots
uggs outlet
kate spade outlet
gucci handbags
cartier watches
chenyingying20160908

7:14 PM, September 07, 2016

 
Blogger chenlina said...

adidas yeezy 350
ugg boots
adidas trainers
mbt shoes
cheap jordan shoes
gucci outlet
louis vuitton
washington wizards jerseys
ugg australia
coach factory outlet
chenlina20161018

7:49 PM, October 17, 2016

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 

If you like this blog, you might also like top photography schools.