If you don't mind changing ntimes to not return a closure:
fn ntimes(f: &fn(T) -> T, times: uint, x: T) -> T {
match times {
0u => x,
_ => ntimes(|x| f(x), times - 1u, f(x))
}
}
fn main() {
println(format!("{:d}", ntimes(|k| k*2, 2u, 3)));
}
__
On Sun, Oct 27, 2013 at 9:41 AM, Patrick Walton wrote:
> But
> by the time the closure is invoked, `f` won't exist, because the activation
> record for the `ntimes` function won't exist anymore.
I tried tying the lifetime of `f` to the lifetime of the returned
function by making them both &'a...,
This is the closest I've been able to get. Note that it doesn't accept
stack closures. This is because ~fn doesn't seem to want to accept
non-static lifetime bounds, and potentially other issues with aliasing
stack closures. The closure story in Rust is supposed to be changing at
some point, thoug
On Sun, Oct 27, 2013 at 10:11 PM, Patrick Walton wrote:
> On 10/27/13 5:15 AM, Ramakrishnan Muthukrishnan wrote:
>>
>> I am having a hard time trying to figure out what is going on here.
>>
>> fn ntimes(f: &fn(int) -> int, n: int) -> &fn(int) -> int {
>> match n {
>> 0 => &|x| { x },
On 10/27/13 5:15 AM, Ramakrishnan Muthukrishnan wrote:
I am having a hard time trying to figure out what is going on here.
fn ntimes(f: &fn(int) -> int, n: int) -> &fn(int) -> int {
match n {
0 => &|x| { x },
_ => &|x| { f(x) },
_ => &|x|
{
l
You got me there...
On Sun, Oct 27, 2013 at 6:04 PM, Ramakrishnan Muthukrishnan <
vu3...@gmail.com> wrote:
> On Sun, Oct 27, 2013 at 6:12 PM, Oren Ben-Kiki wrote:
> > Off the top of my head, I'd take out the "&" in front of the "|x|"
> > everywhere, it seems like you are borrowing a pointer out
On Sun, Oct 27, 2013 at 6:12 PM, Oren Ben-Kiki wrote:
> Off the top of my head, I'd take out the "&" in front of the "|x|"
> everywhere, it seems like you are borrowing a pointer out of something that
> is already a borrowed pointer to a function (expected &, found &&, you have
> one & too many, r
Off the top of my head, I'd take out the "&" in front of the "|x|"
everywhere, it seems like you are borrowing a pointer out of something that
is already a borrowed pointer to a function (expected &, found &&, you have
one & too many, right? :-).
On Sun, Oct 27, 2013 at 2:15 PM, Ramakrishnan Muth
I am having a hard time trying to figure out what is going on here.
fn ntimes(f: &fn(int) -> int, n: int) -> &fn(int) -> int {
match n {
0 => &|x| { x },
_ => &|x| { f(x) },
_ => &|x|
{
let nf = ntimes(f, n - 1);
nf(f(x))
},
}