Skip to content

Instantly share code, notes, and snippets.

@vikingosegundo
Last active October 19, 2021 21:14
Show Gist options
  • Save vikingosegundo/89e47408f78a58d391ced3eb304074ed to your computer and use it in GitHub Desktop.
Save vikingosegundo/89e47408f78a58d391ced3eb304074ed to your computer and use it in GitHub Desktop.
typealias Stack<T> = (push:(T) -> (), pop:() -> (T?))
func createStack<T>() -> Stack<T> {
var array:[T] = []
return ( push: { array.append($0) },
pop: { array.count > 0 ? array.remove(at: array.count - 1) : nil } )
}
let s:Stack<Int> = createStack()
s.push(1)
s.push(2)
s.push(3)
print(String(describing: s.pop() )) // -> 3
print(String(describing: s.pop() )) // -> 2
print(String(describing: s.pop() )) // -> 1
print(String(describing: s.pop() )) // -> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment