Functional.rb

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

On 2/15/07, greg [email protected] wrote:

A look through RAA and rubyforge doesn’t show any functional
programming libraries, are there any that I haven’t noticed?

You may want to have a look at MenTaLguY’s handiwork:

http://moonbase.rydia.net/software/lazy.rb/
http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/00introduction.html

Google also yielded Florian F.'s functional:

http://www.ping.de/~flori/flott/exe/session/project/ruby/functional

/Nick

On Fri, 16 Feb 2007, greg wrote:

end
why not

harp:~ > cat a.rb
class Object
def tap &b
instance_eval &b
return self
end
end

puts “foobar”.tap{ gsub! /bar/, ‘’ } #=> foo

harp:~ > ruby a.rb
foo

??

-a

On 2/16/07, [email protected] [email protected] wrote:

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.

On Fri, 16 Feb 2007, Gregory B. wrote:

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 :wink:

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

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

…OK, never mind, that didn’t work.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs