//simple function that returns a closure that adds two integers
func addClosure(num:Int) -> ((Int) -> Int) {
return {(arg:Int) in print(num); return arg + num}
}
//enum to group function types and allow them to be recursive
enum function {
case end
indirect case fn((Int) -> Int, function)
var isEnd:Bool {
switch self {
case .end:
return true
default:
return false
}
}
func call(num:Int) -> Int {
switch self {
case .fn(let fnc, let next):
if next.isEnd {
return fnc(num)
}
else {
return fnc(next.call(num: num))
}
default:
return 0
}
}
}
var a = function.fn(addClosure(num: 1), function.end)
a = function.fn(addClosure(num: 2), a)
a = function.fn(addClosure(num: 3), a)
a.call(num: 0)
//1
//2
//3