This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Self reference for Closures

7 replies
Peter
Joined: 2009-03-16,
User offline. Last seen 42 years 45 weeks ago.

Hoi,

just one thing I am missing in closures. I'd like them to have the equivalent
of "this" so you could do something like that without the need to assign the
closure to a variable:

list.foreach( x => this(stuff) )

instead of having to write:

val clos = { x => clos(stuff) }

list.foreach(clos)

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Self reference for Closures

On Fri, Mar 20, 2009 at 11:48:23PM +0100, Peter wrote:
> just one thing I am missing in closures. I'd like them to have the
> equivalent of "this" so you could do something like that without the
> need to assign the closure to a variable:

scala> List.range(1,10) map (new ((Int) => Int) { def apply(x: Int) = if (x == 1) x else x * this.apply(x-1) })
res0: List[Int] = List(1, 2, 6, 24, 120, 720, 5040, 40320, 362880)

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: Self reference for Closures
Or for the adventurous:

  case class B[F,T](c: B[F, T] => (F => T)) extends (B[F, T] => (F => T)) {
    def apply(b: B[F, T]) = c(b);
  }

  def Y[F, T] = (f: (F => T) => F => T) =>
    B[F, T](x => f(x(x)(_)))(B(x => f(x(x)(_))))

  (1 to 10).map(Y[Int, Int](f => i => if (i == 1) 1 else i * f(i - 1)))

--j

On Fri, Mar 20, 2009 at 4:29 PM, Paul Phillips <paulp@improving.org> wrote:
On Fri, Mar 20, 2009 at 11:48:23PM +0100, Peter wrote:
> just one thing I am missing in closures. I'd like them to have the
> equivalent of "this" so you could do something like that without the
> need to assign the closure to a variable:

scala> List.range(1,10) map (new ((Int) => Int) { def apply(x: Int) = if (x == 1) x else x * this.apply(x-1) })
res0: List[Int] = List(1, 2, 6, 24, 120, 720, 5040, 40320, 362880)

--
Paul Phillips      | We act as though comfort and luxury were the chief
Protagonist        | requirements of life, when all that we need to make us
Empiricist         | really happy is something to be enthusiastic about.
all hip pupils!    |     -- Charles Kingsley

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Self reference for Closures

I find it quite amusing that:

System.out.println(new Object() { int fac(int x) { return x < 2 ? x :
x * fac(x - 1); } }.fac(5));

is valid Java.

2009/3/20 Jorge Ortiz :
> Or for the adventurous:
>
>   case class B[F,T](c: B[F, T] => (F => T)) extends (B[F, T] => (F => T)) {
>     def apply(b: B[F, T]) = c(b);
>   }
>
>   def Y[F, T] = (f: (F => T) => F => T) =>
>     B[F, T](x => f(x(x)(_)))(B(x => f(x(x)(_))))
>
>   (1 to 10).map(Y[Int, Int](f => i => if (i == 1) 1 else i * f(i - 1)))
>
> --j
>
> On Fri, Mar 20, 2009 at 4:29 PM, Paul Phillips wrote:
>>
>> On Fri, Mar 20, 2009 at 11:48:23PM +0100, Peter wrote:
>> > just one thing I am missing in closures. I'd like them to have the
>> > equivalent of "this" so you could do something like that without the
>> > need to assign the closure to a variable:
>>
>> scala> List.range(1,10) map (new ((Int) => Int) { def apply(x: Int) = if
>> (x == 1) x else x * this.apply(x-1) })
>> res0: List[Int] = List(1, 2, 6, 24, 120, 720, 5040, 40320, 362880)
>>
>> --
>> Paul Phillips      | We act as though comfort and luxury were the chief
>> Protagonist        | requirements of life, when all that we need to make
>> us
>> Empiricist         | really happy is something to be enthusiastic about.
>> all hip pupils!    |     -- Charles Kingsley
>
>

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: Self reference for Closures
Or, for the slightly more sane:

  (1 to 10).map {
    def fac(i: Int): Int = if (i <= 1) 1 else i*fac(i-1)
    fac _
  }

--j

On Fri, Mar 20, 2009 at 4:38 PM, Jorge Ortiz <jorge.ortiz@gmail.com> wrote:
Or for the adventurous:

  case class B[F,T](c: B[F, T] => (F => T)) extends (B[F, T] => (F => T)) {
    def apply(b: B[F, T]) = c(b);
  }

  def Y[F, T] = (f: (F => T) => F => T) =>
    B[F, T](x => f(x(x)(_)))(B(x => f(x(x)(_))))

  (1 to 10).map(Y[Int, Int](f => i => if (i == 1) 1 else i * f(i - 1)))

--j

On Fri, Mar 20, 2009 at 4:29 PM, Paul Phillips <paulp@improving.org> wrote:
On Fri, Mar 20, 2009 at 11:48:23PM +0100, Peter wrote:
> just one thing I am missing in closures. I'd like them to have the
> equivalent of "this" so you could do something like that without the
> need to assign the closure to a variable:

