[R] return value of {....}

akshay kulkarni
2023-01-15T15:30:37Z

Dear leonard,
                         I think Avi's repsonse was best...it's just a
design...as he said, there are other possibilities in other programming
languages which augments this design...thanks anyways for your reply...

THanking you,
Yours sincerely,
AKSHAY M KULKARNI
________________________________
From: Leonard Mada <leo.mada at syonic.eu>
Sent: Friday, January 13, 2023 1:44 AM
To: Akshay Kulkarni <akshay_e4 at hotmail.com>
Cc: R-help Mailing List <r-help at r-project.org>
Subject: Re: [R] return value of {....}

Dear Akshay,

The best response was given by Andrew. "{...}" is not a closure.

This is unusual for someone used to C-type languages. But I will try to
explain some of the rationale.

In the case that "{...}" was a closure, then external variables would
need to be explicitly declared before the closure (in order to reuse
those values):
intermediate = c()
{
     intermediate = ...;
     result = someFUN(intermediate);
}

1.) Interactive Sessions
This is cumbersome in interactive sessions. For example: you often
compute the mean or the variance as intermediary results, and will need
them later on as well. They could have been computed outside the
"closure", but writing code in interactive sessions may not always be
straightforward.

2.) Missing arguments
f = function(x, y) {
     if(missing(y)) {
         # assuming x = matrix
         y = x[,2]; x = x[,1];
     }
}
It would be much more cumbersome to define/use a temporary tempY.

I hope this gives a better perspective why this is indeed a useful
feature - even if it is counterintuitive.

Sincerely,

Leonard



	[[alternative HTML version deleted]]