Swift Playgrounds | Learn to Code 2 | Types | Corners of the World
I’d like to set the isActive status of both greenPortal and orangePortal by passing in their color name for use in the same expression (edit: invoked repeatedly for over multiple calls altering 1 of the 2 portals each time within a loop completing all tiles surrounding a given color of portal (please see code cite later)):
[color]Portal.isActive = false
Below is my current solve (with a screenshot of the puzzle)—managing orientation allows my simple loop to finish properly without further specification.
Having the “meta”-expression I seek would allow me to easily obviate all the shoe-leather that micro-manages between function calls. I’m open to ideas on how to improve my solution, but this post is primarily to ask if the type of semantic insertion I want is possible in Swift and if not why not, what I can/should do instead that’s a nearest approximation, and/or where I should look for what I’m after elsewhere in computing (Swift’s inout values as expounded by Sindell would seem to require 1st casting proxies for the subjects (in this case, the two portals), plus seems to deal only in properties or arguments whole, neither do I wish to predicate a reference upon inheritance or any other criterion)(I looked at LISP and Haskell in the past few days before posting this, but LISP doesn’t seem to handle branching very well at all given it insists upon returning as output a series’ final item, and Haskell doesn’t seem to ever splice its own statements within their names in the code like I’m hoping to do).
(thank you to @toph42 for the tip on formatting code here on Discourse)(please scroll within code cite to see full input):
var gathered = 0
func setGather(target: Int) {
moveForward()
while target > gathered {
if isOnGem {
collectGem()
gathered += 1
} else if isOnClosedSwitch {
toggleSwitch()
gathered += 1
} else {
turnLeft()
turnLeft()
moveForward()
turnLeft()
moveForward()
}
}
}
turnLeft()
moveForward()
moveForward() // through green portal
greenPortal.isActive = false
setGather(target: 4)
greenPortal.isActive = true
turnLeft()
turnLeft()
moveForward() // back through green portal
greenPortal.isActive = false
gathered = 0 // reset for next gathering batch
turnLeft()
turnLeft()
setGather(target: 2)
// shunting to the next set
turnLeft()
turnLeft()
moveForward()
moveForward()
moveForward()
moveForward()
moveForward() // through orange portal
orangePortal.isActive = false
gathered = 0 // reset for next set(/batch)
setGather(target: 4)
orangePortal.isActive = true
turnLeft()
turnLeft()
moveForward() // back through the orange portal
orangePortal.isActive = false
gathered = 0 // reset for next batch
turnLeft()
setGather(target: 2)