Add scoped function

Add a function to handle the common enter-leave pattern.
Actually I'm not sure if this is very useful - thinking of the case where `f` would throw