Last active
October 19, 2021 21:14
-
-
Save vikingosegundo/89e47408f78a58d391ced3eb304074ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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