examples of functional programming in Ruby 
Author Message
 examples of functional programming in Ruby

Would anyone be willing to share some  code snippets that show examples of
functional programming in Ruby?

Phil



Wed, 21 Apr 2004 14:37:52 GMT  
 examples of functional programming in Ruby

Quote:

> Would anyone be willing to share some  code snippets that show examples of
> functional programming in Ruby?

> Phil

checkout

http://www.rubygarden.org/ruby?CodingInRuby

and

http://www.rubycookbook.org

--
Nigel S. Ball
Enesbe Sales Pty Ltd



Wed, 21 Apr 2004 15:53:23 GMT  
 examples of functional programming in Ruby

Quote:

> Would anyone be willing to share some  code snippets that show examples of
> functional programming in Ruby?

> Phil

Hi,

I have been absent from the ruby group for a while. Anyway here is an
Fibbonacci example from the ``what if Fixnum singleton classes
existed'' department.

Surprisingly enough the implementation is faster then a
straightforward recursive implementation.

class Integer
  def fib
     return 1 if self < 2
     return (self-1).fib + (self-2).fib
  end
end

(using generators is much faster but this another matter)

-------------
require 'simple_rec.rb'

class PositiveRecursiveInteger
   def fib; pred.fib + pred.pred.fib end
   def Null.fib; 1 end
   def (Null.succ).fib; 1 end
end

p (31.to_posrec_int.fib) ==> #  2178309

-------------
# simple_rec.rb
# A simple example of a class with recursive
# instance creation - not thread safe!

class Module
   public :remove_method
end
class Object
   def remove_singleton_method sym
      class << self; self end.remove_method sym
   end
end

class PositiveRecursiveInteger < Numeric
   Null = self::new
   def Null.pred; raise ArgumentError 'negative predessor' end
   def Null.succ;  __succ__  end

   attr_reader :succ, :pred, :to_i

   alias inspect to_s


   private
   def __succ__
      remove_singleton_method :succ



   end

   public
   def coerce (other); [to_i, other] end
   def +(other); to_i + other end
   def -(other); to_i - other end
   def *(other); to_i* other end
   # etc  ...
end

class Fixnum
   # this is rather slow ..
   def to_posrec_int
      raise ArgumentError 'negative argument' if self < 0
      res = PositiveRecursiveInteger::Null
      times { res=res.succ }
      return res
   end
end



Thu, 22 Apr 2004 02:09:03 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Good example Ruby program?

2. Ruby/GTK: example needed of Widget written in Ruby

3. Application Facade Example in ruby (was: refactoring ruby code)

4. functional.py 0.6 - Functional Programming in Python

5. functional.py 0.7 - Functional programming in Python

6. functional.py - functional programming in Python

7. functional.py 0.7 - Functional programming in Python

8. functional.py 0.6 - Functional Programming in Python

9. functional.py - functional programming in Python

10. US PhD programs strong on functional programming?

11. again: Ruby program without ruby installation

12. Error in Programming Ruby and patch suggestion for ruby

 

 
Powered by phpBB® Forum Software