[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

PartialInvocation sample

Guoliang Cao

4/26/2008 6:43:00 PM

Hi,

I created a partial invocation example below, which I think can be
useful in many cases. Since ruby 1.9 introduced curry as a syntax suger,
perhaps a generic and consistent partial invocation can be given a
thought.

# A partial invocation example
#
# Define a dummy object @@_ and define a kernel method _ to return that
object
# Modify method_missing in Object to treat method whose name ends with
'_' as a partial invocation
#
# Partial invocation is like obj.do_this_ a, _, c
#
# Benefit: intuitive partial invocation, can work with IDE's
auto-completion.
# Issue: people can use xxx_ as a real method name, which causes
confusion


class A
def do_this a, b, *c
puts "========== do_this: ", a, b, c
end

def self.do_that a, b, *c
puts "========== do_that: ", a, b, c
end
end

A.new.do_this 1, 2, 3, 4
A.do_that 1, 2, 3, 4

module Kernel
@@_ = Object.new
def _
@@_
end
end

class Object
alias method_missing_old method_missing

def method_missing method, *args, &block
if method.to_s[-1,1] == '_'
lambda{|*a|
new_args = []
args.each_with_index do |arg, i|
if arg === _
if i < args.length - 1
new_args.push a.shift
else
new_args.push *a
end
else
new_args.push arg
end
end
new_method = method.to_s.chomp '_'
send new_method, *new_args, &block
}
else
method_missing_old method, *args, &block
end
end
end

A.new.do_this_(1, _, 3, 4)[2]
A.do_that_(1, _, 3, 4)[2]

A.new.do_this_(_, _, 3, 4)[1, 2]
A.do_that_(_, _, 3, 4)[1, 2]

A.new.do_this_(1, 2, 3, 4)[]
A.do_that_(1, 2, 3, 4)[]

do_this_ = A.new.do_this_(1, 2, _)
do_this_[3]
do_this_[3, 4]
--
Posted via http://www.ruby-....

20 Answers

Trans

4/26/2008 8:15:00 PM

0



On Apr 26, 2:42 pm, Guoliang Cao <ca...@verizon.net> wrote:
> Hi,
>
> I created a partial invocation example below, which I think can be
> useful in many cases. Since ruby 1.9 introduced curry as a syntax suger,
> perhaps a generic and consistent partial invocation can be given a
> thought.
>
> # A partial invocation example
> #
> # Define a dummy object @@_ and define a kernel method _ to return that
> object
> # Modify method_missing in Object to treat method whose name ends with
> '_' as a partial invocation
> #
> # Partial invocation is like obj.do_this_ a, _, c
> #
> # Benefit: intuitive partial invocation, can work with IDE's
> auto-completion.
> # Issue: people can use xxx_ as a real method name, which causes
> confusion
>
> class A
> def do_this a, b, *c
> puts "========== do_this: ", a, b, c
> end
>
> def self.do_that a, b, *c
> puts "========== do_that: ", a, b, c
> end
> end
>
> A.new.do_this 1, 2, 3, 4
> A.do_that 1, 2, 3, 4
>
> module Kernel
> @@_ = Object.new
> def _
> @@_
> end
> end
>
> class Object
> alias method_missing_old method_missing
>
> def method_missing method, *args, &block
> if method.to_s[-1,1] == '_'
> lambda{|*a|
> new_args = []
> args.each_with_index do |arg, i|
> if arg === _
> if i < args.length - 1
> new_args.push a.shift
> else
> new_args.push *a
> end
> else
> new_args.push arg
> end
> end
> new_method = method.to_s.chomp '_'
> send new_method, *new_args, &block
> }
> else
> method_missing_old method, *args, &block
> end
> end
> end
>
> A.new.do_this_(1, _, 3, 4)[2]
> A.do_that_(1, _, 3, 4)[2]
>
> A.new.do_this_(_, _, 3, 4)[1, 2]
> A.do_that_(_, _, 3, 4)[1, 2]
>
> A.new.do_this_(1, 2, 3, 4)[]
> A.do_that_(1, 2, 3, 4)[]
>
> do_this_ = A.new.do_this_(1, 2, _)
> do_this_[3]
> do_this_[3, 4]
> --
> Posted viahttp://www.ruby-....

Hi,

#_ is sort off limits because IRB uses it. However, #__ is usable, and
that is what Facets' library uses.

While the notation is nice, I'm not sure is necessary. If #curry is
extended to take slot-order as arguments, then curry itself can be
used. Eg.

aproc = Proc.new{ |a,b| a ** b }

aproc.curry => lambda{ |a| lambda{ |b| a ** b }}

aproc.curry(1) => lambda{ |b| lambda{ |a| a ** b }}

More complex...

aproc = Proc.new{ |a,b,c,d| ...}

aproc.curry(3,1,0) => lambda{ |d| lambda{ |b| lambda{ |a| lambda{ |
c|...

Note, that last slot (2) is implied and need not be specified in the
arguments.

T.

Guoliang Cao

4/27/2008 4:16:00 AM

0

Trans wrote:
>
> Hi,
>
> #_ is sort off limits because IRB uses it. However, #__ is usable, and
> that is what Facets' library uses.

I'm not familiar with how _ is used in IRB. Can you please explain a bit
more?

>
> While the notation is nice, I'm not sure is necessary. If #curry is
> extended to take slot-order as arguments, then curry itself can be
> used. Eg.
>
> aproc = Proc.new{ |a,b| a ** b }
>
> aproc.curry => lambda{ |a| lambda{ |b| a ** b }}
>
> aproc.curry(1) => lambda{ |b| lambda{ |a| a ** b }}
>
> More complex...
>
> aproc = Proc.new{ |a,b,c,d| ...}
>
> aproc.curry(3,1,0) => lambda{ |d| lambda{ |b| lambda{ |a| lambda{ |
> c|...
>
> Note, that last slot (2) is implied and need not be specified in the
> arguments.
>
> T.

I agree curry can be made more generic. It'll be even better if we can
have partial invocation on method calls. I think most of the time procs
are local to a method and we can substitute known values into proc body
without currying. However partial method invocation can be very useful
to reduce entering same literal values.
--
Posted via http://www.ruby-....

Arlen Cuss

4/27/2008 4:24:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Sun, Apr 27, 2008 at 2:16 PM, Guoliang Cao <caogl@verizon.net> wrote:

> I'm not familiar with how _ is used in IRB. Can you please explain a bit
> more?


_ is the last returned value:

$ irb
>> _
=> nil
>> 42
=> 42
>> _
=> 42
>>

Arlen

Matt

6/12/2014 11:24:00 PM

0

"max headroom" <maxheadroom@localnet.com> wrote in
news:lndah7$scv$1@dont-email.me:

> GOP_Decline_and_Fall <Dev@null.net> wrote in
> news:r62kp99nmu9j56tllrpj0d9hm2rceoa30f@4ax.com:
>
>> The Dark Money Machine That Beat Eric Cantor
>
> talk.politics.GUNS
>
>
>

No, alt.society.liberalism. I mean, really, if you are going to complain
about crossposts why not restrict your complaint to the group that it
doesn't belong to (not that I disagree with your complaint)?

Matt

Scout

6/12/2014 11:41:00 PM

0



"max headroom" <maxheadroom@localnet.com> wrote in message
news:lndah7$scv$1@dont-email.me...
> GOP_Decline_and_Fall <Dev@null.net> wrote in
> news:r62kp99nmu9j56tllrpj0d9hm2rceoa30f@4ax.com:
>
>> The Dark Money Machine That Beat Eric Cantor
>
> talk.politics.GUNS

Oh, just FYI.... Cantor outspent Brat by some 25:1

Must be one heck of a money machine if it's dollars are at least 25 times
more effective.


deep

6/13/2014 12:03:00 AM

0

On Thu, 12 Jun 2014 19:41:27 -0400, "Scout"
<me4guns@centurylink.removeme.this2.nospam.net> wrote:

>
>
>"max headroom" <maxheadroom@localnet.com> wrote in message
>news:lndah7$scv$1@dont-email.me...
>> GOP_Decline_and_Fall <Dev@null.net> wrote in
>> news:r62kp99nmu9j56tllrpj0d9hm2rceoa30f@4ax.com:
>>
>>> The Dark Money Machine That Beat Eric Cantor
>>
>> talk.politics.GUNS
>
>Oh, just FYI.... Cantor outspent Brat by some 25:1
>
>Must be one heck of a money machine if it's dollars are at least 25 times
>more effective.
>
Their propaganda was designed to target the weak minded. You know,
like you. Or I guess you don't...

Scout

6/13/2014 12:51:00 AM

0



"deep" wrote in message news:esfkp99fdb9qnncqvtsd3g438tumdfvf01@4ax.com...
> On Thu, 12 Jun 2014 19:41:27 -0400, "Scout"
> <me4guns@centurylink.removeme.this2.nospam.net> wrote:
>
>>
>>
>>"max headroom" <maxheadroom@localnet.com> wrote in message
>>news:lndah7$scv$1@dont-email.me...
>>> GOP_Decline_and_Fall <Dev@null.net> wrote in
>>> news:r62kp99nmu9j56tllrpj0d9hm2rceoa30f@4ax.com:
>>>
>>>> The Dark Money Machine That Beat Eric Cantor
>>>
>>> talk.politics.GUNS
>>
>>Oh, just FYI.... Cantor outspent Brat by some 25:1
>>
>>Must be one heck of a money machine if it's dollars are at least 25 times
>>more effective.
>>
> Their propaganda was designed to target the weak minded. You know,
> like you. Or I guess you don't...

Gee... then Democrats should have been converting to GOP in droves....


GOP_Decline_and_Fall

6/13/2014 1:21:00 AM

0

On Thu, 12 Jun 2014 19:41:27 -0400, "Scout"
<me4guns@centurylink.removeme.this2.nospam.net> wrote:

>
>
>"max headroom" <maxheadroom@localnet.com> wrote in message
>news:lndah7$scv$1@dont-email.me...
>> GOP_Decline_and_Fall <Dev@null.net> wrote in
>> news:r62kp99nmu9j56tllrpj0d9hm2rceoa30f@4ax.com:
>>
>>> The Dark Money Machine That Beat Eric Cantor
>>
>> talk.politics.GUNS
>
>Oh, just FYI.... Cantor outspent Brat by some 25:1

Only in overt funding.

>Must be one heck of a money machine if it's dollars are at least 25 times
>more effective.

Why do you think it's called Dark Money?

http://crooksandliars.com/2014/06/david-brat-represents-taliban-...

Brat's Stealth Dark Money Machine

The media is making it seem like Brat was some sort of underdog,
but in reality, he's strapped to the hilt with billionaire support and
billionaire money.

In fact, you could argue that he pretty much owes his job to
people like the Koch brothers and their cronies. John Allison, the
former CEO of BB&T bank and the current head of the Koch-founded Cato
Institute, gave Brat's college a $500,000 fellowship back in 2010 so
he could teach Ayn Rand and libertarianism at Randolph Macon
University. Like hundreds of other college professors across the
country these days, David Brat is really just a bought-and-paid-for
shill of Charles and David Koch and their buddies.

The Kochs and their network have been funding academic institutions
for years, most notably George Mason University's Mercatus Center,
because they knew they had to bring up young academics in their
libertarian money tradition in order to fully realize their political
ambitions.

Brat's campaign manager, 23-year old Zachary Werrell, cut his
political teeth at Ron Paul's Campaign for Liberty, which is closely
affiliated with Americans for Prosperity and the Kochtopus. His
treasurer is Steven D'Ambrosia, an executive with Altria Corporation.
Free advertising, via right-wing talkers

Most significantly, Brat received the full support of the conservative
messaging machine, from churches to right-wing talk radio hosts like
Laura Ingraham and Mark Levin.

Over the past few months, right-wing talkers like Laura Ingraham
and Mark Levin have been pushing Brat and attacking Cantor non-stop on
their radio shows. Ingraham even went so far as to say that she wished
that President Obama traded Eric Cantor to the Taliban in exchange for
Bowe Bergdahl.

Laura and Mark are both on the populist end of the Republican
Party, so it's not all that surprising that they would want to see
Brat take down Cantor. But since both of them have taken a lot of
money from conservative groups like Americans for Prosperity over the
past few years, there's good reason to be suspicious of why they've
been pushing so hard specifically for Brat.

As Politico reported a few months ago in what should have been a
blockbuster story but was ignored by the mainstream media,"[F]ilings
with the Internal Revenue Service and Federal Election Commission, as
well as interviews and reviews of radio shows, found that conservative
groups spent nearly $22 million to broker and pay for involved
advertising relationships known as sponsorships with a handful of
influential talkers including Beck, Sean Hannity, Laura Ingraham, Mark
Levin and Rush Limbaugh ... Since then, the sponsorship deals have
grown more lucrative and tea party-oriented..."

Levin alone apparently took about $757,000 from the Koch-backed
Americans for Prosperity over the 2012 election cycle, and if
Politico's report is accurate, he's still taking money from them.

The reports are accurate.

Kochtopus tentacles and Mark Levin

Levin is also the architect and spokesperson for the 'movement' to
calls a constitutional convention via the nonprofit Citizens for
Self-Governance, which was incubated by the John Hancock Committee for
the States.

Disgraced former Tea Party Patriot founder Mark Meckler serves as
Executive Director of the John Hancock Committee (JHC) for the States
and Citizens for Self-Governance alongside Ned and Drew Ryun, Tim Dunn
and Michael Sullivan.

JHC was incubated by American Majority with the Ryuns at the helm.
American Majority was incubated by the Sam Adams Alliance, funded with
Koch and Bradley foundation funds.

In 2012, $900,000 flowed through the Vanguard Charitable Gift Fund to
JHC. This Vanguard donor-advised fund is often used as a conduit by
conservatives to direct anonymous donations to political
organizations. In fact, it's not unusual to see donors move money into
Donors' Trust or Donors' Capital and then out to Vanguard or Schwab's
donor-advised funds to give them double indemnity against
identification

Sid9

6/13/2014 1:49:00 AM

0


"Scout" <me4guns@centurylink.removeme.this2.nospam.net> wrote in message
news:lnddrb$fqe$1@dont-email.me...
>
>
> "max headroom" <maxheadroom@localnet.com> wrote in message
> news:lndah7$scv$1@dont-email.me...
>> GOP_Decline_and_Fall <Dev@null.net> wrote in
>> news:r62kp99nmu9j56tllrpj0d9hm2rceoa30f@4ax.com:
>>
>>> The Dark Money Machine That Beat Eric Cantor
>>
>> talk.politics.GUNS
>
> Oh, just FYI.... Cantor outspent Brat by some 25:1
>
> Must be one heck of a money machine if it's dollars are at least 25 times
> more effective.
>
>

Real Republicans didn't care and didn't come out.
This bodes evil for the Republican Party.

Scout

6/13/2014 1:52:00 AM

0



"Sid9" <sid9@bellsouth.net> wrote in message
news:lndlbj$o9r$1@dont-email.me...
>
> "Scout" <me4guns@centurylink.removeme.this2.nospam.net> wrote in message
> news:lnddrb$fqe$1@dont-email.me...
>>
>>
>> "max headroom" <maxheadroom@localnet.com> wrote in message
>> news:lndah7$scv$1@dont-email.me...
>>> GOP_Decline_and_Fall <Dev@null.net> wrote in
>>> news:r62kp99nmu9j56tllrpj0d9hm2rceoa30f@4ax.com:
>>>
>>>> The Dark Money Machine That Beat Eric Cantor
>>>
>>> talk.politics.GUNS
>>
>> Oh, just FYI.... Cantor outspent Brat by some 25:1
>>
>> Must be one heck of a money machine if it's dollars are at least 25 times
>> more effective.
>>
>>
>
> Real Republicans didn't care and didn't come out.

Cite?

Didn't think so.

Keep making stuff up there Sid, it's not like anyone believes you anyway.