replace when ... end with lambda