can someone explain this code to me?

Just for the record, collect does not modify the array in place, in that sense no element is replaced. (Replacement is provided by collect!)

-- fxn