Dissecting Chain-of-Thought: Compositionality through In-Context Filtering and Learning