Skip to content

Instantly share code, notes, and snippets.

@shisama
Forked from es-kumagai/CodePiece.swift
Last active January 15, 2018 16:17
Show Gist options
  • Save shisama/850473e42c057beb3ebcf965c854a337 to your computer and use it in GitHub Desktop.
Save shisama/850473e42c057beb3ebcf965c854a337 to your computer and use it in GitHub Desktop.
Swiftでは++が使えないので、その代わりにcount変数をカウントアップする関数で代用 #minna_de_swift
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