Created
June 5, 2024 12:43
-
-
Save theoknock/be1bc90e99fa3db025ca8e9fddd4c447 to your computer and use it in GitHub Desktop.
Transitions between functions when the value passed to each decrements to zero
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 | |
typealias PredicateFunction = (UInt) -> UInt | |
typealias PredicateFunctionPointer = UnsafeMutablePointer<PredicateFunction> | |
let swapPointers: () -> Void = { | |
var funcA: PredicateFunction = { predicate in | |
print("funcA returned", terminator: " ") | |
return predicate | |
} | |
var funcB: PredicateFunction = { predicate in | |
print("funcB returned", terminator: " ") | |
return predicate | |
} | |
var current: PredicateFunctionPointer = UnsafeMutablePointer(&funcA) | |
var swap: PredicateFunctionPointer = UnsafeMutablePointer(&funcB) | |
print("Invoked current(11) and", terminator: " ") | |
print(current.pointee(11)) | |
print("Swapped current with swap and then ", terminator: "") | |
(current, swap) = (swap, current) | |
print("invoked current again (with a value of 24):", terminator: " ") | |
print(current.pointee(24)) | |
} | |
swapPointers() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment