Add state.apply function

This is just the start of the state.apply function originally requested by @ryan_lane