Google Groups Home Help | Sign in
Function composition in Ruby
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Tom Moertel  
View profile
 More options Apr 7 2006, 12:29 pm
Newsgroups: comp.lang.ruby
Followup-To: comp.lang.ruby
From: "Tom Moertel" <tmoer...@gmail.com>
Date: 7 Apr 2006 09:29:27 -0700
Local: Fri, Apr 7 2006 12:29 pm
Subject: Function composition in Ruby
Is it just me, or does the star operator (*) not make a perfect choice
for function composition?

    class Proc
      def self.compose(f, g)
        lambda { |*args| f[g[*args]] }
      end
      def *(g)
        Proc.compose(self, g)
      end
    end

    inc    = lambda { |x| x + 1 }
    thrice = lambda { |x| 3 * x }

    thrice_of_inc = thrice * inc
    thrice_of_inc[1]
    => 6

    twice_of_dec  = lambda { |x| 2 * x } * lambda { |x| x - 1 }
    twice_of_dec[3]
    => 4

    thrice_of_inc_of_thrice = thrice * inc * thrice
    thrice_of_inc_of_thrice[1]
    => 12

It almost feels as if it were meant to be that way. ;-)
I love Ruby.

Cheers,
Tom


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vincent Foley  
View profile
 More options Apr 7 2006, 2:11 pm
Newsgroups: comp.lang.ruby
From: "Vincent Foley" <vfo...@gmail.com>
Date: 7 Apr 2006 11:11:08 -0700
Local: Fri, Apr 7 2006 2:11 pm
Subject: Re: Function composition in Ruby
Yep.  That's why the fine Ruby Facets folks decided to use it :)
http://facets.rubyforge.org/doc/api/core/classes/Proc.html#M000146

Have a nice week end!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Moertel  
View profile
 More options Apr 7 2006, 3:10 pm
Newsgroups: comp.lang.ruby
Followup-To: comp.lang.ruby
From: "Tom Moertel" <tmoer...@gmail.com>
Date: 7 Apr 2006 12:10:45 -0700
Local: Fri, Apr 7 2006 3:10 pm
Subject: Re: Function composition in Ruby

Vincent Foley wrote:
> Yep.  That's why the fine Ruby Facets folks decided to use it :)

You know, there's a lesson in there somewhere. ;-)

Cheers,
Tom


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google