Created
April 20, 2022 03:02
-
-
Save boraseoksoon/24840f27354efd28560c40bd2817d395 to your computer and use it in GitHub Desktop.
growing a recursion
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
import Foundation | |
struct Recursion { | |
static func generated() | |
-> ((() -> Void)?) -> () | |
{ | |
var tasks: [(() -> ())?] = [] | |
let closure = { (task: (() -> Void)?) in | |
tasks.append(task) | |
tasks.forEach { $0?() } | |
} | |
return closure | |
} | |
} | |
let recur = Recursion.generated() | |
recur { print("1") } | |
recur { print("2") } | |
recur { print("3") } | |
recur { print("4") } | |
// `1` | |
// 1, `2` | |
// 1, 2, `3` | |
// 1, 2, 3, `4` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment