functional.rb

G

greg

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
 
A

ara.t.howard

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

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
 
G

Gregory Brown

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.
 
A

ara.t.howard

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
 
G

Giles Bowkett

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top