scala> List.range(1,10) map (new ((Int) => Int) { def apply(x: Int) = if (x == 1) x else x * this.apply(x-1) })
res0: List[Int] = List(1, 2, 6, 24, 120, 720, 5040, 40320, 362880)

--
Paul Phillips      | We act as though comfort and luxury were the chief
Protagonist        | requirements of life, when all that we need to make us
Empiricist         | really happy is something to be enthusiastic about.
all hip pupils!    |     -- Charles Kingsley


Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Self reference for Closures
Jorge,
Now that you've got a Y combinator, what is the equivalent in a concurrency setting?
Best wishes,
--greg
Managing PartnerBiosimilarity LLC
On Mar 20, 2009, at 16:57, Jorge Ortiz <jorge.ortiz@gmail.com> wrote:

Or, for the slightly more sane:

  (1 to 10).map {
    def fac(i: Int): Int = if (i <= 1) 1 else i*fac(i-1)
    fac _
  }

--j

On Fri, Mar 20, 2009 at 4:38 PM, Jorge Ortiz < (jorge [dot] ortiz [at] gmail [dot] com> wrote:
Or for the adventurous:

  case class B[F,T](c: B[F, T] => (F => T)) extends (B[F, T] => (F => T)) {
    def apply(b: B[F, T]) = c(b);
  }

  def Y[F, T] = (f: (F => T) => F => T) =>
    B[F, T](x => f(x(x)(_)))(B(x => f(x(x)(_))))

  (1 to 10).map(Y[Int, Int](f => i => if (i == 1) 1 else i * f(i - 1)))

--j

On Fri, Mar 20, 2009 at 4:29 PM, Paul Phillips < (paulp [at] improving [dot] org> wrote:
On Fri, Mar 20, 2009 at 11:48:23PM +0100, Peter wrote:
> just one thing I am missing in closures. I'd like them to have the
> equivalent of "this" so you could do something like that without the
> need to assign the closure to a variable:

scala> List.range(1,10) map (new ((Int) => Int) { def apply(x: Int) = if (x == 1) x else x * this.apply(x-1) })
res0: List[Int] = List(1, 2, 6, 24, 120, 720, 5040, 40320, 362880)

--
Paul Phillips      | We act as though comfort and luxury were the chief
Protagonist        | requirements of life, when all that we need to make us
Empiricist         | really happy is something to be enthusiastic about.
all hip pupils!    |     -- Charles Kingsley


Peter
Joined: 2009-03-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Self reference for Closures

I
Actually What annoys me most about the missing self reference for closures is
that: I have to define a variable in a scope where I dont need it to access a
value in a scope where the value is known by the compiler, known by the VM,
and everyone, except they wont let me access it.

Is there some reason I dont know why this reference is not exposed to me ?

Peter

Am Samstag 21 März 2009 00:41:56 schrieb Ricky Clarkson:
> I find it quite amusing that:
>
> System.out.println(new Object() { int fac(int x) { return x < 2 ? x :
> x * fac(x - 1); } }.fac(5));
>
> is valid Java.
>
> 2009/3/20 Jorge Ortiz :
> > Or for the adventurous:
> >
> > case class B[F,T](c: B[F, T] => (F => T)) extends (B[F, T] => (F => T))
> > { def apply(b: B[F, T]) = c(b);
> > }
> >
> > def Y[F, T] = (f: (F => T) => F => T) =>
> > B[F, T](x => f(x(x)(_)))(B(x => f(x(x)(_))))
> >
> > (1 to 10).map(Y[Int, Int](f => i => if (i == 1) 1 else i * f(i - 1)))
> >
> > --j
> >
> > On Fri, Mar 20, 2009 at 4:29 PM, Paul Phillips
wrote:
> >> On Fri, Mar 20, 2009 at 11:48:23PM +0100, Peter wrote:
> >> > just one thing I am missing in closures. I'd like them to have the
> >> > equivalent of "this" so you could do something like that without the
> >> > need to assign the closure to a variable:
> >>
> >> scala> List.range(1,10) map (new ((Int) => Int) { def apply(x: Int) = if
> >> (x == 1) x else x * this.apply(x-1) })
> >> res0: List[Int] = List(1, 2, 6, 24, 120, 720, 5040, 40320, 362880)
> >>
> >> --
> >> Paul Phillips | We act as though comfort and luxury were the chief
> >> Protagonist | requirements of life, when all that we need to make
> >> us
> >> Empiricist | really happy is something to be enthusiastic about.
> >> all hip pupils! | -- Charles Kingsley

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Self reference for Closures

2009/3/21 Peter :
>
> I
> Actually What annoys me most about the missing self reference for closures is
> that: I have to define a variable in a scope where I dont need it to access a
> value in a scope where the value is known by the compiler, known by the VM,
> and everyone, except they wont let me access it.
>
> Is there some reason I dont know why this reference is not exposed to me ?

Because if you used 'this' it would shadow the 'this' that one
actually wants in the vast majority of cases and no one cared enough
about the minority of cases to add yet another language feature to
deal with them?

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland