do s[1] and s[-1] do what you're looking for?
those are just to display... if you want to change s, you need to reassign
it or fiddle with namespacing.  however, I'd say it is better to write R
code as though data structures are immutable until you explicitly re-assign
them rather than trying to deal with side effects and state...


> pop <- function(vec){
+   print(vec[1])
+   print(vec[-1])
+   return(vec[-1])
+}
> s <- 1:5
> s <- pop(s)
[1] 1
[1] 2 3 4 5
> s
[1] 2 3 4 5
>


On Wed, Jan 4, 2012 at 1:22 PM, Tom Roche <Tom_Roche@pobox.com> wrote:

>
> summary: Specifically, how does one do stack/FIFO operations in R?
> Generally, how does one code functions with side effects in R?
>
> details:
>
> I have been a coder for years, mostly using C-like semantics (e.g.,
> Java). I am now trying to become a scientist, and to use R, but I don't
> yet have the sense of "good R" and R idiom (i.e., expressions that are
> to R what (e.g.) the Schwartzian transform is to Perl).
>
> I have a data-assimilation problem for which I see a solution that
> wants a stack--or, really, just a pop(...) such that
>
> * s <- c(1:5)
> * print(s)
> [1] 1 2 3 4 5
> * pop(s)
> [1] 1
> * print(s)
> [1] 2 3 4 5
>
> but in fact I get
>
> > pop(s)
> Error: could not find function "pop"
>
> and Rseek'ing finds me nothing. When I try to write pop(...) I get
>
> pop1 <- function(vector_arg) {
> +   length(vector_arg) -> lv
> +   vector_arg[1] -> ret
> +   vector_arg <<- vector_arg[2:lv]
> +   ret
> + }
> >
> > pop1(s)
> [1] 1
> > print(s)
> [1] 1 2 3 4 5
>
> i.e., no side effect on the argument
>
> pop2 <- function(vector_arg) {
> +   length(vector_arg) -> lv
> +   vector_arg[1] -> ret
> +   assign("vector_arg", vector_arg[2:lv])
> +   return(ret)
> + }
> >
> > pop2(s)
> [1] 1
> > print(s)
> [1] 1 2 3 4 5
>
> ditto :-( What am I missing?
>
> * Is there already a stack API for R (which I would expect)? If so, where?
>
> * How to cause the desired side effect to the argument in the code above?
>
> TIA, Tom Roche <Tom_Roche@pobox.com>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

