-
-
Save shisama/850473e42c057beb3ebcf965c854a337 to your computer and use it in GitHub Desktop.
Swiftでは++が使えないので、その代わりにcount変数をカウントアップする関数で代用 #minna_de_swift
This file contains 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 fibonacci = sequence(state: (current: 0, next: 1)) { state -> Int in | |
defer { | |
state = (current: state.next, next: state.current + state.next) | |
} | |
return state.current | |
} | |
let values = Array(fibonacci.prefix(13)) | |
var count = 0 | |
func index() -> Int { | |
defer { | |
count += 1 | |
} | |
return count | |
} | |
print(values[index()]) // 0 | |
print(values[index()]) // 1 | |
print(values[index()]) // 1 | |
print(values[index()]) // 2 | |
print(values[index()]) // 3 | |
print(values[index()]) // 5 | |
print(values[index()]) // 8 | |
print(values[index()]) // 13 | |
print(values[index()]) // 21 | |
print(values[index()]) // 34 | |
print(values[index()]) // 55 | |
print(values[index()]) // 89 | |
print(values[index()]) // 144 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment