Point free (pointless) programming in ruby?

J

John Carter

I'm very fond of the notion of Concatenative Languages such as Joy,
Factor...
http://en.wikipedia.org/wiki/Joy_(programming_language)
http://www.latrobe.edu.au/philosophy/phimvt/joy/jp-joyjoy.html
http://factorcode.org/

There is a supreme simplicity about them.

So while reading what the current state of them is, I stumbled across
the notion of Point free (sometimes called Pointless) programming.

The idea of sequences of function compositions that elide the
arguments that they will be applied to.

http://www.haskell.org/haskellwiki/Pointfree

One can easily define a sequence of methods in Ruby...

fun_seq = [:a, :b, :c]

as a sequence of symbols.

Which one could apply to an arbitrary argument..

irb
f = [:to_s, :succ] => [:to_s, :succ]
f.inject(4,:send)
=> "5"

Oooh! Looky! That's sneaky of me!
symbol_sequence.inject(arg,:send)

[:a, :b, :c].inject(arg,:send)
that's equivalent to
arg.a.b.c

Not quite function composition. Cute, but let's try...

irb
def a(a)
p ["a",a]
a+"a"
end => nil
def b(b)
p ["b",b]
b+"b"
end => nil
def c(c)
p ["c",c]
c+"c"
end => nil
f=[:a,:b,:c] => [:a, :b, :c]
f.inject("foo"){|memo,obj|send(obj,memo)}
["a", "foo"]
["b", "fooa"]
["c", "fooab"]
=> "fooabc"

[:a, :b, :c].inject(arg){|memo,obj|send(obj,memo)}
is equivalent to
c(b(a(arg)))
c(b(a("bah")))
["a", "bah"]
["b", "baha"]
["c", "bahab"]
=> "bahabc"

Or how about working with lambda's or Proc objects...
a1 = lambda {|x| x+"a"}
=> # said:
b1 = lambda {|x| x+"b"}
=> # said:
c1 = lambda {|x| x+"c"}
=> # said:
[a1, b1, c1]
[a1, b1, c1].inject("foo"){|memo,proc| proc.call(memo)}
=> "fooabc"


Anyhoo! Clearly in principle one can do Pointfree programming Ruby.

Questions for the Group:

1) Is there a neater way of expressing a sequence of function
compositions in Ruby?

2) Which Ruby Pointfree sequences are actually useful?



John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 
B

Brian Candler

1) Is there a neater way of expressing a sequence of function
compositions in Ruby?

Maybe you could define an Array#to_proc. e.g.

class Array
def to_proc
lambda { |src| inject(src) { |memo,proc| proc[memo] } }
end
end

a1 = lambda { |x| x+"a1" }
b1 = lambda { |x| x+"b1" }
c1 = lambda { |x| x+"c1" }
p lambda(&[a1,b1,c1])["test"]

src = ["foo","bar","baz"]
p src.map(&[a1, b1, c1])


def a(x); x+[1]; end
def b(x); x+[2]; end

src = [[], [0], [0,0]]

p src.map(&[method:)a), method:)b)])
 
J

John Carter

1) Is there a neater way of expressing a sequence of function
compositions in Ruby?

Maybe you could define an Array#to_proc. e.g.

class Array
def to_proc
lambda { |src| inject(src) { |memo,proc| proc[memo] } }
end
end
p src.map(&[method:)a), method:)b)])

Cool! I had to spend a bit of time rereading the ri docs and playing
with irb to work out what you were doing... :) ...but very cool
nevertheless.

Ooh.. I bet somebody could...
a) Win an obfuscated programming contest...
b) Do something very very cool and surprisingly useful..
with the stuff.

Basically what I'm seeing here is Ruby is actually a superset of the
concatenative languages. If you stick within a strict subset of Ruby
you can do the almost magical rewrite algebras that you can do in
something like Joy..

http://www.latrobe.edu.au/philosophy/phimvt/joy/j07rrs.html

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
 
B

Brian Candler

John said:
Cool! I had to spend a bit of time rereading the ri docs and playing
with irb to work out what you were doing... :) ...but very cool
nevertheless.

Sorry, I should have added: this was nothing clever on my behalf. I was
just copying Symbol#to_proc (which you can google for, and has been
incorporated into both Rails and ruby 1.9)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top