[Terminology] What's the Ruby/OO name for this paradigm?

E

E S

In my current project, one of the main paradigms is code such as in
the following example. I'm wondering if there's a Ruby/OO designation
for it so that I wouldn't have to always show some excerpt when
describing the project. It's sort of an inverse continuation passing
scheme (which, in itself, isn't exactly a prime OO term).


def three()
return lambda { |s1, s2| puts s1 + s2 }
end

def two()
return lambda { |s1| three().call(s1, 'world!') }
end

def one()
return lambda { two().call('Hello ') }
end

one().call # "Hello world!"


Anything? Input on the style welcome, too, by the way: is this hard to
understand/unintuitive/there's a better way/etc.?

E
 
T

Trans

E said:
In my current project, one of the main paradigms is code such as in
the following example. I'm wondering if there's a Ruby/OO designation
for it so that I wouldn't have to always show some excerpt when
describing the project. It's sort of an inverse continuation passing
scheme (which, in itself, isn't exactly a prime OO term).


def three()
return lambda { |s1, s2| puts s1 + s2 }
end

def two()
return lambda { |s1| three().call(s1, 'world!') }
end

def one()
return lambda { two().call('Hello ') }
end

one().call # "Hello world!"


Anything? Input on the style welcome, too, by the way: is this hard to
understand/unintuitive/there's a better way/etc.?

def three
lambda { |s1, s2| puts s1 + s2 }
end

def two
lambda { |s1| three[s1, 'world!'] }
end

def one
lambda { two['Hello '] }
end

one[] # "Hello world!"


But why do?

T
 
E

Eric Hodel

--Apple-Mail-44-258959299
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

In my current project, one of the main paradigms is code such as in
the following example. I'm wondering if there's a Ruby/OO designation
for it so that I wouldn't have to always show some excerpt when
describing the project. It's sort of an inverse continuation passing
scheme (which, in itself, isn't exactly a prime OO term).

def three()
return lambda { |s1, s2| puts s1 + s2 }
end

def two()
return lambda { |s1| three().call(s1, 'world!') }
end

def one()
return lambda { two().call('Hello ') }
end

one().call # "Hello world!"

Its called "Currying".

http://c2.com/cgi/wiki/Curry?CurryingSchonfinkelling

Not an OO term, but a functional programming term.
Anything? Input on the style welcome, too, by the way: is this hard to
understand/unintuitive/there's a better way/etc.?

--
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-44-258959299
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCAdCLMypVHHlsnwQRAlQ6AJ9xeMD5EGAjf6ySwPNkGGnBUvAzOQCeM5FX
qrym2FH4SvsC+L4GMQTDGFY=
=QX7h
-----END PGP SIGNATURE-----

--Apple-Mail-44-258959299--
 
M

Martin DeMello

E S said:
In my current project, one of the main paradigms is code such as in
the following example. I'm wondering if there's a Ruby/OO designation
for it so that I wouldn't have to always show some excerpt when
describing the project. It's sort of an inverse continuation passing
scheme (which, in itself, isn't exactly a prime OO term).

Technically currying, but from the example it looks like the intent is
simply function wrapping.

e.g. we do this a lot at work:

one(args) {
ensure thread safety;
return two(args)
}

two(args) {
ensure constraint satisfaction;
return three(args)
}

three(args) {
do the actual work;
return the actual result;
}

Is that the same thing you're using it for?

martin
 
D

Douglas Livingstone

e.g. we do this a lot at work:
one(args) {
ensure thread safety;
return two(args)
}

two(args) {
ensure constraint satisfaction;
return three(args)
}

three(args) {
do the actual work;
return the actual result;
}

Is that the same thing you're using it for?

Reminds me of decorators[1] except applied to functions instead of
objects. (Well.. OK... functions are objects... clss sense then...)

Douglas

[1]http://c2.com/cgi/wiki?DecoratorPattern
 
M

Mathieu Bouchard

In my current project, one of the main paradigms is code such as in
the following example. I'm wondering if there's a Ruby/OO designation
for it so that I wouldn't have to always show some excerpt when
describing the project. It's sort of an inverse continuation passing
scheme (which, in itself, isn't exactly a prime OO term).

Anything? Input on the style welcome, too, by the way: is this hard to
understand/unintuitive/there's a better way/etc.?

It is called "delayed evaluation". The pattern is that instead of having a
value of type A, you have a function taking no arguments whose return is
of type A. Some take this concept further, ensuring the function is only
actually called once, and the result is cached (Scheme has a macro called
DELAY for that). That's the kind of thing you'd do to have lazy semantics
in a non-lazy language.

Don't listen to those who might call this "currying". Currying means
decomposing the argument list like this:

f = lambda{|a,b,c| a*b+c }
p f[4,10,2]

g = lambda{|a| lambda{|b| lambda{|c| a*b+c }}}
p g[4][10][2]

here g is called the currying of f.

typewise, that's (A*B*C)->D becoming A->(B->(C->D)).

_____________________________________________________________________
Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top