Last active
April 6, 2017 14:39
-
-
Save tomisacat/f8b05c5ff593a2c6df17e706223e180e to your computer and use it in GitHub Desktop.
simple illustration of `flatMap` and `reduce` to filter digits from string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s = "fdsf6fdsf7dsf7s7sdf789sdff9s8fs8df98dfsdsd5fsf" | |
let r = s.characters.flatMap { Int(String($0)) != nil ? nil : $0 }.reduce("") { $0 + String($1) } | |
Swift.print(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's inefficient and you should use
filter
with regular expression.