How to make smart chaining of custom pure functions