- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Self reference for Closures
Fri, 2009-03-20, 23:48
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)
Sat, 2009-03-21, 00:47
#2
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:
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
Sat, 2009-03-21, 01:07
#3
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
>
>
Sat, 2009-03-21, 01:07
#4
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:
(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
Sat, 2009-03-21, 01:37
#5
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:
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
Sat, 2009-03-21, 14:47
#6
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
Sat, 2009-03-21, 14:57
#7
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?
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)