6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift]reduce<U>(initial: U, combine: (U, T) -> U) -> Uを使ってみました

Posted at

Array<String> に対して reduce<U>(initial: U, combine: (U, T) -> U) -> U を使ってみました。

let list = ["tokyo", "shibuya", "shinjyuku", "akasaka"]
let result = list.filter{ $0.utf16count > 5 }
  .map { $0.uppercaseString }
  .reduce(Array<String>()) {
    var temp = $0
    temp.append($1)
    return temp
  }
result

initial で渡したものは、 combine にはimmutableで渡されるようで、以下ではエラーとなった。

  .reduce(Array<String>()) {
    $0.append($1)
    return $0
  }
Playground execution failed: error: <REPL>:44:5: error: immutable value of type 'Array<String>' only has mutating members named 'append'
    $0.append($1)
    ^  ~~~~~~
6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?