Shortcut for ".compactMap { $0 }" in Combine (Swift)

I have this pet peeve where I don’t like to type out .compactMap { $0 } every time I want to remove nils from a publisher. Today I finally took some time to find a good alternative & I really like how it turned out:


extension Publisher {
    
    func compactMap<T>() -> some Publisher<T, Failure> where Output == T? {
        compactMap { $0 }
    }
    
}

Once this code has been added to your project, you can simply replace .compactMap { $0 } with .compactMap() and you’re good to go. When you need to use this in the future, autocomplete will help you get the job done much faster than typing out braces and dollar signs 😉