Skip to content

Instantly share code, notes, and snippets.

@sumitokamoi
Last active June 29, 2017 04:24
Show Gist options
  • Save sumitokamoi/ac82416fb9bb6892656a8595e7b53bf2 to your computer and use it in GitHub Desktop.
Save sumitokamoi/ac82416fb9bb6892656a8595e7b53bf2 to your computer and use it in GitHub Desktop.
クロージャ内で変数をキャプチャして使う
var nums = (10...19)
let result = nums.enumerated().flatMap { [count = nums.count, x = 10] (offset: Int, element: Int) -> [Int] in
if offset < count / 2 {
return []
}
return [element, element-x]
}
print(result) // [15, 5, 16, 6, 17, 7, 18, 8, 19, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment