For or While loops in Mathematica code always make me feel a little dirty but I was confusing myself trying to do some list munging all functional-like, and resorted to this:
(* # Given a list of {x,y} pairs, transform the data as follows: every time
# there's a decrease in y-value from one datapoint to the next, say {x1,Y}
# followed by {x2,y}, add Y to the value of every datapoint on or after x2. *)
monotonify[data_] := Module[{data0, i, offset = 0},
data0 = data;
For[i = 2, i <= Length[data], i++,
If[data[[i-1,2]] > data[[i,2]], offset += data[[i-1,2]]];
data0[[i]] += {0,offset}];
data0]
(Think of the y-values as odometer readings where sometimes the odometer gets accidentally reset -- evident because the value decreases, which odometers shouldn't do. So we transform the readings by adding the last known value before each reset to all future values.)
How would you write monotonify in a nice functional style?
(The fact that I don't consider the above For loop perfectly fine is probably a mild form of OCD.)