Displaying 6 results from an estimated 6 matches for "factorial_tr_1".
Did you mean:
factorial_tr_2
2018 Feb 11
4
Parallel assignments and goto
...nctions to implemented tail-recursion optimisations. See https://github.com/mailund/tailr
As a toy-example, consider the factorial function
factorial <- function(n, acc = 1) {
if (n <= 1) acc
else factorial(n - 1, acc * n)
}
I can automatically translate this into the loop-version
factorial_tr_1 <- function (n, acc = 1)
{
repeat {
if (n <= 1)
return(acc)
else {
.tailr_n <- n - 1
.tailr_acc <- acc * acc
n <- .tailr_n
acc <- .tailr_acc
next
}
}
}
which will run faster...
2018 Feb 11
0
Parallel assignments and goto
...See https://github.com/mailund/tailr
>
> As a toy-example, consider the factorial function
>
> factorial <- function(n, acc = 1) {
> if (n <= 1) acc
> else factorial(n - 1, acc * n)
> }
>
> I can automatically translate this into the loop-version
>
> factorial_tr_1 <- function (n, acc = 1)
> {
> repeat {
> if (n <= 1)
> return(acc)
> else {
> .tailr_n <- n - 1
> .tailr_acc <- acc * acc
> n <- .tailr_n
> acc <- .tailr_acc
> next...
2018 Feb 14
0
Parallel assignments and goto
...n) is even slower.
> factorial_tr_3 <- function (n, acc = 1) {
+ repeat {
+ if (n <= 1)
+ return(acc)
+ else {
+ passign(n = n - 1, acc = acc * n)
+ next
+ }
+ }
+ }
> microbenchmark::microbenchmark(factorial(100),
+ factorial_tr_1(100),
+ factorial_tr_2(100),
+ factorial_tr_3(100))
Unit: microseconds
expr min lq mean median uq max neval cld
factorial(100) 55.009 69.290 100.4507 104.5515 131.174 228.496 100 a
factorial_tr_1(100) 10.227 11.637...
2018 Feb 26
0
Parallel assignments and goto
...isations. See https://github.com/mailund/tailr
>
> As a toy-example, consider the factorial function
>
> factorial <- function(n, acc = 1) {
> if (n <= 1) acc
> else factorial(n - 1, acc * n)
> }
>
> I can automatically translate this into the loop-version
>
> factorial_tr_1 <- function (n, acc = 1)
> {
> repeat {
> if (n <= 1)
> return(acc)
> else {
> .tailr_n <- n - 1
> .tailr_acc <- acc * acc
> n <- .tailr_n
> acc <- .tailr_acc
> next
> }
> }
> }
>
> which will run faster and not have problems with recur...
2018 Feb 27
2
Parallel assignments and goto
...;
> > As a toy-example, consider the factorial function
> >
> > factorial <- function(n, acc = 1) {
> > if (n <= 1) acc
> > else factorial(n - 1, acc * n)
> > }
> >
> > I can automatically translate this into the loop-version
> >
> > factorial_tr_1 <- function (n, acc = 1)
> > {
> > repeat {
> > if (n <= 1)
> > return(acc)
> > else {
> > .tailr_n <- n - 1
> > .tailr_acc <- acc * acc
> > n <- .tailr_n
> > acc <- .tailr_acc
> > next
> > }
> > }
> > }...
2018 Feb 27
0
Parallel assignments and goto
...ctorial function
> > >
> > > factorial <- function(n, acc = 1) {
> > > if (n <= 1) acc
> > > else factorial(n - 1, acc * n)
> > > }
> > >
> > > I can automatically translate this into the loop-version
> > >
> > > factorial_tr_1 <- function (n, acc = 1)
> > > {
> > > repeat {
> > > if (n <= 1)
> > > return(acc)
> > > else {
> > > .tailr_n <- n - 1
> > > .tailr_acc <- acc * acc
> > > n <- .tailr_n
> > > acc <- .tailr_acc
>...