[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

functional.rb

greg

2/16/2007 12:50:00 AM

A look through RAA and rubyforge doesn't show any functional
programming libraries, are there any that I haven't noticed? Any one
interested in starting functional.rb? Here are some functional
helpers I have used.

class Object
def tap
yield self
return self
end
end

require 'enumerator'
module Enumerable
def map_msg(meth, *args)
if block_given?
map{|e| yield( e.send(meth, *args) )}
else
map{|e| e.send(meth, *args)}
end
end

def thread_map
map do |e|
Thread.new(e) {|thr_e| yield thr_e}
end.map_msg(:value)
end

def thread_map_msg( meth, *args )
if block_given?
map { |e| Thread.new(e) do |thr_e|
yield thr_e.send(meth, *args)
end }.map_msg(:value)
else
map { |e| Thread.new(e) do |thr_e|
thr_e.send( meth, *args )
end }.map_msg(:value)
end
end
end


4 Answers

Ara.T.Howard

2/16/2007 6:20:00 AM

0

Gregory Brown

2/16/2007 6:23:00 AM

0

On 2/16/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

> why not
>
> harp:~ > cat a.rb
> class Object
> def tap &b
> instance_eval &b
> return self
> end
> end
>
> puts "foobar".tap{ gsub! /bar/, '' } #=> foo

Well that prevents you from being able to access the enclosing scope,
but maybe that's okay for a tap. You could always offer both by
checking block arity.

Ara.T.Howard

2/16/2007 6:34:00 AM

0

Giles Bowkett

2/16/2007 11:56:00 PM

0

Except you should alias method_missing to #tap, so that anything can
just receive blocks directly.

.....OK, never mind, that didn't work.

On 2/15/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> On Fri, 16 Feb 2007, Gregory Brown wrote:
>
> > On 2/16/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> >
> >> why not
> >>
> >> harp:~ > cat a.rb
> >> class Object
> >> def tap &b
> >> instance_eval &b
> >> return self
> >> end
> >> end
> >>
> >> puts "foobar".tap{ gsub! /bar/, '' } #=> foo
> >
> > Well that prevents you from being able to access the enclosing scope,
> > but maybe that's okay for a tap. You could always offer both by
> > checking block arity.
> >
>
> hrrrmmm. i wouldn't say 'prevents' - but confuses ;-)
>
> harp:~ > cat a.rb
> class Object
> def tap &b
> b.arity == 1 ? yield(self) : instance_eval(&b)
> return self
> end
> end
>
> s, re = "foobar", /bar/
>
> puts s.tap{ gsub! re, nil.to_s }.upcase #=> FOO
>
> harp:~ > ruby a.rb
> FOO
>
> good idea on the arity btw.
>
> -a
> --
> we can deny everything, except that we have the possibility of being better.
> simply reflect on that.
> - the dalai lama
>
>


--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...