Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3142 > unrolled thread

anonymous closures with Proc,new, lambda and ->

Started byStu <stu@rubyprogrammer.net>
First post2011-04-19 01:07 -0500
Last post2011-04-20 12:44 -0500
Articles 12 on this page of 32 — 8 participants

Back to article view | Back to comp.lang.ruby


Contents

  anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-19 01:07 -0500
    Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-19 02:30 -0500
    Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 03:05 -0500
      Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 03:15 -0500
        Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-19 04:57 -0500
          Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 07:24 -0500
            Re: anonymous closures with Proc,new, lambda and -> Adam Prescott <adam@aprescott.com> - 2011-04-19 14:55 -0500
        Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-19 16:43 -0500
          Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 18:48 -0500
          Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 21:28 -0500
            Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-20 01:03 -0500
              Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-20 02:23 -0500
                Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-20 05:55 -0500
              Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-20 12:32 -0500
                Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-20 12:59 -0500
                  Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-20 15:40 -0500
                    Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-20 19:51 -0500
                      Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-21 01:31 -0500
                        Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-21 04:34 -0500
                        Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 06:13 -0500
                        Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 09:20 -0500
                        Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-21 11:56 -0500
                          Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-21 12:06 -0500
                            Re: anonymous closures with Proc,new, lambda and -> Robert Klemme <shortcutter@googlemail.com> - 2011-04-27 06:49 -0500
                              Re: anonymous closures with Proc,new, lambda and -> Stu <stu@rubyprogrammer.net> - 2011-04-27 13:33 -0500
                              Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-27 17:06 -0500
    Re: anonymous closures with Proc,new, lambda and -> 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-19 13:02 -0500
      Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-19 13:19 -0500
        Re: anonymous closures with Proc,new, lambda and -> Steve Klabnik <steve@steveklabnik.com> - 2011-04-19 13:26 -0500
          Re: anonymous closures with Proc,new, lambda and -> Michael Edgar <adgar@carboni.ca> - 2011-04-19 13:51 -0500
            Re: anonymous closures with Proc,new, lambda and -> Brian Candler <b.candler@pobox.com> - 2011-04-20 10:18 -0500
              Re: anonymous closures with Proc,new, lambda and -> Jeremy Bopp <jeremy@bopp.net> - 2011-04-20 12:44 -0500

Page 2 of 2 — ← Prev page 1 [2]


#3315

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-21 09:20 -0500
Message-ID<BANLkTi=+kN757G8KCOF-A4WYATNic+Kcqw@mail.gmail.com>
In reply to#3291
On Thu, Apr 21, 2011 at 8:31 AM, Stu <stu@rubyprogrammer.net> wrote:
> My interest in learning functional programming( or at least the
> foundation of) is because I haven't done it before( though I flirted
> with scheme a couple years ago). I don't really know what monads are I
> was just compiling a list of "things I should look into" for future
> reference to take the autodidactic approach to learning functional
> paradigms.

I just came across this report how others teach FP:
http://existentialtype.wordpress.com/2011/04/17/some-advice-on-teaching-fp/

Cheers

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#3319

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-21 11:56 -0500
Message-ID<f54ae0eea0f00bd413231285d665100f@ruby-forum.com>
In reply to#3291
Stu wrote in post #994213:
>
> As Robert pointed out Ruby may not be the best language to learn
> functional programming concepts with.
>

I would agree with that.  Functional programming is much different than 
creating a ruby closure.


> Also is there a canonical or pedagogical tutorial for me to start
> with? Hello, World! =)
>

Learn you a Haskell for great Good!

http://learnyouahaskell.com/

Just start the tutorial, and see what you can see.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3320

FromSteve Klabnik <steve@steveklabnik.com>
Date2011-04-21 12:06 -0500
Message-ID<BANLkTi=xp-Uh8t--tSYXF=f4eaOpBpjD0Q@mail.gmail.com>
In reply to#3319
[Note:  parts of this message were removed to make it a legal post.]

+1 for learn you a haskell.

Ruby does have functional elements, but you won't truly learn FP, because
Ruby is all about structuring programs in an OO fashion, and solving small
problems in FPish ways when it makes sense. If that makes sense. Using a
'real' FP language will make you structure your problems in a functional
way...

Erlang would be okay too, but Haskell is better. Scheme is also solid. But
Haskell is uber-strict, and so you'll end up really learning FP well.

[toc] | [prev] | [next] | [standalone]


#3558

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-27 06:49 -0500
Message-ID<BANLkTikKFLTh89xfGBOnFvNxXYnThcFkcA@mail.gmail.com>
In reply to#3320
On Thu, Apr 21, 2011 at 7:06 PM, Steve Klabnik <steve@steveklabnik.com> wrote:
> +1 for learn you a haskell.

Professor Robert Harper thinks "Haskell is not suitable for teaching
introductory functional programming":
http://existentialtype.wordpress.com/2011/04/24/the-real-point-of-laziness/

Cheers

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#3569

FromStu <stu@rubyprogrammer.net>
Date2011-04-27 13:33 -0500
Message-ID<BANLkTikNOSkc-awjM6hq0TXEs=uMq0kF-g@mail.gmail.com>
In reply to#3558
I'm gonna give scheme a go. I did run through a bit of
http://tryhaskell.org/ though.

On Wed, Apr 27, 2011 at 6:49 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On Thu, Apr 21, 2011 at 7:06 PM, Steve Klabnik <steve@steveklabnik.com> wrote:
>> +1 for learn you a haskell.
>
> Professor Robert Harper thinks "Haskell is not suitable for teaching
> introductory functional programming":
> http://existentialtype.wordpress.com/2011/04/24/the-real-point-of-laziness/
>
> Cheers
>
> robert
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/
>
>

