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
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
["b", "fooa"]
["c", "fooab"]
=> "fooabc"
[:a, :b, :c].inject(arg){|memo,obj|send(obj,memo)}
is equivalent to
c(b(a(arg)))
["b", "baha"]
["c", "bahab"]
=> "bahabc"
Or how about working with lambda's or Proc objects...
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
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
=> "5"f = [:to_s, :succ] => [:to_s, :succ]
f.inject(4,:send)
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
["a", "foo"]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)}
["b", "fooa"]
["c", "fooab"]
=> "fooabc"
[:a, :b, :c].inject(arg){|memo,obj|send(obj,memo)}
is equivalent to
c(b(a(arg)))
["a", "bah"]c(b(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]
=> "fooabc"[a1, b1, c1].inject("foo"){|memo,proc| proc.call(memo)}
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