Take the final computation from a sequence

Is there a clean idiom for evaluating a sequence of computations and “taking” the final one?

In my case I’m doing some profiling and I do several steps to warmup and I want the last one.
The code looks sort of like the following (very simplified)

for{i <- 1 to n}{
  f(i)
  do_some_stuff
  do_some_more_stuff
  g(i)
}

I would like this to evaluate to the final call to g(i). I can of course do it in an ugly way by assign tmp = g(i) each time and then just returning tmp, but is there a more elegant way?

Not really, without it getting convoluted for little gain.

(1 to n).iterator.map(i => {
  f(i)
  do_some_stuff
  do_some_more_stuff
  g(i)
}).last

or

(1 to n).reduceLeft( (i, _) => {
  f(i)
  do_some_stuff
  do_some_more_stuff
  g(i)
})

I doubt there are any takers.

reduceLeft, that’s a clever idea. reduce is a really versatile function. :wink: