LoginSignup
1
1

More than 3 years have passed since last update.

Swift:flatMap便利すぎた.

Posted at

背景

grepargumentsとして-e str1 -e str2を渡して複数の文字列でOR検索しようとしていて,今は[str1, str2]という配列を持っているなんて場面での話

flatMap便利

let strs = ["str1", "str2"]

let args = strs.flatMap { (str) -> [String] in
    return ["-e", str]
}
print(args.joined(separator: " "))
// "-e str1 -e str2"が手に入る

flatMapは多次元配列の次元を下げる以外にも使えるんですね.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1