# Functional.rb

**URL:** <https://www.ruby-forum.com/t/functional-rb/85752>\
**Category:** Ruby\
**Created:** [February 16, 2007, 1:50am UTC](https://www.ruby-forum.com/t/functional-rb/85752 "2007-02-16T01:50:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 16, 2007, 1:50am UTC](https://www.ruby-forum.com/t/functional-rb/85752/1 "2007-02-16T01:50:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 16, 2007, 3:27am UTC](https://www.ruby-forum.com/t/functional-rb/85752/2 "2007-02-16T03:27:41Z")

</div>

On 2/15/07, greg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/software/lazy.rb/)  
[http://moonbase.rydia.net/mental/writings/programming/monads-in-ruby/00introduction.html](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](http://www.ping.de/~flori/flott/exe/session/project/ruby/functional)

/Nick

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 16, 2007, 7:20am UTC](https://www.ruby-forum.com/t/functional-rb/85752/3 "2007-02-16T07:20:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 16, 2007, 7:23am UTC](https://www.ruby-forum.com/t/functional-rb/85752/4 "2007-02-16T07:23:54Z")

</div>

On 2/16/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 16, 2007, 7:34am UTC](https://www.ruby-forum.com/t/functional-rb/85752/5 "2007-02-16T07:34:54Z")

</div>

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 😉

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 17, 2007, 12:56am UTC](https://www.ruby-forum.com/t/functional-rb/85752/6 "2007-02-17T00:56:29Z")

</div>

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

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