[toc] | [prev] | [next] | [standalone]


#3586

FromSteve Klabnik <steve@steveklabnik.com>
Date2011-04-27 17:06 -0500
Message-ID<BANLkTinDvzs2wDTNSa1drh8wgvwymqRBZA@mail.gmail.com>
In reply to#3558
[Note:  parts of this message were removed to make it a legal post.]

>
> Professor Robert Harper thinks
>

Well, non-professor Steve disagrees. :p

[toc] | [prev] | [next] | [standalone]


#3182

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-19 13:02 -0500
Message-ID<03b15245deb79f0148cf821c34a7ed41@ruby-forum.com>
In reply to#3142
Where does this syntax come from?

  closed = Proc.new(over=0){over+=1}

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3184

FromBrian Candler <b.candler@pobox.com>
Date2011-04-19 13:19 -0500
Message-ID<ae38c1271ac391d8aad35ef2cc946cda@ruby-forum.com>
In reply to#3182
7stud -- wrote in post #993798:
> Where does this syntax come from?
>
>   closed = Proc.new(over=0){over+=1}

It's invalid in 1.8:

>> Proc.new(0) {}
ArgumentError: wrong number of arguments (1 for 0)
  from (irb):2:in `initialize'
  from (irb):2:in `new'
  from (irb):2
  from :0

But accepted in 1.9.2:

>> Proc.new(0) {}
=> #<Proc:0x000000023cc380@(irb):1>

Seems to be undocumented, like much of ruby 1.9.
http://ruby-doc.org/core/classes/Proc.html

I can't see any obvious purpose for these args:

irb(main):006:0> c = Proc.new(1,2,3) { |x,y,z| p x,y,z }
=> #<Proc:0x00000001f17920@(irb):6>
irb(main):007:0> c.call(5)
5
nil
nil
=> [5, nil, nil]

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3185

FromSteve Klabnik <steve@steveklabnik.com>
Date2011-04-19 13:26 -0500
Message-ID<BANLkTimUF7wuGnis4endTjSQgW7TOnpJ+w@mail.gmail.com>
In reply to#3184
[Note:  parts of this message were removed to make it a legal post.]

Doesn't look like it's really used for anything:
https://github.com/ruby/ruby/blob/trunk/proc.c#L448

[toc] | [prev] | [next] | [standalone]


#3187

FromMichael Edgar <adgar@carboni.ca>
Date2011-04-19 13:51 -0500
Message-ID<F3D54DF8-5D84-4B3F-9EFF-BE6D36319697@carboni.ca>
In reply to#3185
Interesting. The default allocation behavior method is removed, and Class.new is overridden, which uses custom allocation and forwards arguments to #initialize - just like normal Class.new. Except Proc doesn't implement #initialize, so it bubbles up to Object#initialize. Since Object#initialize just takes any number of arguments and ignores them, providing an argument to Proc.new doesn't raise.

It seems adding an empty, 0-arg #initialize method to Proc doesn't really lose anything here, and would catch mistakes. The only tiny downside would be a miniscule slow down on explicit calls to Proc.new... which I think most Rubyists can live with.

Michael Edgar
adgar@carboni.ca
http://carboni.ca/

On Apr 19, 2011, at 2:26 PM, Steve Klabnik wrote:

> Doesn't look like it's really used for anything:
> https://github.com/ruby/ruby/blob/trunk/proc.c#L448

[toc] | [prev] | [next] | [standalone]


#3246

FromBrian Candler <b.candler@pobox.com>
Date2011-04-20 10:18 -0500
Message-ID<9b174c2306ccb671f0cdd13f433067f9@ruby-forum.com>
In reply to#3187
Michael Edgar wrote in post #993817:
> Proc
> doesn't implement #initialize, so it bubbles up to Object#initialize.
> Since Object#initialize just takes any number of arguments and ignores
> them, providing an argument to Proc.new doesn't raise.

So I think the question becomes: why does Object#initialize accept any 
number of arguments in 1.9? It doesn't in 1.8.

$ ruby -ve 'Object.new(1,2,3)'
ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
-e:1:in `initialize': wrong number of arguments (3 for 0) 
(ArgumentError)
  from -e:1:in `new'
  from -e:1
$ ruby192 -ve 'Object.new(1,2,3)'
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
$

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3260

FromJeremy Bopp <jeremy@bopp.net>
Date2011-04-20 12:44 -0500
Message-ID<4DAF1B58.1040607@bopp.net>
In reply to#3246
On 4/20/2011 10:18, Brian Candler wrote:
> Michael Edgar wrote in post #993817:
>> Proc
>> doesn't implement #initialize, so it bubbles up to Object#initialize.
>> Since Object#initialize just takes any number of arguments and ignores
>> them, providing an argument to Proc.new doesn't raise.
> 
> So I think the question becomes: why does Object#initialize accept any 
> number of arguments in 1.9? It doesn't in 1.8.

It seems that it was a bit of an oversight when the change that
implemented that feature was approved.  This is the redmine issue that
tracked the change:

http://redmine.ruby-lang.org/issues/show/2451

Here is a discussion about it:

http://www.ruby-forum.com/topic/330466

Apparently, the change was reverted for Ruby 1.9.3+.  I haven't checked
to confirm for myself yet.

-Jeremy

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.ruby


csiph-web