From: Brian Candler Newsgroups: comp.lang.ruby Subject: Re: anonymous closures with Proc,new, lambda and -> Date: Tue, 19 Apr 2011 03:05:53 -0500 Organization: Service de news de lacave.net Lines: 37 Message-ID: References: NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1303200374 84743 65.111.164.187 (19 Apr 2011 08:06:14 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Tue, 19 Apr 2011 08:06:14 +0000 (UTC) In-Reply-To: X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381829 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!feed.ac-versailles.fr!talisker.lacave.net!lacave.net!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:3149 Stu wrote in post #993687: > I am new to the study of functional paradigm. If this question is > academic > please bear with me. > > How would I make this counter with lambda or -> without deferring to a > named > generator method or sigil var? Starting with the "named generator method": def make_counter(init) lambda { init += 1 } end Calling that method creates a new scope, and binds the value of 'init' within that scope. Now, you can do the same without def, by wrapping in another lambda: counter_maker = lambda { |init| lambda { init += 1 } } c = counter_maker.call(100) c.call # 101 c.call # 102 And you can do the same without explicitly binding 'counter_maker' either: c = lambda { |init| lambda { init += 1 } }.call(200) c.call # 201 c.call # 202 Is that what you were looking for? -- Posted via http://www.ruby-forum.com/.