Skip to content

Instantly share code, notes, and snippets.

@tomisacat
Last active April 6, 2017 14:39
Show Gist options
  • Save tomisacat/f8b05c5ff593a2c6df17e706223e180e to your computer and use it in GitHub Desktop.
Save tomisacat/f8b05c5ff593a2c6df17e706223e180e to your computer and use it in GitHub Desktop.
simple illustration of `flatMap` and `reduce` to filter digits from string
let s = "fdsf6fdsf7dsf7s7sdf789sdff9s8fs8df98dfsdsd5fsf"
let r = s.characters.flatMap { Int(String($0)) != nil ? nil : $0 }.reduce("") { $0 + String($1) }
Swift.print(r)
@tomisacat
Copy link
Author

It's inefficient and you should use filter with regular expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment